willowisp position relative to ghosttree now, ghosttree is notified from vanished...
authorMatthias Braun <matze@braunis.de>
Sat, 26 May 2007 15:41:46 +0000 (15:41 +0000)
committerMatthias Braun <matze@braunis.de>
Sat, 26 May 2007 15:41:46 +0000 (15:41 +0000)
SVN-Revision: 5025

src/badguy/ghosttree.cpp
src/badguy/ghosttree.hpp
src/badguy/root.cpp
src/badguy/treewillowisp.cpp
src/badguy/treewillowisp.hpp

index 56ca1b3..5fd1fcb 100644 (file)
 #include "treewillowisp.hpp"
 #include "root.hpp"
 
-static const int WILLOWISP_COUNT = 10;
+static const size_t WILLOWISP_COUNT = 10;
 
 GhostTree::GhostTree(const lisp::Lisp& lisp)
   : BadGuy(lisp, "images/creatures/ghosttree/ghosttree.sprite",
            LAYER_OBJECTS - 10),
-    willowisp_counter(0), willo_spawn_y(0), willo_radius(200),
-    willo_speed(1.8f), willo_color(0), treecolor(0)
+    willo_spawn_y(0), willo_radius(200), willo_speed(1.8f), willo_color(0),
+    treecolor(0)
 {
 }
 
@@ -67,14 +67,13 @@ GhostTree::active_update(float elapsed_time)
   }
 
   if(willowisp_timer.check()) {
-    if(willowisp_counter < WILLOWISP_COUNT) {
-      Vector pos = get_bbox().get_middle();
-      pos.y  += willo_spawn_y;
+    if(willowisps.size() < WILLOWISP_COUNT) {
+      Vector pos = Vector(bbox.get_width() / 2, bbox.get_height() / 2 + willo_spawn_y);
       TreeWillOWisp *willowisp 
-        = new TreeWillOWisp(pos, 200 + willo_radius, willo_speed);
+        = new TreeWillOWisp(this, pos, 200 + willo_radius, willo_speed);
 
       Sector::current()->add_object(willowisp);
-      willowisp_counter++;
+      willowisps.push_back(willowisp);
 
       willo_spawn_y -= 40;
       if(willo_spawn_y < -160)
@@ -114,9 +113,9 @@ GhostTree::active_update(float elapsed_time)
 }
 
 void
-GhostTree::willowisp_died()
+GhostTree::willowisp_died(TreeWillOWisp *willowisp)
 {
-  willowisp_counter--;
+  willowisps.erase(std::find(willowisps.begin(), willowisps.end(), willowisp));
 }
 
 void
index c52b6a0..df07788 100644 (file)
 #ifndef __GHOSTTREE_H__
 #define __GHOSTTREE_H__
 
+#include <vector>
 #include "badguy.hpp"
 
+class TreeWillOWisp;
+
 class GhostTree : public BadGuy
 {
 public:
@@ -29,11 +32,10 @@ public:
 
   void activate();
   void active_update(float elapsed_time);
-  void willowisp_died();
+  void willowisp_died(TreeWillOWisp *willowisp);
   void start_sucking();
 
 private:
-  int willowisp_counter;
   Timer willowisp_timer;
   float willo_spawn_y;
   float willo_radius;
@@ -43,6 +45,8 @@ private:
   Timer colorchange_timer;
   Timer root_timer;
   int   treecolor;
+
+  std::vector<TreeWillOWisp*> willowisps;
 };
 
 #endif
index 77a220a..4b94051 100644 (file)
@@ -38,7 +38,6 @@ void
 Root::activate()
 {
   set_pos(start_position + Vector(0, ypos));
-  std::cout << "SetPos: " << get_pos() << "\n";
 }
 
 void
@@ -53,6 +52,5 @@ Root::active_update(float elapsed_time)
     remove_me();
   }
   set_pos(start_position + Vector(0, ypos));
-  std::cout << "SetPos: " << get_pos() << "\n";
 }
 
index 9ec0897..c3d14e0 100644 (file)
 #include <config.h>
 
 #include "treewillowisp.hpp"
+#include "ghosttree.hpp"
 #include "object/lantern.hpp"
 
 static const std::string SOUNDFILE = "sounds/willowisp.wav";
 
-TreeWillOWisp::TreeWillOWisp(const Vector& pos, float radius, float speed)
-  : BadGuy(pos, "images/creatures/willowisp/willowisp.sprite",
-           LAYER_OBJECTS - 20), mystate(STATE_DEFAULT)
+TreeWillOWisp::TreeWillOWisp(GhostTree* tree, const Vector& pos,
+                             float radius, float speed)
+  : BadGuy(Vector(0, 0), "images/creatures/willowisp/willowisp.sprite",
+           LAYER_OBJECTS - 20), mystate(STATE_DEFAULT), tree(tree)
 {
+  treepos_delta = pos;
   sound_manager->preload(SOUNDFILE);
 
   this->radius = radius;
   this->angle  = 0;
   this->speed  = speed;
+  start_position = tree->get_pos() + treepos_delta;
 }
 
 TreeWillOWisp::~TreeWillOWisp()
@@ -58,6 +62,8 @@ TreeWillOWisp::vanish()
   mystate = STATE_VANISHING;
   sprite->set_action("vanishing", 1);
   set_group(COLGROUP_DISABLED);
+
+  tree->willowisp_died(this);
 }
 
 HitResponse
@@ -70,15 +76,17 @@ TreeWillOWisp::collision_player(Player& player, const CollisionHit& hit)
 bool
 TreeWillOWisp::collides(GameObject& other, const CollisionHit& ) {
   Lantern* lantern = dynamic_cast<Lantern*>(&other);
-  if (lantern && lantern->is_open()) return true;
-  if (dynamic_cast<Player*>(&other)) return true;
+  if (lantern && lantern->is_open())
+    return true;
+  if (dynamic_cast<Player*>(&other))
+    return true;
+  
   return false;
 }
 
 void
 TreeWillOWisp::active_update(float elapsed_time)
 {
-
   // remove TreeWillOWisp if it has completely vanished
   if (mystate == STATE_VANISHING) {
     if(sprite->animation_done()) {
@@ -88,7 +96,7 @@ TreeWillOWisp::active_update(float elapsed_time)
   }
 
   angle = fmodf(angle + elapsed_time * speed, (float) (2*M_PI));
-  Vector newpos(start_position.x + sin(angle) * radius, start_position.y);
+  Vector newpos(tree->get_pos() + treepos_delta + Vector(sin(angle) * radius, 0));
   movement = newpos - get_pos();
   float sizemod = cos(angle) * 0.8f;
   /* TODO: modify sprite size */
index 49b8d07..66215ab 100644 (file)
 
 #include "badguy.hpp"
 
+class GhostTree;
+
 class TreeWillOWisp : public BadGuy
 {
 public:
-  TreeWillOWisp(const Vector& pos, float radius, float speed);
+  TreeWillOWisp(GhostTree* tree, const Vector& pos, float radius, float speed);
   virtual ~TreeWillOWisp();
 
   void activate();
@@ -60,6 +62,8 @@ private:
   float speed;
 
   std::auto_ptr<SoundSource> sound_source;
+  Vector     treepos_delta;
+  GhostTree* tree;
 };
 
 #endif