-renamed ViewPort to Camera
[supertux.git] / src / badguy.cpp
index 57f147a..0e4b7de 100644 (file)
@@ -35,6 +35,7 @@
 #include "gameloop.h"
 #include "display_manager.h"
 #include "lispwriter.h"
+#include "camera.h"
 
 Sprite* img_mriceblock_flat_left;
 Sprite* img_mriceblock_flat_right;
@@ -156,8 +157,6 @@ BadGuy::BadGuy(DisplayManager& display_manager, BadGuyKind kind_,
   lispreader.read_float("y", &base.y);
   base.width  = 0;
   base.height = 0;
-  base.xm  = 0;
-  base.ym  = 0;
 
   kind     = kind_;
 
@@ -176,8 +175,6 @@ BadGuy::BadGuy(DisplayManager& display_manager, BadGuyKind kind_,
   base.y = y;
   base.width  = 0;
   base.height = 0;
-  base.xm  = 0;
-  base.ym  = 0;
   stay_on_platform = false;
 
   kind     = kind_;
@@ -216,7 +213,7 @@ BadGuy::init()
     // hack so that the bomb doesn't hurt until it expldes...
     dying = DYING_SQUISHED;
   } else if(kind == BAD_FLAME) {
-    base.ym = 0; // we misuse base.ym as angle for the flame
+    angle = 0;
     physic.enable_gravity(false);
     set_sprite(img_flame, img_flame);
   } else if(kind == BAD_BOUNCINGSNOWBALL) {
@@ -252,13 +249,13 @@ BadGuy::init()
 void
 BadGuy::write(LispWriter& writer)
 {
-  writer.startList(badguykind_to_string(kind));
+  writer.start_list(badguykind_to_string(kind));
 
-  writer.writeFloat("x", base.x);
-  writer.writeFloat("y", base.y);
-  writer.writeBool("stay-on-platform", stay_on_platform);  
+  writer.write_float("x", base.x);
+  writer.write_float("y", base.y);
+  writer.write_bool("stay-on-platform", stay_on_platform);  
 
-  writer.endList(badguykind_to_string(kind));
+  writer.end_list(badguykind_to_string(kind));
 }
 
 void
@@ -319,6 +316,8 @@ BadGuy::action_mriceblock(double elapsed_time)
       check_horizontal_bump();
       if(mode == KICK && changed != dir)
         {
+          float scroll_x = World::current()->camera->get_translation().x;
+          
           /* handle stereo sound (number 10 should be tweaked...)*/
           if (base.x < scroll_x + screen->w/2 - 10)
             play_sound(sounds[SND_RICOCHET], SOUND_LEFT_SPEAKER);
@@ -518,6 +517,8 @@ BadGuy::action_bomb(double elapsed_time)
       dying = DYING_NOT; // now the bomb hurts
       timer.start(EXPLODETIME);
 
+      float scroll_x = World::current()->camera->get_translation().x;                 
+
       /* play explosion sound */  // FIXME: is the stereo all right? maybe we should use player cordinates...
       if (base.x < scroll_x + screen->w/2 - 10)
         play_sound(sounds[SND_EXPLODE], SOUND_LEFT_SPEAKER);
@@ -584,10 +585,10 @@ BadGuy::action_flame(double elapsed_time)
 {
     static const float radius = 100;
     static const float speed = 0.02;
-    base.x = old_base.x + cos(base.ym) * radius;
-    base.y = old_base.y + sin(base.ym) * radius;
+    base.x = old_base.x + cos(angle) * radius;
+    base.y = old_base.y + sin(angle) * radius;
 
-    base.ym = fmodf(base.ym + elapsed_time * speed, 2*M_PI);
+    angle = fmodf(angle + elapsed_time * speed, 2*M_PI);
 }
 
 void
@@ -750,13 +751,9 @@ BadGuy::action_snowball(double elapsed_time)
 void
 BadGuy::action(float elapsed_time)
 {
-  // Remove if it's far off the screen:
-  if (base.x < scroll_x - OFFSCREEN_DISTANCE)
-    {
-      remove_me();                                                
-      return;
-    }
-
+  float scroll_x = World::current()->camera->get_translation().x;
+  float scroll_y = World::current()->camera->get_translation().y;
+  
   // BadGuy fall below the ground
   if (base.y > World::current()->get_level()->height * 32) {
     remove_me();
@@ -775,7 +772,10 @@ BadGuy::action(float elapsed_time)
       }
 
   // Once it's on screen, it's activated!
-  if (base.x <= scroll_x + screen->w + OFFSCREEN_DISTANCE)
+  if (base.x > scroll_x - X_OFFSCREEN_DISTANCE &&
+       base.x < scroll_x + screen->w + X_OFFSCREEN_DISTANCE &&
+       base.y > scroll_y - Y_OFFSCREEN_DISTANCE &&
+       base.y < scroll_y + screen->h + Y_OFFSCREEN_DISTANCE)
     seen = true;
 
   if(!seen)
@@ -832,7 +832,7 @@ BadGuy::action(float elapsed_time)
 }
 
 void
-BadGuy::draw(ViewPort& viewport, int)
+BadGuy::draw(Camera& viewport, int)
 {
   float scroll_x = viewport.get_translation().x;
   float scroll_y = viewport.get_translation().y;
@@ -847,7 +847,7 @@ BadGuy::draw(ViewPort& viewport, int)
     }
 
   Sprite* sprite = (dir == LEFT) ? sprite_left : sprite_right;
-  sprite->draw(base.x, base.y);
+  sprite->draw(viewport.world2screen(Vector(base.x, base.y)));
 
   if (debug_mode)
     fillrect(base.x - scroll_x, base.y - scroll_y, base.width, base.height, 75,0,75, 150);