void Player::Update(const jutil::GameTime& timespan) { if (m_Health <= 0) { HandleDeath(timespan); return; } if (m_TimerInvincibility.ElapsedMilliseconds() < INVINCIBILITY_TIME) { HandleInvincibility(timespan); } else { SetAlpha(1.f); m_PlayerState = UNKNOWN; } HandleLevelCollisionResolution(timespan); // Jumping controls if (m_PlayerState != TAKING_DAMAGE) { HandleControls(timespan); } HandleBullets(timespan); SetAnimationFrame(); }
void Character2D::HandleWoundedState(float timeStep) { auto* body = GetComponent<RigidBody2D>(); auto* animatedSprite = GetComponent<AnimatedSprite2D>(); // Play "hit" animation in loop if (animatedSprite->GetAnimation() != "hit") animatedSprite->SetAnimation("hit", LM_FORCE_LOOPED); // Update timer timer_ += timeStep; if (timer_ > 2.0f) { // Reset timer timer_ = 0.0f; // Clear forces (should be performed by setting linear velocity to zero, but currently doesn't work) body->SetLinearVelocity(Vector2::ZERO); body->SetAwake(false); body->SetAwake(true); // Remove particle emitter node_->GetChild("Emitter", true)->Remove(); // Update lifes UI and counter remainingLifes_ -= 1; auto* ui = GetSubsystem<UI>(); Text* lifeText = static_cast<Text*>(ui->GetRoot()->GetChild("LifeText", true)); lifeText->SetText(String(remainingLifes_)); // Update lifes UI counter // Reset wounded state wounded_ = false; // Handle death if (remainingLifes_ == 0) { HandleDeath(); return; } // Re-position the character to the nearest point if (node_->GetPosition().x_ < 15.0f) node_->SetPosition(Vector3(1.0f, 8.0f, 0.0f)); else node_->SetPosition(Vector3(18.8f, 9.2f, 0.0f)); } }
//announceDamage: announce to the player that he had injured void GamePlayer::announceDamage(GameObj* obj,GameManager* mgr) { if ( obj->ClassType() == typeGamePlayer ) { GamePlayer* otherPlayer = (GamePlayer*)obj; if (otherPlayer->getPower() == getPower()) { otherPlayer->HitByPlayer(Same,mgr); HitByPlayer(Same,mgr); } else if (otherPlayer->getPower() < getPower()) { otherPlayer->HitByPlayer(Higher,mgr); HitByPlayer(Lower,mgr); } else { otherPlayer->HitByPlayer(Lower,mgr); HitByPlayer(Higher,mgr); } } else if ( obj->ClassType() == typeGameArrow ) { m_power -= 500; HandleDeath(mgr); } }
//Move: Performs game round for Player. void GamePlayer::Move(GameManager* mgr) { if (m_pauseMoveRounds == 0) { // check if player can move Undraw(); Point p = GetPosition(); p.set(p.getX()+m_direction.getX(),p.getY()+m_direction.getY()); // set new postion fixPoint(p); //if needed fix the position of the point bool move=!mgr->isValidPosition(p); if (!move) // in case the new position is valid, do change of direction randomly move = (rand() % 10 == 0); while (move) { // if new poisiton is not valid get valid new position. p=GetPosition(); m_direction.set(rand() % 3 - 1,rand() % 3 - 1); p.set(p.getX()+m_direction.getX(),p.getY()+m_direction.getY()); fixPoint(p); if ( mgr->isValidPosition(p) && GetPosition().comper(p) != 0 ) move=false; } SetPosition(p); //set the new position m_pauseMoveRounds=PAUSE_MOVE_AFTER_MOVE; //update waiting rounds Draw(mgr); switch(mgr->TakeMapObject(p)) { // check if payer is on gift case GlobalConsts::Food: m_power+=ADD_POWER; break; case GlobalConsts::Quiver: m_numOfArrows+=ADD_ARROWS; break; case GlobalConsts::Bomb: m_power-=LOSE_POWER; break; } HandleDeath(mgr); // check death if (m_pauseArrowsRounds == 0 && m_numOfArrows > 0 ) // handle shooting arrow { Point arrowPosition(GetPosition()); arrowPosition.set(arrowPosition.getX() + m_direction.getX(),arrowPosition.getY() + m_direction.getY()); if (mgr->isValidPosition(arrowPosition)) { mgr->createArrow(arrowPosition,m_direction); m_pauseArrowsRounds = PAUSE_ARROWS_AFTER_SHOOT; --m_numOfArrows; } } else if ( m_pauseArrowsRounds > 0 ) { m_pauseArrowsRounds--; } } else { --m_pauseMoveRounds; } }