add separate Types.hh, and fix projectile-worm collisions on network
authorterom
Mon, 08 Dec 2008 17:24:40 +0000
changeset 296 4d3ebaa29430
parent 295 4d3adfbec077
child 297 809a0f84d581
add separate Types.hh, and fix projectile-worm collisions on network
src/Network/Client.cc
src/Network/Client.hh
src/Network/Packet.hh
src/Network/Protocol.hh
src/Network/Server.cc
src/Network/Server.hh
src/Player.cc
src/Player.hh
src/Projectile.cc
src/Projectile.hh
src/Rope.cc
src/Terrain.cc
src/Terrain.hh
src/Types.hh
--- a/src/Network/Client.cc	Mon Dec 08 16:46:48 2008 +0000
+++ b/src/Network/Client.cc	Mon Dec 08 17:24:40 2008 +0000
@@ -285,6 +285,7 @@
 {
     // hook up signals
     slots.connect(sig_message(NETMSG_PROJECTILE_DESTROY), this, &NetworkClientProjectile::on_destroy);
+    slots.connect(sig_message(NETMSG_PROJECTILE_HIT_PLAYER), this, &NetworkClientProjectile::on_hit_player);
     
     // tell Player
     player->weaponFired(weapon);
@@ -296,6 +297,13 @@
 
     // ignore :>
 }
+        
+void NetworkClientProjectile::onHitPlayer (Player *player, Health damage) {
+    (void) player;
+    (void) damage;
+
+    // ignore :>
+}
 
 void NetworkClientProjectile::on_destroy (NetworkPacketInput &pkt) {
     Vector position = pkt.read_vector();
@@ -303,9 +311,26 @@
 
     Engine::log(INFO, "client_projectile.on_destroy") << this << ": position=" << position << ", flags=" << flags;
     
-    // XXX: leak obj, not yet implemented:  obj.destory();
-
     // pass on to super
     Projectile::onDestroy(position, flags & NETWORK_PROJECTILE_REMOVE_GROUND);
 }
 
+void NetworkClientProjectile::on_hit_player (NetworkPacketInput &pkt) {
+    // read packet
+    NetworkObject *player_obj = controller.read_object(pkt);
+    Health damage = pkt.read_uint16();
+
+    NetworkClientPlayerBase *player;
+
+    // ignore for invalid players
+    if ((player = dynamic_cast<NetworkClientPlayerBase*>(player_obj)) == NULL) {
+        Engine::log(ERROR, "client.on_hit_player") << this << ": Unknown player object";
+        return;
+    }
+    
+    Engine::log(INFO, "client_projectile.hit_player") << this << ": player=" << player << ", damage=" << damage;
+    
+    // pass on to super
+    Projectile::onHitPlayer(player, damage);
+}
+
--- a/src/Network/Client.hh	Mon Dec 08 16:46:48 2008 +0000
+++ b/src/Network/Client.hh	Mon Dec 08 17:24:40 2008 +0000
@@ -233,12 +233,23 @@
          * so that we can remove the ground reliably
          */
         virtual void onDestroy (Vector position, bool removeGround);
+        
+        /**
+         * Overrides Projectile::onHitPlayer to ignore this, as we must wait for the server to tell us if it really did
+         * happen.
+         */
+        virtual void onHitPlayer (Player *player, Health damage);
 
     private:
         /**
          * NETMSG_PROJECTILE_DESTROY -> Projectile::onDestory
          */
         void on_destroy (NetworkPacketInput &pkt);
+
+        /**
+         * NETMSG_PROJECTILE_HIT_PLAYER -> Projectile::onHitPlayer
+         */
+        void on_hit_player (NetworkPacketInput &pkt);
 };
 
 #endif
--- a/src/Network/Packet.hh	Mon Dec 08 16:46:48 2008 +0000
+++ b/src/Network/Packet.hh	Mon Dec 08 17:24:40 2008 +0000
@@ -35,12 +35,10 @@
          * @return T The value
          */
         template <typename T> T read_val (void);
-
+        
+        // @{
         /**
-         * @defgroup read_* Read methods
-         *  Collection of methods to write out simple types, these convert from network-endianness and return the value
-         *
-         * @{
+         * Collection of methods to write out simple types, these convert from network-endianness and return the value
          */
 
         /** 32-bit unsigned int */
@@ -64,9 +62,7 @@
         /** 32-bit float */
         float read_float32 (void);
 
-        /**
-         * @}
-         */
+        // @}
         
         /**
          * Read a Vector from the packet:
@@ -98,11 +94,9 @@
          */
         template <typename T> void write_val (const T &val);
         
+        // @{        
         /**
-         * @defgroup write* Write methods
-         *  Collection of methods to write out simple types, these convert the given value to network-byte-order
-         *
-         * @{
+         * Collection of methods to write out simple types, these convert the given value to network-byte-order
          */
         void write_uint32 (uint32_t val);
         void write_uint16 (uint16_t val);
@@ -112,9 +106,7 @@
         void write_int8 (int8_t val);
         void write_float32 (float val);
         
-        /**
-         * @}
-         */
+        // @{ 
 
         /**
          * Write a vector to the packet:
--- a/src/Network/Protocol.hh	Mon Dec 08 16:46:48 2008 +0000
+++ b/src/Network/Protocol.hh	Mon Dec 08 17:24:40 2008 +0000
@@ -132,6 +132,14 @@
      */
     NETMSG_PROJECTILE_PLAYER_FIRED  = 0x0411,
 
+    /**
+     * Projectile has hit a player
+     *
+     * Object   player
+     * uint16_t damage
+     */
+    NETMSG_PROJECTILE_HIT_PLAYER    = 0x0421,
+
     /*
      * Projectile has gone away
      *
--- a/src/Network/Server.cc	Mon Dec 08 16:46:48 2008 +0000
+++ b/src/Network/Server.cc	Mon Dec 08 17:24:40 2008 +0000
@@ -257,7 +257,7 @@
 void NetworkServerProjectile::onDestroy (Vector position, bool removeGround) {
     NetworkPacket pkt;
 
-    Engine::log(INFO, "server_projectile.destroy") << "position=" << position << ", removeGround=" << removeGround;
+    Engine::log(INFO, "server_projectile.destroy") << this << "position=" << position << ", removeGround=" << removeGround;
     
     pkt.write_vector(position);
     pkt.write_uint8(removeGround ? NETWORK_PROJECTILE_REMOVE_GROUND : 0);
@@ -270,3 +270,20 @@
     Projectile::onDestroy(position, removeGround);
 }
 
+void NetworkServerProjectile::onHitPlayer (Player *player_ptr, Health damage) {
+    NetworkPacket pkt;
+    NetworkServerPlayer *player = dynamic_cast<NetworkServerPlayer*>(player_ptr);
+
+    if (player == NULL) 
+        throw Error("NetworkServerProjectile::onHitPlayer called with non-NetworkServerPlayer player");
+    
+    Engine::log(INFO, "server_projectile.hit_player") << this << ": player=" << player << ", damage=" << damage;
+    
+    // write packet
+    controller.write_object(pkt, player);
+    pkt.write_uint16(damage);
+
+    // send
+    send_all(NETMSG_PROJECTILE_HIT_PLAYER, pkt, true);
+}
+
--- a/src/Network/Server.hh	Mon Dec 08 16:46:48 2008 +0000
+++ b/src/Network/Server.hh	Mon Dec 08 17:24:40 2008 +0000
@@ -166,6 +166,14 @@
          * @param removeGround controls the NETWORK_PROJECTILE_REMOVE_GROUND flag
          */
         virtual void onDestroy (Vector position, bool removeGround);
+        
+        /**
+         * Overriden from Projectile to send a NETMSG_PROJECTILE_HIT_PLAYER with the given player and damage
+         *
+         * @param player the NetworkServerPlayer that got hit
+         * @param damage the amount of damage inflicted
+         */
+        virtual void onHitPlayer (Player *player_ptr, Health damage);
 };
 
 #endif
--- a/src/Player.cc	Mon Dec 08 16:46:48 2008 +0000
+++ b/src/Player.cc	Mon Dec 08 17:24:40 2008 +0000
@@ -211,12 +211,14 @@
 }
 
 void Player::onCollision (Vector collisionPoint, PhysicsObject *other) {
+    (void) collisionPoint;
 
     if (other == NULL)
         return;
 
     // Currently we handle only player-player collision here.
     // Player-projectile collision is handled in projectile's onCollision.
+    // XXX: not network-safe
     if (other->getType() == PLAYER) {
         Vector normal = this->getPosition() - other->getPosition();
         this->bounce(normal);
@@ -226,12 +228,13 @@
     }
 }
 
-void Player::takeDamage(int damage) {
+void Player::takeDamage (Health damage) {
     this->health -= damage;
-    Engine::log(DEBUG, "Player.takeDamage") << "Your health is now: " << health;
-    if (this->health <= 0) {
+
+    if (this->health <= 0)
         this->disable();
-    }
+
+    Engine::log(DEBUG, "player.take_damage") << this << ": damage=" << damage << ", health=" << health;
 }
 
 void Player::draw (Graphics *g, PixelCoordinate camera) {
--- a/src/Player.hh	Mon Dec 08 16:46:48 2008 +0000
+++ b/src/Player.hh	Mon Dec 08 17:24:40 2008 +0000
@@ -11,6 +11,7 @@
 #include "Input.hh"
 #include "Rope.hh"
 #include "GraphicsPointer.hh"
+#include "Types.hh"
 #include <vector>
 
 /**
@@ -35,8 +36,9 @@
         
     // we have a rope
     Rope rope;
-
-    int health;
+    
+    /** Our current health */
+    Health health;
 
     // XXX: hmm... updated where?
     int animation_step;
@@ -102,13 +104,20 @@
      */
     void onCollision (Vector collisionPoint, PhysicsObject *other);
 
+    /**
+     * We have been hit by something, and therefore take some damage.
+     *
+     * XXX: should this take the Projectile instead or somesuch?
+     */
+    void takeDamage (Health damage);
+
     /*
      * Drawing requires the skin texture, which is loaded on-demand when draw is called
      */
     static bool skin_loaded;
     static CL_Surface skin_surface;
     virtual void draw (Graphics *g, PixelCoordinate camera);
-    void takeDamage(int damage);
+
 };
 
 /**
--- a/src/Projectile.cc	Mon Dec 08 16:46:48 2008 +0000
+++ b/src/Projectile.cc	Mon Dec 08 17:24:40 2008 +0000
@@ -38,19 +38,23 @@
 
     destroy();
 }
+    
+void Projectile::onHitPlayer (Player *player, Health damage) {
+    player->takeDamage(damage);
+}
 
 void Projectile::onCollision (Vector collisionPoint, PhysicsObject *other) {
-//    (void) other;
+    // did we hit a player?
+    if (other != NULL && other->getType() == PLAYER) {
+        Player* player = dynamic_cast<Player*>(other);
 
-    if(other != NULL && other->getType() == PLAYER) {
-        Player* pl = dynamic_cast<Player*>(other);
-        pl->takeDamage(10);
+        // XXX: check that player really is !NULL
+        onHitPlayer(player, 10);
     }
 
     if (collision_elasticity == 0)
         onDestroy(collisionPoint, true);
 }
-
    
 void Projectile::tick (TimeMS dt) {
     // expire projectiles
--- a/src/Projectile.hh	Mon Dec 08 16:46:48 2008 +0000
+++ b/src/Projectile.hh	Mon Dec 08 17:24:40 2008 +0000
@@ -4,8 +4,10 @@
 class Projectile;
 
 #include "GameState.hh"
+#include "Player.hh"
 #include "PhysicsObject.hh"
 #include "Timer.hh"
+#include "Types.hh"
 #include "GraphicsPointer.hh"
 
 /**
@@ -69,6 +71,14 @@
      * This is overriden by Network.
      */
     virtual void onDestroy (Vector position, bool removeGround);
+
+    /**
+     * This Projectile has hit the given player.
+     *
+     * This Projectile inflicts a certain amount of damage on the player. We are not destroyed, this can be called
+     * multiple times...
+     */
+    virtual void onHitPlayer (Player *player, Health damage);
     
     /**
      * Call onDestroy, removingGround
--- a/src/Rope.cc	Mon Dec 08 16:46:48 2008 +0000
+++ b/src/Rope.cc	Mon Dec 08 17:24:40 2008 +0000
@@ -6,7 +6,7 @@
 #include <stdexcept>
 
 Rope::Rope(Player &player) : 
-    PhysicsObject(player.state.world, ROPE_MASS, Vector(0,0), Vector(0,0), ROPE, false), player(player), state(ROPE_FOLDED) 
+    PhysicsObject(player.state.world, ROPE_MASS, Vector(0,0), Vector(0,0), ROPE, 0.00, false), player(player), state(ROPE_FOLDED) 
 {
     // XXX: better shape
     std::vector<Vector> shape(4);
--- a/src/Terrain.cc	Mon Dec 08 16:46:48 2008 +0000
+++ b/src/Terrain.cc	Mon Dec 08 17:24:40 2008 +0000
@@ -7,6 +7,17 @@
 #include <algorithm>
 #include <ClanLib/display.h>
 
+const Vector DIRECTIONS[] = {
+    Vector(0,-1),
+    Vector(1,-1),
+    Vector(1,0),
+    Vector(1,1),
+    Vector(0,1),
+    Vector(-1,1),
+    Vector(-1,0),
+    Vector(-1,-1)
+};
+
 Terrain::Terrain (void) :
     map_width(0), map_height(0)
 {
--- a/src/Terrain.hh	Mon Dec 08 16:46:48 2008 +0000
+++ b/src/Terrain.hh	Mon Dec 08 17:24:40 2008 +0000
@@ -5,6 +5,7 @@
 
 #include "Vector.hh"
 #include "GraphicsPointer.hh"
+#include "Types.hh"
 #include "Config.hh"
 
 enum TerrainType {
@@ -13,20 +14,6 @@
     TERRAIN_ROCK
 };
 
-const Vector DIRECTIONS[] = {
-    Vector(0,-1),
-    Vector(1,-1),
-    Vector(1,0),
-    Vector(1,1),
-    Vector(0,1),
-    Vector(-1,1),
-    Vector(-1,0),
-    Vector(-1,-1)
-};
-
-typedef long int PixelDimension;
-typedef _Vector<PixelDimension> PixelCoordinate;
-
 /**
  * Terrain class. Represents game terrain and contains member
  * functions to manipulate terrain and get info about it.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Types.hh	Mon Dec 08 17:24:40 2008 +0000
@@ -0,0 +1,26 @@
+#ifndef TYPES_HH
+#define TYPES_HH
+
+/**
+ * Define various types
+ */
+
+#include <stdint.h>
+
+/**
+ * A player's health is measured in...
+ */
+typedef int16_t Health;
+
+/**
+ * Terrain/Graphics pixel dimension values are long ints
+ */
+typedef long int PixelDimension;
+
+/**
+ * A Terrain/Pixel coordinate as a PixelDimension Vector
+ */
+typedef _Vector<PixelDimension> PixelCoordinate;
+
+
+#endif