# HG changeset patch # User terom # Date 1227546869 0 # Node ID 4a801210096c0c639a7f9630c10bad1942c18d1f # Parent 10704e1df844ecf4946ecfc57f8ddba8a40a3d1b fix movement physics+network code to some degree, jumping is now buggy? diff -r 10704e1df844 -r 4a801210096c src/proto2/GameState.cc --- a/src/proto2/GameState.cc Mon Nov 24 15:18:55 2008 +0000 +++ b/src/proto2/GameState.cc Mon Nov 24 17:14:29 2008 +0000 @@ -2,35 +2,31 @@ #include "GameState.hh" #include "Engine.hh" -void LocalPlayer::handleMove (PlayerInput_Move input) { - float fx = 0, fy = 0; +void Player::handleMove (PlayerInput_Move input) { + float fx = 0; - // handle up/down/left/right - if (input & INPUT_MOVE_UP) - fy -= PLAYER_MOVE_FORCE; - - if (input & INPUT_MOVE_DOWN) - fy += PLAYER_MOVE_FORCE; - + // handle left/right if (input & INPUT_MOVE_LEFT) fx -= PLAYER_MOVE_FORCE; if (input & INPUT_MOVE_RIGHT) fx += PLAYER_MOVE_FORCE; + + // we behave differently depending on if we're in the air or on the ground + if (inAir) { + // apply horizontal force + if (fx) + applyForce(Vector(fx, 0), INPUT_INTERVAL_MS); - // If the player if on the ground make it crawl, jump or dig - if(!inAir) { - if(fy == 0) - this->position = moveVertically(fx > 0); - else - jump(); - } else { - - if(fx) { - // apply force - applyForce(Vector(fx, 0), INPUT_INTERVAL_MS); - } - } + } else { + // walk right + if (fx) + this->position = walk(fx > 0); + + // jump? + if (input & INPUT_MOVE_JUMP) + jump(); + } } void Player::debugInfo (void) { diff -r 10704e1df844 -r 4a801210096c src/proto2/GameState.hh --- a/src/proto2/GameState.hh Mon Nov 24 15:18:55 2008 +0000 +++ b/src/proto2/GameState.hh Mon Nov 24 17:14:29 2008 +0000 @@ -38,14 +38,14 @@ } void debugInfo (); + + public: + virtual void handleMove (PlayerInput_Move input); }; class LocalPlayer : public Player { protected: LocalPlayer (GameState &state, Vector pos, bool visible) : Player(state, pos, visible) { } - - public: - virtual void handleMove (PlayerInput_Move input); }; class RemotePlayer : public Player { diff -r 10704e1df844 -r 4a801210096c src/proto2/Graphics.cc --- a/src/proto2/Graphics.cc Mon Nov 24 15:18:55 2008 +0000 +++ b/src/proto2/Graphics.cc Mon Nov 24 17:14:29 2008 +0000 @@ -53,19 +53,16 @@ if ((player = state.getLocalPlayer()) == NULL) return; - // handle up/down/left/right - if (keyboard.get_keycode(CL_KEY_UP)) - input_move |= INPUT_MOVE_UP; - - if (keyboard.get_keycode(CL_KEY_DOWN)) - input_move |= INPUT_MOVE_DOWN; - + // handle movement if (keyboard.get_keycode(CL_KEY_LEFT)) input_move |= INPUT_MOVE_LEFT; if (keyboard.get_keycode(CL_KEY_RIGHT)) input_move |= INPUT_MOVE_RIGHT; + if (keyboard.get_keycode(CL_KEY_RSHIFT)) + input_move |= INPUT_MOVE_JUMP; + if (keyboard.get_keycode(CL_KEY_I)) player->debugInfo(); diff -r 10704e1df844 -r 4a801210096c src/proto2/Input.hh --- a/src/proto2/Input.hh Mon Nov 24 15:18:55 2008 +0000 +++ b/src/proto2/Input.hh Mon Nov 24 17:14:29 2008 +0000 @@ -4,10 +4,14 @@ const uint16_t INPUT_INTERVAL_MS = 20; enum { - INPUT_MOVE_UP = 0x01, - INPUT_MOVE_DOWN = 0x02, - INPUT_MOVE_LEFT = 0x04, - INPUT_MOVE_RIGHT = 0x08, + // XXX: aiming is not movement? + INPUT_MOVE_UP = 0x0001, + INPUT_MOVE_DOWN = 0x0002, + + INPUT_MOVE_LEFT = 0x0004, + INPUT_MOVE_RIGHT = 0x0008, + + INPUT_MOVE_JUMP = 0x0010, }; typedef uint16_t PlayerInput_Move; diff -r 10704e1df844 -r 4a801210096c src/proto2/Network.hh --- a/src/proto2/Network.hh Mon Nov 24 15:18:55 2008 +0000 +++ b/src/proto2/Network.hh Mon Nov 24 17:14:29 2008 +0000 @@ -55,8 +55,7 @@ /* * Client has moved * - * Vector impulse_force - * uint16_t impulse_ms + * uint16_t PlayerInput_Move */ NETMSG_CLIENT_MOVE = 0x0201, diff -r 10704e1df844 -r 4a801210096c src/proto2/NetworkClient.cc --- a/src/proto2/NetworkClient.cc Mon Nov 24 15:18:55 2008 +0000 +++ b/src/proto2/NetworkClient.cc Mon Nov 24 17:14:29 2008 +0000 @@ -92,11 +92,10 @@ slots.connect(obj->sig_message(NETMSG_PLAYER_POSITION), this, &NetworkClientLocalPlayer::on_position); } -void NetworkClientLocalPlayer::applyForce (Vector force, uint16_t dt) { +void NetworkClientLocalPlayer::handleMove (PlayerInput_Move input) { // always send move, in all cases NetworkPacket pkt; - pkt.write_vector(force); - pkt.write_uint16(dt); + pkt.write_uint16(input); obj->send(NETMSG_CLIENT_MOVE, pkt, false); diff -r 10704e1df844 -r 4a801210096c src/proto2/NetworkClient.hh --- a/src/proto2/NetworkClient.hh Mon Nov 24 15:18:55 2008 +0000 +++ b/src/proto2/NetworkClient.hh Mon Nov 24 17:14:29 2008 +0000 @@ -45,7 +45,7 @@ public: NetworkClientLocalPlayer (NetworkClient &client, NetworkObject_Client *obj, Vector position); - virtual void applyForce (Vector force, uint16_t dt); + virtual void handleMove (PlayerInput_Move input); private: void on_position (NetworkPacket &pkt); diff -r 10704e1df844 -r 4a801210096c src/proto2/NetworkServer.cc --- a/src/proto2/NetworkServer.cc Mon Nov 24 15:18:55 2008 +0000 +++ b/src/proto2/NetworkServer.cc Mon Nov 24 17:14:29 2008 +0000 @@ -84,17 +84,18 @@ } void NetworkServerPlayer::on_move (NetworkNode *src, NetworkPacket &pkt) { - // sanity-check - if (src != node) + // sanity-check, other players shouldn't move + if (src != node) { + Engine::log(WARN, "server_player.on_move") << "packet from wrong src=" << src << ", node=" << node; return; + } - Vector impulse_force = pkt.read_vector(); - uint16_t impulse_ms = pkt.read_uint16(); + PlayerInput_Move input = pkt.read_uint16(); - Engine::log(INFO, "server_player.on_move") << "player=" << obj << ", old_pos=" << position << ", impulse=" << impulse_force << "@" << impulse_ms << "ms"; + Engine::log(INFO, "server_player.on_move") << "player=" << obj << ", old_pos=" << position << ", input=" << input; - // apply force - applyForce(impulse_force, impulse_ms); + // apply input + handleMove(input); // send position update send_position_update(); diff -r 10704e1df844 -r 4a801210096c src/proto2/Physics.cc --- a/src/proto2/Physics.cc Mon Nov 24 15:18:55 2008 +0000 +++ b/src/proto2/Physics.cc Mon Nov 24 17:14:29 2008 +0000 @@ -41,7 +41,7 @@ * Make the worm walk on the ground. * @return Final position. */ -Vector PhysicsObject::moveVertically (bool right) { +Vector PhysicsObject::walk (bool right) { Vector cursor = right ? this->position + Vector(1,0) : this->position + Vector(-1,0); Vector reached = this->position; @@ -101,8 +101,8 @@ } void PhysicsObject::jump () { - this->velocity.y = -100; - this->inAir = true; + velocity.y = -100; + inAir = true; } bool PhysicsObject::possibleLocation (Vector loc) { @@ -332,7 +332,8 @@ * @param dt The time the force is applied. */ void PhysicsObject::applyForce (Force force, TimeMS dt) { - + // XXX: dt is not used? Is it assumed to be the same as the integrate() dt? + // Add applied force to the queue forceq.push(force); this->inAir = true; diff -r 10704e1df844 -r 4a801210096c src/proto2/Physics.hh --- a/src/proto2/Physics.hh Mon Nov 24 15:18:55 2008 +0000 +++ b/src/proto2/Physics.hh Mon Nov 24 17:14:29 2008 +0000 @@ -77,11 +77,28 @@ */ PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity); + /** + * Used to handle in-air movement + */ virtual void applyForce (Force force, TimeMS dt); + + /** + * Called on network clients to sync state from server + */ void updatePhysics (Vector position, Vector velocity); - Vector moveVertically (bool right); + + /** + * Handle ground movement + * + * @return new position + */ + Vector walk (bool right); + + /** + * Handle ground-jumping + */ void jump (void); - + private: void updatePosition (void); bool possibleLocation (Vector loc);