New stay_group of SpriteChange objects ensures in_stay_action is synchronized, i...
authorChristoph Sommer <mail@christoph-sommer.de>
Wed, 21 Jun 2006 13:47:25 +0000 (13:47 +0000)
committerChristoph Sommer <mail@christoph-sommer.de>
Wed, 21 Jun 2006 13:47:25 +0000 (13:47 +0000)
SVN-Revision: 3686

data/levels/world1/worldmap.stwm
data/levels/world2/worldmap.stwm
src/worldmap/sprite_change.cpp
src/worldmap/sprite_change.hpp
src/worldmap/tux.cpp

index d63db50..7fe019e 100644 (file)
       (change-on-touch #t)
       (stay-action "")
       (initial-stay-action #f)
+      (stay-group "world2-boat")
       (x 38)
       (y 21)
       (sprite "images/worldmap/common/boat/boat.sprite")
       (change-on-touch #f)
       (stay-action "empty")
       (initial-stay-action #t)
+      (stay-group "world2-boat")
       (x 34)
       (y 15)
       (sprite "images/worldmap/common/boat/boat.sprite")
index 7c85b36..3633d6e 100644 (file)
     (sprite-change
       (change-on-touch #f)
       (stay-action "empty")
-      (initial-stay-action #f)
+      (initial-stay-action #t)
+      (stay-group "world1-boat")
       (x 19)
       (y 42)
       (sprite "images/worldmap/common/boat/boat.sprite")
       (change-on-touch #t)
       (stay-action "")
       (initial-stay-action #f)
+      (stay-group "world1-boat")
       (x 10)
       (y 45)
       (sprite "images/worldmap/common/boat/boat.sprite")
index 9ac536e..ef32fb2 100644 (file)
@@ -39,10 +39,15 @@ SpriteChange::SpriteChange(const lisp::Lisp* lisp)
 
   lisp->get("stay-action", stay_action);
   lisp->get("initial-stay-action", in_stay_action);
+
+  lisp->get("stay-group", stay_group);
+
+  all_sprite_changes.push_back(this);
 }
 
 SpriteChange::~SpriteChange()
 {
+  all_sprite_changes.remove(this);
 }
 
 void
@@ -59,4 +64,27 @@ SpriteChange::update(float )
 {
 }
 
+void
+SpriteChange::set_stay_action()
+{
+  in_stay_action = true;
+}
+
+void
+SpriteChange::clear_stay_action()
+{
+  in_stay_action = false;
+
+  // if we are in a stay_group, also clear all stay actions in this group
+  if (stay_group != "") {
+    for (std::list<SpriteChange*>::iterator i = all_sprite_changes.begin(); i != all_sprite_changes.end(); i++) {
+      SpriteChange* sc = *i;
+      if (sc->stay_group != stay_group) continue;
+      sc->in_stay_action = false;
+    }
+  }
+}
+
+std::list<SpriteChange*> SpriteChange::all_sprite_changes;
+
 }
index 7bf2f8c..b48787d 100644 (file)
@@ -21,6 +21,7 @@
 
 #include <string>
 #include <memory>
+#include <list>
 #include "game_object.hpp"
 #include "lisp/lisp.hpp"
 #include "math/vector.hpp"
@@ -50,13 +51,34 @@ public:
    * another SpriteChange object.
    */
   std::string stay_action;
+
   /**
-   * should the stayaction be displayed
+   * name of a group in which only one SpriteChange will ever have its stay_action displayed. 
+   * Leave empty if you don't care. 
    */
-  bool in_stay_action;
+  std::string stay_group; 
 
   virtual void draw(DrawingContext& context);
   virtual void update(float elapsed_time);
+
+  /**
+   * Activates the SpriteChange's stay action, if applicable
+   */
+  void set_stay_action();
+
+  /**
+   * Deactivates the SpriteChange's stay action, if applicable
+   */
+  void clear_stay_action();
+
+private:
+  /**
+   * should the stayaction be displayed
+   */
+  bool in_stay_action;
+
+  static std::list<SpriteChange*> all_sprite_changes;
+
 };
 
 }
index 0e51bda..7f5c50a 100644 (file)
@@ -170,7 +170,7 @@ Tux::tryContinueWalking(float elapsed_time)
   SpriteChange* sprite_change = worldmap->at_sprite_change(tile_pos);
   if(sprite_change != NULL) {
     sprite.reset(new Sprite( *(sprite_change->sprite.get()) ));
-    sprite_change->in_stay_action = false;
+    sprite_change->clear_stay_action();
   }
 
   // if this is a special_tile with passive_message, display it
@@ -259,12 +259,12 @@ Tux::tryContinueWalking(float elapsed_time)
   SpriteChange* next_sprite = worldmap->at_sprite_change(next_tile);
   if(next_sprite != NULL && next_sprite->change_on_touch) {
     sprite.reset(new Sprite( *(next_sprite->sprite.get()) ));
-    next_sprite->in_stay_action = false;
+    next_sprite->clear_stay_action();
   }
   SpriteChange* last_sprite = worldmap->at_sprite_change(tile_pos);
   if(last_sprite != NULL && next_sprite != NULL) {
     log_debug << "Old: " << tile_pos << " New: " << next_tile << std::endl;
-    last_sprite->in_stay_action = true;
+    last_sprite->set_stay_action();
   }
 
   tile_pos = next_tile;
@@ -300,7 +300,7 @@ Tux::setup()
   SpriteChange* sprite_change = worldmap->at_sprite_change(tile_pos);
   if(sprite_change != NULL) {
     sprite.reset(new Sprite( *(sprite_change->sprite.get()) ));
-    sprite_change->in_stay_action = false;
+    sprite_change->clear_stay_action();
   }
 }