Looks good :) Florian Köberle a écrit : > Previous this patch the character got moved by the HandleKeyRefreshed_Move* > methods. > Sending a character movement notification every frame would cause a lot of > network traffic. > This patch let the turn master only send two actions for a simple movement: > One which starts the movement and one which stops it. > --- > lib/wormux/include/WORMUX_action.h | 4 + > src/character/character.cpp | 118 > ++++++++++++++++++++++++++++-------- > src/character/character.h | 15 ++++- > src/include/action_handler.cpp | 29 +++++++++ > 4 files changed, 137 insertions(+), 29 deletions(-) > > diff --git a/lib/wormux/include/WORMUX_action.h > b/lib/wormux/include/WORMUX_action.h > index c5d0f05..c396fb4 100644 > --- a/lib/wormux/include/WORMUX_action.h > +++ b/lib/wormux/include/WORMUX_action.h > @@ -82,6 +82,10 @@ public: > ACTION_CHARACTER_JUMP, > ACTION_CHARACTER_HIGH_JUMP, > ACTION_CHARACTER_BACK_JUMP, > + ACTION_CHARACTER_START_MOVING_LEFT, > + ACTION_CHARACTER_STOP_MOVING_LEFT, > + ACTION_CHARACTER_START_MOVING_RIGHT, > + ACTION_CHARACTER_STOP_MOVING_RIGHT, > > // ######################################################## > // Using Weapon > diff --git a/src/character/character.cpp b/src/character/character.cpp > index f65edda..cab8bf6 100644 > --- a/src/character/character.cpp > +++ b/src/character/character.cpp > @@ -130,6 +130,10 @@ Character::Character (Team& my_team, const std::string > &name, Body *char_body) : > channel_step(-1), > particle_engine(new ParticleEngine(500)), > is_playing(false), > + move_left_pressed(false), > + move_left_slowly_pressed(false), > + move_right_pressed(false), > + move_right_slowly_pressed(false), > previous_strength(0), > body(NULL) > { > @@ -186,6 +190,10 @@ Character::Character (const Character& acharacter) : > channel_step(acharacter.channel_step), > particle_engine(new ParticleEngine(250)), > is_playing(acharacter.is_playing), > + move_left_pressed(false), > + move_left_slowly_pressed(false), > + move_right_pressed(false), > + move_right_slowly_pressed(false), > previous_strength(acharacter.previous_strength), > body(NULL) > { > @@ -345,6 +353,11 @@ void Character::Die() > SetMovement("breathe"); > SetCollisionModel(true, false, false); > > + move_left_pressed = false; > + move_left_slowly_pressed = false; > + move_right_pressed = false; > + move_right_slowly_pressed = false; > + > if(death_explosion) > ApplyExplosion(GetCenter(), > GameMode::GetInstance()->death_explosion_cfg); > ASSERT(IsDead()); > @@ -518,6 +531,16 @@ void Character::UpdateLastMovingTime() > > void Character::Refresh() > { > + if (IsImmobile()) { > + bool left =(move_left_pressed || move_left_slowly_pressed); > + bool right = (move_right_pressed || move_right_slowly_pressed); > + if (left && !right) { > + Move(DIRECTION_LEFT, move_left_slowly_pressed); > + } else if (right && !left) { > + Move(DIRECTION_RIGHT, move_right_slowly_pressed); > + } > + } > + > if (IsGhost()) return; > > UpdatePosition(); > @@ -814,6 +837,10 @@ void Character::StopPlaying() > SetMovement("breathe"); > body->ResetWalk(); > SetRebounding(true); > + move_left_pressed = false; > + move_left_slowly_pressed = false; > + move_right_pressed = false; > + move_right_slowly_pressed = false; > } > > // Begining of turn or changed to this character > @@ -939,54 +966,95 @@ void Character::SetCustomName(const std::string name) > // ################################################################### > // ################################################################### > > +void Character::StopWalkingIfNecessary() > +{ > + bool right = move_right_pressed || move_right_slowly_pressed; > + bool left = move_left_pressed || move_left_slowly_pressed; > + if ((right == left) || (!left && !right)) > + body->StopWalk(); > +} > + > // #################### MOVE_RIGHT > -void Character::HandleKeyPressed_MoveRight(bool slowly) > +void Character::StartMovingRight(bool slowly) > { > - StartWalk(slowly); > + if (slowly) > + move_right_slowly_pressed = true; > + else > + move_right_pressed = true; > + StopWalkingIfNecessary(); > > - HandleKeyRefreshed_MoveRight(slowly); > + if (Network::GetInstance()->IsTurnMaster()) { > + HideGameInterface(); > + ActiveTeam().crosshair.Hide(); > + } > } > > -void Character::HandleKeyRefreshed_MoveRight(bool slowly) > +void Character::StopMovingRight(bool slowly) > { > - HideGameInterface(); > - > - ActiveTeam().crosshair.Hide(); > + if (slowly) > + move_right_slowly_pressed = false; > + else > + move_right_pressed = false; > + StopWalkingIfNecessary(); > > - if (IsImmobile()) > - Move(DIRECTION_RIGHT, slowly); > + if (Network::GetInstance()->IsTurnMaster()) > + ActiveTeam().crosshair.Show(); > } > > -void Character::HandleKeyReleased_MoveRight(bool /*slowly*/) > +void Character::HandleKeyPressed_MoveRight(bool slowly) > { > - StopWalk(); > + Action *a = new Action(Action::ACTION_CHARACTER_START_MOVING_RIGHT); > + a->Push(slowly ? 1 : 0); > + ActionHandler::GetInstance()->NewAction(a); > +} > > - ActiveTeam().crosshair.Show(); > +void Character::HandleKeyReleased_MoveRight(bool slowly) > +{ > + Action *a = new Action(Action::ACTION_CHARACTER_STOP_MOVING_RIGHT); > + a->Push(slowly ? 1 : 0); > + ActionHandler::GetInstance()->NewAction(a); > } > > // #################### MOVE_LEFT > -void Character::HandleKeyPressed_MoveLeft(bool slowly) > + > +void Character::StartMovingLeft(bool slowly) > { > - StartWalk(slowly); > + if (slowly) > + move_left_slowly_pressed = true; > + else > + move_left_pressed = true; > + StopWalkingIfNecessary(); > > - HandleKeyRefreshed_MoveLeft(slowly); > + if (Network::GetInstance()->IsTurnMaster()) { > + HideGameInterface(); > + ActiveTeam().crosshair.Hide(); > + } > } > > -void Character::HandleKeyRefreshed_MoveLeft(bool slowly) > +void Character::StopMovingLeft(bool slowly) > { > - HideGameInterface(); > - > - ActiveTeam().crosshair.Hide(); > + if (slowly) > + move_left_slowly_pressed = false; > + else > + move_left_pressed = false; > + StopWalkingIfNecessary(); > > - if (IsImmobile()) > - Move(DIRECTION_LEFT, slowly); > + if (Network::GetInstance()->IsTurnMaster()) > + ActiveTeam().crosshair.Show(); > } > > -void Character::HandleKeyReleased_MoveLeft(bool /*slowly*/) > +void Character::HandleKeyPressed_MoveLeft(bool slowly) > { > - body->StopWalk(); > + Action *a = new Action(Action::ACTION_CHARACTER_START_MOVING_LEFT); > + a->Push(slowly ? 1 : 0); > + ActionHandler::GetInstance()->NewAction(a); > +} > > - ActiveTeam().crosshair.Show(); > +void Character::HandleKeyReleased_MoveLeft(bool slowly) > +{ > + Action *a = new Action(Action::ACTION_CHARACTER_STOP_MOVING_LEFT); > + a->Push(slowly ? 1 : 0); > + ActionHandler::GetInstance()->NewAction(a); > } > > // #################### UP > @@ -1064,5 +1132,3 @@ void Character::HandleKeyPressed_BackJump() > } > } > > - > - > diff --git a/src/character/character.h b/src/character/character.h > index ce9b54f..88436e0 100644 > --- a/src/character/character.h > +++ b/src/character/character.h > @@ -83,6 +83,10 @@ private: > // this is needed because of network needing to know > // if we have changed of active character > bool is_playing; > + bool move_left_pressed; > + bool move_left_slowly_pressed; > + bool move_right_pressed; > + bool move_right_slowly_pressed; > public: > > // Previous strength > @@ -108,7 +112,7 @@ private: > void StartWalk(bool slowly); > void StopWalk(); > bool IsWalking() const; > - > + void StopWalkingIfNecessary(); > public: > > Character (Team& my_team, const std::string &name, Body *char_body); > @@ -176,6 +180,11 @@ public: > bool CanJump() const { return CanMoveRL(); }; > void Move(enum BodyDirection direction, bool slowly); > > + void StartMovingLeft(bool slowly); > + void StopMovingLeft(bool slowly); > + void StartMovingRight(bool slowly); > + void StopMovingRight(bool slowly); > + > // Jumps > void Jump(double strength, double angle); > void Jump(); > @@ -219,11 +228,11 @@ public: > > // Keyboard handling > void HandleKeyPressed_MoveRight(bool slowly); > - void HandleKeyRefreshed_MoveRight(bool slowly); > + void HandleKeyRefreshed_MoveRight(bool /*slowly*/) {}; > void HandleKeyReleased_MoveRight(bool slowly); > > void HandleKeyPressed_MoveLeft(bool slowly); > - void HandleKeyRefreshed_MoveLeft(bool slowly); > + void HandleKeyRefreshed_MoveLeft(bool /*slowly*/) {}; > void HandleKeyReleased_MoveLeft(bool slowly); > > void HandleKeyPressed_Up(bool slowly) { HandleKeyRefreshed_Up(slowly); }; > diff --git a/src/include/action_handler.cpp b/src/include/action_handler.cpp > index e7aa6e9..36eed52 100644 > --- a/src/include/action_handler.cpp > +++ b/src/include/action_handler.cpp > @@ -576,6 +576,30 @@ static void Action_Character_BackJump (Action */*a*/) > ActiveCharacter().BackJump(); > } > > +static void Action_Character_StartMovingLeft(Action *a) > +{ > + bool slowly = a->PopInt(); > + ActiveCharacter().StartMovingLeft(slowly); > +} > + > +static void Action_Character_StopMovingLeft(Action *a) > +{ > + bool slowly = a->PopInt(); > + ActiveCharacter().StopMovingLeft(slowly); > +} > + > +static void Action_Character_StartMovingRight(Action *a) > +{ > + bool slowly = a->PopInt(); > + ActiveCharacter().StartMovingRight(slowly); > +} > + > +static void Action_Character_StopMovingRight(Action *a) > +{ > + bool slowly = a->PopInt(); > + ActiveCharacter().StopMovingRight(slowly); > +} > + > static void Action_Weapon_Shoot (Action *a) > { > if (Game::GetInstance()->ReadState() != Game::PLAYING) > @@ -934,6 +958,11 @@ void Action_Handler_Init() > ActionHandler::GetInstance()->Register (Action::ACTION_CHARACTER_JUMP, > "CHARACTER_jump", &Action_Character_Jump); > ActionHandler::GetInstance()->Register > (Action::ACTION_CHARACTER_HIGH_JUMP, "CHARACTER_super_jump", > &Action_Character_HighJump); > ActionHandler::GetInstance()->Register > (Action::ACTION_CHARACTER_BACK_JUMP, "CHARACTER_back_jump", > &Action_Character_BackJump); > + ActionHandler::GetInstance()->Register > (Action::ACTION_CHARACTER_START_MOVING_LEFT, "CHARACTER_start_moving_left", > &Action_Character_StartMovingLeft); > + ActionHandler::GetInstance()->Register > (Action::ACTION_CHARACTER_STOP_MOVING_LEFT, "CHARACTER_stop_moving_left", > &Action_Character_StopMovingLeft); > + ActionHandler::GetInstance()->Register > (Action::ACTION_CHARACTER_START_MOVING_RIGHT, "CHARACTER_start_moving_right", > &Action_Character_StartMovingRight); > + ActionHandler::GetInstance()->Register > (Action::ACTION_CHARACTER_STOP_MOVING_RIGHT, "CHARACTER_stop_moving_right", > &Action_Character_StopMovingRight); > + > // ######################################################## > // Using Weapon > ActionHandler::GetInstance()->Register (Action::ACTION_WEAPON_SHOOT, > "WEAPON_shoot", &Action_Weapon_Shoot); >
_______________________________________________ Wormux-dev mailing list Wormux-dev@gna.org https://mail.gna.org/listinfo/wormux-dev