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 | 112 +++++++++++++++++++++++++++++-------
src/character/character.h | 15 ++++-
src/include/action_handler.cpp | 33 +++++++++++
4 files changed, 139 insertions(+), 25 deletions(-)
diff --git a/lib/wormux/include/WORMUX_action.h
b/lib/wormux/include/WORMUX_action.h
index 32460f3..1675417 100644
--- a/lib/wormux/include/WORMUX_action.h
+++ b/lib/wormux/include/WORMUX_action.h
@@ -74,6 +74,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 37aac08..f259f88 100644
--- a/src/character/character.cpp
+++ b/src/character/character.cpp
@@ -124,6 +124,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)
{
@@ -180,6 +184,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)
{
@@ -339,6 +347,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());
@@ -550,6 +563,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) {
+ MoveLeft(move_left_slowly_pressed);
+ } else if (right && !left) {
+ MoveRight(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,52 +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);
+void Character::StopMovingRight(bool slowly)
+{
+ if (slowly)
+ move_right_slowly_pressed = false;
+ else
+ move_right_pressed = false;
+ StopWalkingIfNecessary();
}
-void Character::HandleKeyRefreshed_MoveRight(bool slowly)
+void Character::HandleKeyPressed_MoveRight(bool slowly)
{
- HideGameInterface();
+ Action a(Action::ACTION_CHARACTER_START_MOVING_RIGHT);
+ a.Push(slowly ? 1 : 0);
+ Network::GetInstance()->SendActionToAll(a);
- ActiveTeam().crosshair.Hide();
+ StartMovingRight(slowly);
- if (IsImmobile())
- MoveRight(slowly);
+ HideGameInterface();
+ ActiveTeam().crosshair.Hide();
}
-void Character::HandleKeyReleased_MoveRight(bool /*slowly*/)
+void Character::HandleKeyReleased_MoveRight(bool slowly)
{
- StopWalk();
+ Action a(Action::ACTION_CHARACTER_STOP_MOVING_RIGHT);
+ a.Push(slowly ? 1 : 0);
+ Network::GetInstance()->SendActionToAll(a);
+
+ StopMovingRight(slowly);
ActiveTeam().crosshair.Show();
}
// #################### 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);
+void Character::StopMovingLeft(bool slowly)
+{
+ if (slowly)
+ move_left_slowly_pressed = false;
+ else
+ move_left_pressed = false;
+ StopWalkingIfNecessary();
}
-void Character::HandleKeyRefreshed_MoveLeft(bool slowly)
+void Character::HandleKeyPressed_MoveLeft(bool slowly)
{
- HideGameInterface();
+ Action a(Action::ACTION_CHARACTER_START_MOVING_LEFT);
+ a.Push(slowly ? 1 : 0);
+ Network::GetInstance()->SendActionToAll(a);
- ActiveTeam().crosshair.Hide();
+ StartMovingLeft(slowly);
- if (IsImmobile())
- MoveLeft(slowly);
+ HideGameInterface();
+ ActiveTeam().crosshair.Hide();
}
-void Character::HandleKeyReleased_MoveLeft(bool /*slowly*/)
+void Character::HandleKeyReleased_MoveLeft(bool slowly)
{
- body->StopWalk();
+ Action a(Action::ACTION_CHARACTER_STOP_MOVING_LEFT);
+ a.Push(slowly ? 1 : 0);
+ Network::GetInstance()->SendActionToAll(a);
+
+ StopMovingLeft(slowly);
ActiveTeam().crosshair.Show();
}
@@ -1064,5 +1134,3 @@ void Character::HandleKeyPressed_BackJump()
}
}
-
-
diff --git a/src/character/character.h b/src/character/character.h
index 9e7a0ea..0fe5daa 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);
@@ -177,6 +181,11 @@ public:
void MoveRight(bool slowly);
void MoveLeft(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();
@@ -220,11 +229,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 b802d5b..2653c42 100644
--- a/src/include/action_handler.cpp
+++ b/src/include/action_handler.cpp
@@ -575,6 +575,34 @@ static void Action_Character_BackJump (Action */*a*/)
ActiveCharacter().BackJump();
}
+static void Action_Character_StartMovingLeft(Action *a)
+{
+ ASSERT(!Network::GetInstance()->IsTurnMaster());
+ bool slowly = a->PopInt();
+ ActiveCharacter().StartMovingLeft(slowly);
+}
+
+static void Action_Character_StopMovingLeft(Action *a)
+{
+ ASSERT(!Network::GetInstance()->IsTurnMaster());
+ bool slowly = a->PopInt();
+ ActiveCharacter().StopMovingLeft(slowly);
+}
+
+static void Action_Character_StartMovingRight(Action *a)
+{
+ ASSERT(!Network::GetInstance()->IsTurnMaster());
+ bool slowly = a->PopInt();
+ ActiveCharacter().StartMovingRight(slowly);
+}
+
+static void Action_Character_StopMovingRight(Action *a)
+{
+ ASSERT(!Network::GetInstance()->IsTurnMaster());
+ bool slowly = a->PopInt();
+ ActiveCharacter().StopMovingRight(slowly);
+}
+
static void Action_Weapon_Shoot (Action *a)
{
if (Game::GetInstance()->ReadState() != Game::PLAYING)
@@ -926,6 +954,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);
--
1.6.0.4
_______________________________________________
Wormux-dev mailing list
[email protected]
https://mail.gna.org/listinfo/wormux-dev