fixed logger, and network projectiles should work reasonably well now
authorterom
Sat, 06 Dec 2008 19:14:58 +0000
changeset 224 e6faefba2ec1
parent 223 2fcaf54ed37b
child 225 22ecb9cb9245
fixed logger, and network projectiles should work reasonably well now
src/Logger.hh
src/Network/Client.cc
src/Network/Client.hh
src/Network/Protocol.hh
src/Network/Server.cc
src/Network/Server.hh
src/Projectile.cc
src/Projectile.hh
--- a/src/Logger.hh	Sat Dec 06 18:49:51 2008 +0000
+++ b/src/Logger.hh	Sat Dec 06 19:14:58 2008 +0000
@@ -22,13 +22,12 @@
     public:
         Logger (std::ostream &stream, enum LogLevel level, const char *module);
 
-        template <typename T> Logger& operator<< (T &val) {
+        template <typename T> Logger& operator<< (const T val) {
             stream << val;
 
             return *this;
         }
 
-
         ~Logger (void);
 };
 
--- a/src/Network/Client.cc	Sat Dec 06 18:49:51 2008 +0000
+++ b/src/Network/Client.cc	Sat Dec 06 19:14:58 2008 +0000
@@ -181,6 +181,22 @@
         Vector velocity, float explosionRadius) :
     NetworkClientObjectHelper(client, obj), Projectile(client.state, position, velocity, true, explosionRadius)
 {
-    // XXX: register messages
+    // hook up signals
+    slots.connect(obj->sig_message(NETMSG_PROJECTILE_DESTROY), this, &NetworkClientProjectile::on_destroy);
 }
 
+void NetworkClientProjectile::onDestroy (Vector position, bool removeGround) {
+    // ignore :>
+}
+
+void NetworkClientProjectile::on_destroy (NetworkPacketInput &pkt) {
+    Vector position = pkt.read_vector();
+    uint8_t flags = pkt.read_uint8();
+
+    Engine::log(INFO, "client_projectile.on_destroy") << "obj=" << obj << ", position=" << position << ", flags=" << flags;
+    
+    // XXX: leak obj, not yet implemented:  obj.destory();
+
+    // pass on to super
+    Projectile::onDestroy(position, flags & NETWORK_PROJECTILE_REMOVE_GROUND);
+}
--- a/src/Network/Client.hh	Sat Dec 06 18:49:51 2008 +0000
+++ b/src/Network/Client.hh	Sat Dec 06 19:14:58 2008 +0000
@@ -81,6 +81,12 @@
     public:
         NetworkClientProjectile (NetworkClient &client, NetworkObject_Client *obj, Vector position, Vector velocity,
                 float explosionRadius);
+    
+    protected:
+        virtual void onDestroy (Vector position, bool removeGround);
+
+    private:
+        void on_destroy (NetworkPacketInput &pkt);
 };
 
 #endif
--- a/src/Network/Protocol.hh	Sat Dec 06 18:49:51 2008 +0000
+++ b/src/Network/Protocol.hh	Sat Dec 06 19:14:58 2008 +0000
@@ -22,8 +22,12 @@
 };
 
 enum NetworkPhysicsFlags {
-    NETWORK_PHYSICS_INAIR      = 0x01,
-    NETWORK_PHYSICS_FACE_RIGHT = 0x02,
+    NETWORK_PHYSICS_INAIR               = 0x01,
+    NETWORK_PHYSICS_FACE_RIGHT          = 0x02,
+};
+
+enum NetworkProjectileFlags {
+    NETWORK_PROJECTILE_REMOVE_GROUND    = 0x01,
 };
 
 enum NetworkMessage {
@@ -89,6 +93,14 @@
      * float    explosionRadius
      */
     NETMSG_PROJECTILE_CREATE    = 0x0401,
+
+    /*
+     * Projectile has gone away
+     *
+     * Vector   position
+     * uint8_t  NetworkProjectileFlags (REMOVE_GROUND)
+     */
+    NETMSG_PROJECTILE_DESTROY   = 0x0402,
 };
 
 #endif
--- a/src/Network/Server.cc	Sat Dec 06 18:49:51 2008 +0000
+++ b/src/Network/Server.cc	Sat Dec 06 19:14:58 2008 +0000
@@ -187,3 +187,19 @@
     obj.send_all(NETMSG_PROJECTILE_CREATE, pkt, true);
 }
 
+void NetworkServerProjectile::onDestroy (Vector position, bool removeGround) {
+    NetworkPacket pkt;
+
+    Engine::log(INFO, "server_projectile.destroy") << "position=" << position << ", removeGround=" << removeGround;
+
+    pkt.write_vector(position);
+    pkt.write_uint8(removeGround ? NETWORK_PROJECTILE_REMOVE_GROUND : 0);
+
+    obj.send_all(NETMSG_PROJECTILE_DESTROY, pkt, true);
+
+    // XXX: leak obj, not yet implemented:  obj.destory();
+    
+    // pass on to super
+    Projectile::onDestroy(position, removeGround);
+}
+
--- a/src/Network/Server.hh	Sat Dec 06 18:49:51 2008 +0000
+++ b/src/Network/Server.hh	Sat Dec 06 19:14:58 2008 +0000
@@ -64,6 +64,9 @@
     
     public:
         NetworkServerProjectile (NetworkServer &server, Vector position, Vector velocity, float explosionRadius, TickCount age);
+    
+    protected:
+        virtual void onDestroy (Vector position, bool removeGround);
 };
 
 #endif
--- a/src/Projectile.cc	Sat Dec 06 18:49:51 2008 +0000
+++ b/src/Projectile.cc	Sat Dec 06 19:14:58 2008 +0000
@@ -21,10 +21,25 @@
     state.projectiles.remove(this);
 }
  
+void Projectile::onDestroy (Vector position, bool removeGround) {
+    if (removeGround)
+        world.removeGround(position, radius);
+
+    destroy();
+}
+
 void Projectile::onCollision() {
-    world.removeGround(position, radius);
-    
-    destroy();
+    onDestroy(position, true);
+}
+
+   
+void Projectile::tick (TimeMS dt) {
+    // expire projectiles
+    if (world.tick_timer.get_ticks() > birth_tick + age)
+        onDestroy(position, false);
+
+    // super
+    PhysicsObject::tick(dt);
 }
 
 void Projectile::draw(CL_GraphicContext *gc) const {
@@ -42,7 +57,7 @@
         const uint16_t chlen = 10;
         uint16_t x = projectile.center().x;
         uint16_t y = projectile.center().y;
-        if(target_visible) {
+        if (target_visible) {
             if (facingRight) {
                 gc->draw_line(x, y,
                               x + std::cos(aim)*chlen,
@@ -57,13 +72,4 @@
         }
     }
 }
-    
-void Projectile::tick (TimeMS dt) {
-    // expire projectiles
-    if (world.tick_timer.get_ticks() > birth_tick + age)
-        destroy();
-
-    // super
-    PhysicsObject::tick(dt);
-}
-
+ 
--- a/src/Projectile.hh	Sat Dec 06 18:49:51 2008 +0000
+++ b/src/Projectile.hh	Sat Dec 06 19:14:58 2008 +0000
@@ -24,7 +24,21 @@
     virtual void draw (CL_GraphicContext *gc) const;
 
 protected:
+    /*
+     * Removes ground at given position if applicable, and destroys this PhysicsObject.
+     *
+     * This is overriden by Network.
+     */
+    virtual void onDestroy (Vector position, bool removeGround);
+    
+    /*
+     * Call onDestroy, removingGround
+     */
     virtual void onCollision (void);
+
+    /*
+     * If we have expired, call onDestory without removingGround
+     */
     virtual void tick (TimeMS dt);
 };