#include #include #include #include #include #include namespace Game::AGame { void Player::setShipTexture(std::shared_ptr tex) { mShipTex = std::move(tex); if (mIsShipMode && mShipTex) { setTexture(mShipTex); } } void Player::setGroundTexture(std::shared_ptr tex) { mGroundTex = std::move(tex); if (!mIsShipMode && mGroundTex) { setTexture(mGroundTex); } } void Player::respawnRandomSea(float landBoundaryX) { auto spawnTex = mShipTex ? mShipTex : mTex; if (!spawnTex) { return; } // Use logical world dimensions (1280×720) not actual screen size constexpr int w = 1280; constexpr int h = 720; const float halfWidth = spawnTex->getWidth() * mTransform.adjustedScaleX() / 2.f; const float halfHeight = spawnTex->getHeight() * mTransform.adjustedScaleY() / 2.f; const float minCenterX = landBoundaryX + mShoreMargin + halfWidth; const float maxCenterX = w / 2.f - halfWidth - 10.f; const float minCenterY = -h / 2.f + halfHeight; const float maxCenterY = h / 2.f - halfHeight; float centerX = minCenterX; if (maxCenterX > minCenterX) { centerX = static_cast(Utils::getUtils().rirng32(static_cast(minCenterX), static_cast(maxCenterX))); } float centerY = 0.f; if (maxCenterY > minCenterY) { centerY = static_cast(Utils::getUtils().rirng32(static_cast(minCenterY), static_cast(maxCenterY))); } mTransform.x = centerX - halfWidth; mTransform.y = centerY - halfHeight; mIsShipMode = true; if (mShipTex) { setTexture(mShipTex); } } void Player::start() { //mSound = Object::Sound("../resources/example.wav", Object::Format::WAV); //mSound.play(); mZIndex = 100; Game::GameManager::setSharedData("gameStage", 1); Game::GameManager::setSharedData("gameScore", 0); mTransform.scaleX = 5.3f; mTransform.scaleY = 5.3f; if (!mShipTex) { mShipTex = mTex; } if (!mGroundTex) { mGroundTex = mTex; } // Use logical world dimensions (1280×720) not actual screen size constexpr int w = 1280; constexpr int h = 720; const float halfWidth = mTex->getWidth() * mTransform.adjustedScaleX() / 2.f; const float halfHeight = mTex->getHeight() * mTransform.adjustedScaleY() / 2.f; const float minX = -w / 2.f + halfWidth; const float maxX = w / 2.f - halfWidth; const float minY = -h / 2.f + halfHeight; const float maxY = h / 2.f - halfHeight; const float landBoundaryX = Game::GameManager::getSharedData("terrainLandBoundaryX"); mTransform.x = static_cast(Utils::getUtils().rirng32(static_cast(minX), static_cast(maxX))); mTransform.y = static_cast(Utils::getUtils().rirng32(static_cast(minY), static_cast(maxY))); mIsShipMode = (mTransform.x + halfWidth) >= landBoundaryX; mTransform.x -= mTex->getWidth() * mTransform.adjustedScaleX() / 2.f; mTransform.y -= mTex->getHeight() * mTransform.adjustedScaleY() / 2.f; if (mIsShipMode && mShipTex) { setTexture(mShipTex); } else if (!mIsShipMode && mGroundTex) { setTexture(mGroundTex); } LOG("W: " << w << " H: " << h); //mSound.~Sound(); } void Player::update(float deltaTime) { //mTransform.rotation += 1.f; // Rotate clockwise for testing //mTransform.scaleX = 1.f + 1.f * std::sin(RUNNING_TIME() / 0.5f); // Pulsate scale for testing //mTransform.scaleY = 1.f + 0.5f * std::cos(RUNNING_TIME() / 0.5f); // Pulsate scale for testing //Object::Camera::getInstance().move(1.f, 0.f); if (mStateTransitionCooldownTimer > 0.f) { mStateTransitionCooldownTimer -= deltaTime; if (mStateTransitionCooldownTimer < 0.f) { mStateTransitionCooldownTimer = 0.f; } } const float landBoundaryX = Game::GameManager::getSharedData("terrainLandBoundaryX"); const float halfWidth = mTex->getWidth() * mTransform.adjustedScaleX() / 2.f; if (Input::isKeyPressed(SDL_SCANCODE_E)) { const bool nearShore = std::abs((mTransform.x + halfWidth) - landBoundaryX) <= mShoreMargin; if (nearShore && mStateTransitionCooldownTimer <= 0.f) { mIsShipMode = !mIsShipMode; mStateTransitionCooldownTimer = mStateTransitionCooldown; } } // Simple movement if (Input::isKeyPressed(SDL_SCANCODE_W)) { mTransform.y -= mSpeed * deltaTime; } if (Input::isKeyPressed(SDL_SCANCODE_S)) { mTransform.y += mSpeed * deltaTime; } if (Input::isKeyPressed(SDL_SCANCODE_A)) { mTransform.x -= mSpeed * deltaTime; mIsFlipped = false; } if (Input::isKeyPressed(SDL_SCANCODE_D)) { mTransform.x += mSpeed * deltaTime; mIsFlipped = true; } mSpeed = Input::isKeyPressed(SDL_SCANCODE_LSHIFT) ? 400.f : 200.f; // Use logical world dimensions (1280×720) not actual screen size constexpr int w = 1280; constexpr int h = 720; const float entityWidth = mTex ? mTex->getWidth() * mTransform.adjustedScaleX() : 0.f; const float entityHeight = mTex ? mTex->getHeight() * mTransform.adjustedScaleY() : 0.f; const float minX = -w / 2.f; const float maxX = w / 2.f - entityWidth; const float minY = -h / 2.f; const float maxY = h / 2.f - entityHeight; if (mTransform.x < minX) mTransform.x = minX; if (mTransform.x > maxX) mTransform.x = maxX; if (mTransform.y < minY) mTransform.y = minY; if (mTransform.y > maxY) mTransform.y = maxY; if (mIsShipMode && (mTransform.x + halfWidth) < landBoundaryX + mShoreMargin) { mTransform.x = landBoundaryX + mShoreMargin - halfWidth; } if (!mIsShipMode && (mTransform.x + halfWidth) > landBoundaryX - mShoreMargin) { mTransform.x = landBoundaryX - mShoreMargin - halfWidth; } if (mIsShipMode && mShipTex) { setTexture(mShipTex); } else if (!mIsShipMode && mGroundTex) { setTexture(mGroundTex); } // Push replay (position and form state) GameManager::pushPlayerPosition(mTransform); GameManager::pushPlayerFormState(mIsShipMode); } void Player::onCollisionEnter(Object::Entity* other) { (void)other; if (GameManager::getSharedData("gameLost")) { GameManager::destroyEntity(this); } } }