Animated eyes of icecrusher to follow Tux
authorLMH <lmh.0013@gmail.com>
Sun, 28 Jul 2013 20:11:31 +0000 (10:11 -1000)
committerLMH <lmh.0013@gmail.com>
Sun, 28 Jul 2013 20:11:31 +0000 (10:11 -1000)
data/images/creatures/icecrusher/iceblock.xcf [new file with mode: 0644]
data/images/creatures/icecrusher/iceblock_lefteye.png [new file with mode: 0644]
data/images/creatures/icecrusher/iceblock_main.png [new file with mode: 0644]
data/images/creatures/icecrusher/iceblock_righteye.png [new file with mode: 0644]
data/images/creatures/icecrusher/iceblock_whites.png [new file with mode: 0644]
data/images/creatures/icecrusher/icecrusher.sprite
src/object/icecrusher.cpp
src/object/icecrusher.hpp

diff --git a/data/images/creatures/icecrusher/iceblock.xcf b/data/images/creatures/icecrusher/iceblock.xcf
new file mode 100644 (file)
index 0000000..2183ca2
Binary files /dev/null and b/data/images/creatures/icecrusher/iceblock.xcf differ
diff --git a/data/images/creatures/icecrusher/iceblock_lefteye.png b/data/images/creatures/icecrusher/iceblock_lefteye.png
new file mode 100644 (file)
index 0000000..017978f
Binary files /dev/null and b/data/images/creatures/icecrusher/iceblock_lefteye.png differ
diff --git a/data/images/creatures/icecrusher/iceblock_main.png b/data/images/creatures/icecrusher/iceblock_main.png
new file mode 100644 (file)
index 0000000..d6c9b3e
Binary files /dev/null and b/data/images/creatures/icecrusher/iceblock_main.png differ
diff --git a/data/images/creatures/icecrusher/iceblock_righteye.png b/data/images/creatures/icecrusher/iceblock_righteye.png
new file mode 100644 (file)
index 0000000..eabeca9
Binary files /dev/null and b/data/images/creatures/icecrusher/iceblock_righteye.png differ
diff --git a/data/images/creatures/icecrusher/iceblock_whites.png b/data/images/creatures/icecrusher/iceblock_whites.png
new file mode 100644 (file)
index 0000000..c7b1fd0
Binary files /dev/null and b/data/images/creatures/icecrusher/iceblock_whites.png differ
index 0727f85..1709b5c 100644 (file)
@@ -2,7 +2,7 @@
  (action
   (name "idle")
   (hitbox 11 8 64 64)
-  (images "iceblock.png"
+  (images "iceblock_main.png"
   )
  )
  (action
  (action
   (name "recovering")
   (hitbox 11 8 64 64)
-  (images "iceblock.png"
+  (images "iceblock_main.png"
   )
  )
+ (action
+  (name "whites")
+  (hitbox 11 8 64 64)
+  (images "iceblock_whites.png")
+ )
+ (action
+  (name "lefteye")
+  (hitbox 11 8 64 64)
+  (images "iceblock_lefteye.png")
+ )
+ (action
+  (name "righteye")
+  (hitbox 11 8 64 64)
+  (images "iceblock_righteye.png")
+ )
 )
index be68a65..e97fd62 100644 (file)
 
 #include "object/icecrusher.hpp"
 
+#include <math.h>
+
 #include "audio/sound_manager.hpp"
 #include "badguy/badguy.hpp"
-//#include "math/random_generator.hpp"
 #include "object/camera.hpp"
 #include "object/particles.hpp"
 #include "object/player.hpp"
 #include "sprite/sprite.hpp"
+#include "sprite/sprite_manager.hpp"
 #include "supertux/object_factory.hpp"
 #include "supertux/sector.hpp"
 
@@ -38,11 +40,14 @@ const float PAUSE_TIME_LARGE  = 1.0;
 }
 
 IceCrusher::IceCrusher(const Reader& reader) :
-  MovingSprite(reader, "images/creatures/icecrusher/icecrusher.sprite", LAYER_OBJECTS, COLGROUP_STATIC), 
+  MovingSprite(reader, "images/creatures/icecrusher/icecrusher.sprite", LAYER_OBJECTS, COLGROUP_STATIC),
   state(IDLE), 
   start_position(),
   physic(),
   cooldown_timer(0.0),
+  lefteye(),
+  righteye(),
+  whites(),
   ic_size(NORMAL)
 {
   // TODO: icecrusher hitting deserves its own sounds-
@@ -55,6 +60,13 @@ IceCrusher::IceCrusher(const Reader& reader) :
   float sprite_width = sprite->get_width ();
   if (sprite_width >= 128.0)
     ic_size = LARGE;
+
+  lefteye = sprite_manager->create(sprite_name);
+  lefteye->set_action("lefteye");
+  righteye = sprite_manager->create(sprite_name);
+  righteye->set_action("righteye");
+  whites = sprite_manager->create(sprite_name);
+  whites->set_action("whites");
 }
 
 /*
@@ -221,6 +233,23 @@ IceCrusher::update(float elapsed_time)
   }
 }
 
+void
+IceCrusher::draw(DrawingContext& context)
+{
+  context.push_target();
+  context.set_target(DrawingContext::NORMAL);
+  sprite->draw(context, get_pos(), layer);
+  if(!(state == CRUSHING)) // Remove if eyes are to be animated during crushing
+  {
+    // draw icecrusher's eyes slightly behind
+    lefteye->draw(context, get_pos()+eye_position(false), layer-1);
+    righteye->draw(context, get_pos()+eye_position(true), layer-1);
+    // draw the whites of icecrusher's eyes even further behind
+    whites->draw(context, get_pos(), layer-2);
+  }
+  context.pop_target();
+}
+
 bool
 IceCrusher::found_victim()
 {
@@ -237,4 +266,32 @@ IceCrusher::found_victim()
     return false;
 }
 
+Vector
+IceCrusher::eye_position(bool right)
+{
+  if(!(state == CRUSHING))
+  {
+    Player* player = Sector::current()->get_nearest_player (this->get_bbox ());
+    if(player)
+    {
+      // Icecrusher focuses on approximate position of player's head
+      const float player_focus_x = (player->get_bbox().p2.x + player->get_bbox().p1.x) * 0.5;
+      const float player_focus_y = player->get_bbox().p2.y * 0.25 + player->get_bbox().p1.y * 0.75;
+      // Icecrusher's approximate origin of line-of-sight
+      const float crusher_origin_x = (get_bbox().p2.x + get_bbox().p1.x) * 0.5;
+      const float crusher_origin_y = (get_bbox().p2.y + get_bbox().p1.y) * 0.5;
+      // Line-of-sight displacement from icecrusher to player
+      const float displacement_x = player_focus_x - crusher_origin_x;
+      const float displacement_y = player_focus_y - crusher_origin_y;
+      const float displacement_mag = pow(pow(displacement_x, 2.0) + pow(displacement_y, 2.0), 0.5);
+      // Determine weighting for eye displacement along x given icecrusher eye shape
+      int weight = ((displacement_x > 0) == right) ? 1 : 4;
+
+      return Vector(displacement_x/displacement_mag * weight, displacement_y/displacement_mag * 2 - 2);
+    }
+  }
+
+  return Vector(0,0);
+}
+
 /* EOF */
index 224ee79..1b38448 100644 (file)
@@ -34,6 +34,7 @@ public:
   virtual HitResponse collision(GameObject& other, const CollisionHit& hit);
   virtual void collision_solid(const CollisionHit& hit);
   virtual void update(float elapsed_time);
+  virtual void draw(DrawingContext& context);
 
 #if 0
   const Vector& get_speed() const
@@ -56,6 +57,11 @@ protected:
   Player* get_nearest_player();
   bool found_victim();
   void set_state(IceCrusherState state, bool force = false);
+  Vector eye_position(bool right);
+
+  SpritePtr lefteye;
+  SpritePtr righteye;
+  SpritePtr whites;
 
 private:
   enum IceCrusherSize {