make spawn/die work over the network
authorterom
Mon, 08 Dec 2008 18:31:08 +0000
changeset 302 e734d8e9bbb5
parent 301 02ad02d28245
child 303 36a502a0023f
make spawn/die work over the network
src/Network/Client.cc
src/Network/Client.hh
src/Network/Protocol.hh
src/Network/Server.cc
src/Network/Server.hh
src/Player.cc
src/Player.hh
--- a/src/Network/Client.cc	Mon Dec 08 18:17:37 2008 +0000
+++ b/src/Network/Client.cc	Mon Dec 08 18:31:08 2008 +0000
@@ -152,13 +152,33 @@
     Player(client.state, position, true),
     NetworkClientObject(client, obj_id)
 {
-    slots.connect(sig_message(NETMSG_PLAYER_POSITION),         this,   &NetworkClientPlayerBase::on_position         );
-    slots.connect(sig_message(NETMSG_PLAYER_DIG),              this,   &NetworkClientPlayerBase::on_dig              );
-    slots.connect(sig_message(NETMSG_PLAYER_WEAPON_CHANGE),    this,   &NetworkClientPlayerBase::on_weapon_change    );
-    slots.connect(sig_message(NETMSG_PLAYER_ROPE_THROW),       this,   &NetworkClientPlayerBase::on_rope_throw       );
-    slots.connect(sig_message(NETMSG_PLAYER_ROPE_FIXED),       this,   &NetworkClientPlayerBase::on_rope_fixed       );
-    slots.connect(sig_message(NETMSG_PLAYER_ROPE_RELEASED),    this,   &NetworkClientPlayerBase::on_rope_released    );
-    slots.connect(sig_message(NETMSG_PLAYER_ROPE_LENGTH),      this,   &NetworkClientPlayerBase::on_rope_length      );
+    slots.connect(sig_message(NETMSG_PLAYER_POSITION),          this,   &NetworkClientPlayerBase::on_position           );
+    slots.connect(sig_message(NETMSG_PLAYER_DIG),               this,   &NetworkClientPlayerBase::on_dig                );
+    slots.connect(sig_message(NETMSG_PLAYER_WEAPON_CHANGE),     this,   &NetworkClientPlayerBase::on_weapon_change      );
+    slots.connect(sig_message(NETMSG_PLAYER_ROPE_THROW),        this,   &NetworkClientPlayerBase::on_rope_throw         );
+    slots.connect(sig_message(NETMSG_PLAYER_ROPE_FIXED),        this,   &NetworkClientPlayerBase::on_rope_fixed         );
+    slots.connect(sig_message(NETMSG_PLAYER_ROPE_RELEASED),     this,   &NetworkClientPlayerBase::on_rope_released      );
+    slots.connect(sig_message(NETMSG_PLAYER_ROPE_LENGTH),       this,   &NetworkClientPlayerBase::on_rope_length        );
+    slots.connect(sig_message(NETMSG_PLAYER_SPAWN),             this,   &NetworkClientPlayerBase::on_spawn              );
+    slots.connect(sig_message(NETMSG_PLAYER_DIE),               this,   &NetworkClientPlayerBase::on_die                );
+}
+
+void NetworkClientPlayerBase::spawn (Vector position) {
+    (void) position;
+
+    throw Error("NetworkClientPlayerBase::spawn called");
+}
+
+void NetworkClientPlayerBase::die (bool start_timer) {
+    (void) start_timer;
+
+    throw Error("NetworkClientPlayerBase::die called");
+}
+
+void NetworkClientPlayerBase::respawn (TimeMS dt) {
+    (void) dt;
+
+    throw Error("NetworkClientPlayerBase::respawn called");
 }
 
 void NetworkClientPlayerBase::on_position (NetworkPacketInput &pkt) {
@@ -230,6 +250,25 @@
 
     rope.updateLength(length);
 }
+        
+void NetworkClientPlayerBase::on_spawn (NetworkPacketInput &pkt) {
+    // read packet
+    Vector position = pkt.read_vector();
+
+    Engine::log(INFO, "client_player.on_spawn") << this << ": position=" << position;
+    
+    // super
+    Player::spawn(position);
+}
+        
+void NetworkClientPlayerBase::on_die (NetworkPacketInput &pkt) {
+    (void) pkt;
+
+    Engine::log(INFO, "client_player.on_die") << this;
+
+    // super, but don't start the respawn_timer
+    Player::die(false);
+}
 
 /*
  * NetworkClientLocalPlayer
--- a/src/Network/Client.hh	Mon Dec 08 18:17:37 2008 +0000
+++ b/src/Network/Client.hh	Mon Dec 08 18:31:08 2008 +0000
@@ -145,6 +145,16 @@
          */
         NetworkClientPlayerBase (NetworkClient &client, NetworkObjectID obj_id, Vector position);
     
+    protected:
+        // @{
+        /**
+         * These should never be called directly, always via the network
+         */
+        virtual void spawn (Vector position);
+        virtual void die (bool start_timer = true);
+        virtual void respawn (TimeMS dt);
+        // @}
+
     private:
         /**
          * NETMSG_PLAYER_POSITION -> PhysicsObject::updatePhysics
@@ -180,6 +190,16 @@
          * NETMSG_PLAYER_ROPE_LENGTH -> Player::Rope.updateLength
          */
         void on_rope_length (NetworkPacketInput &pkt);
+
+        /**
+         * NETMSG_PLAYER_SPAWN -> Player::spawn
+         */
+        void on_spawn (NetworkPacketInput &pkt);
+
+        /**
+         * NETMSG_PLAYER_Die -> Player::die
+         */
+        void on_die (NetworkPacketInput &pkt);
 };
 
 /**
--- a/src/Network/Protocol.hh	Mon Dec 08 18:17:37 2008 +0000
+++ b/src/Network/Protocol.hh	Mon Dec 08 18:31:08 2008 +0000
@@ -78,6 +78,19 @@
     NETMSG_PLAYER_POSITION      = 0x0301,
 
     /*
+     * Player has spawned somewhere
+     *
+     * Vector   position
+     */
+    NETMSG_PLAYER_SPAWN         = 0x0302,
+
+    /*
+     * Player has died
+     *
+     */
+    NETMSG_PLAYER_DIE           = 0x0303,
+
+    /*
      * Terrain update, removeGround
      *
      * Vector   position 
--- a/src/Network/Server.cc	Mon Dec 08 18:17:37 2008 +0000
+++ b/src/Network/Server.cc	Mon Dec 08 18:31:08 2008 +0000
@@ -158,6 +158,33 @@
     send_all(NETMSG_PLAYER_ROPE_LENGTH, pkt, true);
 }
 
+void NetworkServerPlayer::spawn (Vector position) {
+    NetworkPacket pkt;
+    
+    // write packet
+    pkt.write_vector(position);
+
+    Engine::log(INFO, "server_player.spawn") << this << ": position=" << position;
+    
+    // send
+    send_all(NETMSG_PLAYER_SPAWN, pkt, true);
+    
+    // super
+    Player::spawn(position);
+}
+        
+void NetworkServerPlayer::die (bool start_timer) {
+    NetworkPacket pkt;
+
+    Engine::log(INFO, "server_player.die") << this;
+    
+    // send
+    send_all(NETMSG_PLAYER_DIE, pkt, true);
+
+    // super
+    Player::die(start_timer);
+}
+
 void NetworkServerPlayer::on_disconnected (void) {
     NetworkPacket pkt;
     
--- a/src/Network/Server.hh	Mon Dec 08 18:17:37 2008 +0000
+++ b/src/Network/Server.hh	Mon Dec 08 18:31:08 2008 +0000
@@ -113,6 +113,9 @@
         virtual void handleChangeWeapon (unsigned int weaponIndex);
         virtual void handleRopeState (RopeState state);
         virtual void handleRopeLength (float length);
+    
+        virtual void spawn (Vector position);
+        virtual void die (bool start_timer = true);
         // @}
         //
         
--- a/src/Player.cc	Mon Dec 08 18:17:37 2008 +0000
+++ b/src/Player.cc	Mon Dec 08 18:31:08 2008 +0000
@@ -68,12 +68,20 @@
     enable();
 }
 
-void Player::die (void) {
+void Player::die (bool start_timer) {
+    // we don't have a rope anymore
+    rope.release();
+
     // disable our PhysicsObject
     disable();
+
+    // XXX: PhysicsObject::reset
+    this->velocity = Vector(0, 0);
     
-    // start respawn timer
-    respawn_timer.fire_once();
+    if (start_timer) {
+        // start respawn timer
+        respawn_timer.fire_once();
+    }
 }
 
 void Player::respawn (TimeMS dt) {
@@ -82,6 +90,8 @@
     // reset health
     health = PLAYER_HEALTH;
 
+    // XXX: reload weapons
+
     // XXX: ...
     spawn(Vector(PLAYER_INITIAL_X, PLAYER_INITIAL_Y));
 }
--- a/src/Player.hh	Mon Dec 08 18:17:37 2008 +0000
+++ b/src/Player.hh	Mon Dec 08 18:31:08 2008 +0000
@@ -70,12 +70,12 @@
     /**
      * We die. Disable and prepare respawn_timer
      */
-    virtual void die (void);
+    virtual void die (bool start_timer = true);
 
     /**
      * Calculate a new position for the worm, and respawn there. Also set health back to 100%
      */
-    void respawn (TimeMS dt);
+    virtual void respawn (TimeMS dt);
 
     /**
      *  Used by the network code to execute various actions