Fix issue with action not being updated when typing grow()/fire()/etc in console...
[supertux.git] / src / object / gameobjs.cpp
index 3201eb2..a0276ed 100644 (file)
@@ -1,8 +1,7 @@
 //  $Id$
-// 
+//
 //  SuperTux
-//  Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
-//  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
+//  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
 //
 //  This program is free software; you can redistribute it and/or
 //  modify it under the terms of the GNU General Public License
 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 //  GNU General Public License for more details.
-// 
+//
 //  You should have received a copy of the GNU General Public License
 //  along with this program; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-//  02111-1307, USA.
+//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
 #include <config.h>
 
 #include <algorithm>
 #include <iostream>
 #include <cmath>
 
-#include "app/globals.h"
-#include "tile.h"
-#include "tile_manager.h"
-#include "gameloop.h"
-#include "gameobjs.h"
-#include "special/sprite_manager.h"
-#include "resources.h"
-#include "sector.h"
-#include "tilemap.h"
-#include "video/drawing_context.h"
-#include "camera.h"
-
-BouncyCoin::BouncyCoin(const Vector& pos)
-  : position(pos)
-{
-  timer.start(.3);
-  sprite = sprite_manager->create("coin");
-  sprite->set_action("still");
+#include "tile.hpp"
+#include "tile_manager.hpp"
+#include "game_session.hpp"
+#include "gameobjs.hpp"
+#include "sprite/sprite_manager.hpp"
+#include "sprite/sprite.hpp"
+#include "resources.hpp"
+#include "sector.hpp"
+#include "tilemap.hpp"
+#include "video/drawing_context.hpp"
+#include "camera.hpp"
+#include "main.hpp"
+#include "random_generator.hpp"
+
+/** this controls the time over which a bouncy coin fades */
+static const float FADE_TIME = .2f;
+/** this is the total life time of a bouncy coin */
+static const float LIFE_TIME = .5f;
+
+BouncyCoin::BouncyCoin(const Vector& pos, bool emerge)
+  : position(pos), emerge_distance(0)
+{
+  timer.start(LIFE_TIME);
+  sprite = sprite_manager->create("images/objects/coin/coin.sprite");
+
+  if(emerge) {
+    emerge_distance = sprite->get_height();
+  }
 }
 
 BouncyCoin::~BouncyCoin()
@@ -50,9 +59,11 @@ BouncyCoin::~BouncyCoin()
 }
 
 void
-BouncyCoin::action(float elapsed_time)
+BouncyCoin::update(float elapsed_time)
 {
-  position.y += -200 * elapsed_time;
+  float dist = -200 * elapsed_time;
+  position.y += dist;
+  emerge_distance += dist;
 
   if(timer.check())
     remove_me();
@@ -61,7 +72,25 @@ BouncyCoin::action(float elapsed_time)
 void
 BouncyCoin::draw(DrawingContext& context)
 {
-  sprite->draw(context, position, LAYER_OBJECTS);
+  float time_left = timer.get_timeleft();
+  bool fading = time_left < FADE_TIME;
+  if(fading) {
+    float alpha = time_left/FADE_TIME;
+    context.push_transform();
+    context.set_alpha(alpha);
+  }
+
+  int layer;
+  if(emerge_distance > 0) {
+    layer = LAYER_OBJECTS - 5;
+  } else {
+    layer = LAYER_OBJECTS + 5;
+  }
+  sprite->draw(context, position, layer);
+
+  if(fading) {
+    context.pop_transform();
+  }
 }
 
 //---------------------------------------------------------------------------
@@ -70,7 +99,7 @@ BrokenBrick::BrokenBrick(Sprite* nsprite,
     const Vector& pos, const Vector& nmovement)
   : sprite(new Sprite(*nsprite)), position(pos), movement(nmovement)
 {
-  timer.start(.2);
+  timer.start(.2f);
 }
 
 BrokenBrick::~BrokenBrick()
@@ -79,7 +108,7 @@ BrokenBrick::~BrokenBrick()
 }
 
 void
-BrokenBrick::action(float elapsed_time)
+BrokenBrick::update(float elapsed_time)
 {
   position += movement * elapsed_time;
 
@@ -91,7 +120,7 @@ void
 BrokenBrick::draw(DrawingContext& context)
 {
   sprite->draw_part(context,
-      Vector(rand() % 16, rand() % 16), Vector(16, 16),
+      Vector(systemRandom.rand(16), systemRandom.rand(16)), Vector(16, 16),
       position, LAYER_OBJECTS + 1);
 }
 
@@ -100,14 +129,14 @@ BrokenBrick::draw(DrawingContext& context)
 FloatingText::FloatingText(const Vector& pos, const std::string& text_)
   : position(pos), text(text_)
 {
-  timer.start(.1);
+  timer.start(.1f);
   position.x -= text.size() * 8;
 }
 
 FloatingText::FloatingText(const Vector& pos, int score)
   : position(pos)
 {
-  timer.start(.1);
+  timer.start(.1f);
 
   // turn int into a string
   char str[10];
@@ -118,7 +147,7 @@ FloatingText::FloatingText(const Vector& pos, int score)
 }
 
 void
-FloatingText::action(float elapsed_time)
+FloatingText::update(float elapsed_time)
 {
   position.y -= 1.4 * elapsed_time;
 
@@ -131,7 +160,7 @@ FloatingText::action(float elapsed_time)
 void
 FloatingText::draw(DrawingContext& context)
 {
-  // make an alpha animation when disapearing
+  // make an alpha animation when disappearing
   int alpha;
   if(timer.get_timeleft() < FADING_TIME)
     alpha = int(timer.get_timeleft() * 255 / FADING_TIME);
@@ -141,405 +170,36 @@ FloatingText::draw(DrawingContext& context)
   context.push_transform();
   context.set_alpha(alpha);
 
-  context.draw_text(gold_text, text, position, LEFT_ALLIGN, LAYER_OBJECTS+1);
+  context.draw_text(gold_text, text, position, ALIGN_LEFT, LAYER_OBJECTS+1);
 
   context.pop_transform();
 }
 
-/* Trampoline */
-
-#if 0
-Sprite *img_trampoline;
-
-Trampoline::Trampoline(LispReader& reader)
-{
-  reader.read_float("x", base.x);
-  reader.read_float("y", base.y); 
-  base.width = 32;
-  base.height = 32;
-  power = 7.5;
-  reader.read_float("power", power);
-
-  frame = 0;
-  mode = M_NORMAL;
-  physic.reset();
-}
-
-Trampoline::Trampoline(float x, float y)
-{
-  base.x = x;
-  base.y = y;
-  base.width = 32;
-  base.height = 32;
-  power = 7.5;
-
-  frame = 0;
-  mode = M_NORMAL;
-  physic.reset();
-}
-
-void
-Trampoline::write(LispWriter& writer)
-{
-  writer.start_list("trampoline");
-
-  writer.write_float("x", base.x);
-  writer.write_float("y", base.y);
-  writer.write_float("power", power);
-
-  writer.end_list("trampoline");
-}
-
-void
-Trampoline::draw(DrawingContext& context)
-{
-  img_trampoline->set_frame(frame);
-  img_trampoline->draw(context, base, LAYER_OBJECTS);
-  frame = 0;
-}
-
-void
-Trampoline::action(float frame_ratio)
-{
-  // TODO: Remove if we're too far off the screen
-
-  // Falling
-  if (mode != M_HELD)
-  {
-    if (issolid(base.x + base.width/2, base.y + base.height))
-    {
-      base.y = int((base.y + base.height)/32) * 32 - base.height;
-
-      physic.enable_gravity(false);
-      physic.set_velocity_y(0.0f);
-
-      physic.set_velocity_x(0);
-    }
-    else
-    {
-      physic.enable_gravity(true);
-    }
-  }
-  else // Player is carrying us around
-  {
-    /* FIXME: The trampoline object shouldn't know about pplayer objects. */
-    /* If we're holding the iceblock */
-    Player& tux = *Sector::current()->player;
-    Direction dir = tux.dir;
-
-    if(dir == RIGHT)
-    {
-      base.x = tux.base.x + 16;
-      base.y = tux.base.y + tux.base.height/1.5 - base.height;
-    }
-    else /* facing left */
-    {
-      base.x = tux.base.x - 16;
-      base.y = tux.base.y + tux.base.height/1.5 - base.height;
-    }
-
-    if(collision_object_map(base))
-    {
-      base.x = tux.base.x;
-      base.y = tux.base.y + tux.base.height/1.5 - base.height;
-    }
-  }
-
-  physic.apply(frame_ratio, base.x, base.y, Sector::current()->gravity);
-  collision_swept_object_map(&old_base, &base);
-}
-
-void
-Trampoline::collision(const MovingObject&, int)
-{
-  // comes later
-}
-
-void
-Trampoline::collision(void *p_c_object, int c_object, CollisionType type)
-{
-  Player* pplayer_c = NULL;
-  switch (c_object)
-  {
-    case CO_PLAYER:
-      pplayer_c = (Player*) p_c_object;
-
-      if (type == COLLISION_NORMAL)
-      {
-        // Pick up if HELD (done in Player)
-      }
-
-      else if (type == COLLISION_SQUISH)
-      {
-        int squish_amount = (32 - (int)pplayer_c->base.y % 32);
-
-        if (squish_amount < 24)
-          frame = 3;
-        else if (squish_amount < 28)
-          frame = 2;
-        else if (squish_amount < 30)
-          frame = 1;
-        else
-          frame = 0;
-
-        if (squish_amount < 20) {
-          pplayer_c->physic.set_velocity_y(power);
-          pplayer_c->fall_mode = Player::TRAMPOLINE_JUMP;
-        }
-        else if (pplayer_c->physic.get_velocity_y() < 0)
-          pplayer_c->physic.set_velocity_y(-squish_amount/32);
-      }
-
-      break;
-
-    default:
-      break;
-    
-  }
-}
-#endif
-
-/* Flying Platform */
-
-#if 0
-Sprite *img_flying_platform;
-
-FlyingPlatform::FlyingPlatform(LispReader& reader)
-{
-  reader.read_int_vector("x", pos_x);
-  reader.read_int_vector("y", pos_y);
-
-  velocity = 2.0;
-  reader.read_float("velocity", velocity);
-
-  base.x = pos_x[0];
-  base.y = pos_y[0];
-  base.width = 96;
-  base.height = 40;
-
-  point = 0;
-  move = false;
-
-  float x = pos_x[point+1] - pos_x[point];
-  float y = pos_y[point+1] - pos_y[point];
-  vel_x = x*velocity / sqrt(x*x + y*y);
-  vel_y = -(velocity - vel_x);
-
-  frame = 0;
-}
-
-FlyingPlatform::FlyingPlatform(int x, int y)
-{
-base.x = x;
-base.y = y;
-point = 0;
-move = false;
-}
-
-void
-FlyingPlatform::write(LispWriter& writer)
-{
-  writer.start_list("flying-trampoline");
-
-  writer.write_int_vector("x", pos_x);
-  writer.write_int_vector("y", pos_y);
-  writer.write_float("velocity", velocity);
-
-  writer.end_list("flying-trampoline");
-}
-
-void
-FlyingPlatform::draw(DrawingContext& context)
-{
-  img_flying_platform->draw(context, base, LAYER_OBJECTS);
-}
-
-void
-FlyingPlatform::action(float frame_ratio)
-{
-  // TODO: Remove if we're too far off the screen
-
-if(!move)
-  return;
-
-if((unsigned)point+1 != pos_x.size())
-  {
-  if(((pos_x[point+1] > pos_x[point] && base.x >= pos_x[point+1]) ||
-      (pos_x[point+1] < pos_x[point] && base.x <= pos_x[point+1]) ||
-      pos_x[point] == pos_x[point+1]) &&
-    ((pos_y[point+1] > pos_y[point] && base.y >= pos_y[point+1]) ||
-      (pos_y[point+1] < pos_y[point] && base.y <= pos_y[point+1]) ||
-      pos_y[point] == pos_y[point+1]))
-    {
-    point++;
-
-    float x = pos_x[point+1] - pos_x[point];
-    float y = pos_y[point+1] - pos_y[point];
-    vel_x = x*velocity / sqrt(x*x + y*y);
-    vel_y = -(velocity - vel_x);
-    }
-  }
-else   // last point
-  {
-  // point = 0;
-  // reverse vector
-  return;
-  }
-/*
-if(pos_x[point+1] > base.x)
-  base.x += velocity * frame_ratio;
-else if(pos_x[point+1] < base.x)
-  base.x -= velocity * frame_ratio;
-
-if(pos_y[point+1] > base.y)
-  base.y += velocity * frame_ratio;
-else if(pos_y[point+1] < base.y)
-  base.y -= velocity * frame_ratio;
-*/
-
-base.x += vel_x * frame_ratio;
-base.y += vel_y * frame_ratio;
-}
-
-void
-FlyingPlatform::collision(const MovingObject&, int)
-{
-  // comes later
-}
-
-void
-FlyingPlatform::collision(void *p_c_object, int c_object, CollisionType type)
-{
-(void) p_c_object;
-(void) type;
-
-//  Player* pplayer_c = NULL;
-  switch (c_object)
-  {
-    case CO_PLAYER:
-//      pplayer_c = (Player*) p_c_object;
-      move = true;
-
-      break;
-
-    default:
-      break;
-    
-  }
-}
-#endif
-
 Sprite *img_smoke_cloud = 0;
 
 SmokeCloud::SmokeCloud(const Vector& pos)
   : position(pos)
 {
-  timer.start(.3);
+  timer.start(.3f);
+  sprite = sprite_manager->create("images/objects/particles/stomp.sprite");
 }
 
-void
-SmokeCloud::action(float elapsed_time)
+SmokeCloud::~SmokeCloud()
 {
-  position.y -= 120 * elapsed_time;
-
-  if(timer.check())
-    remove_me();
-}
-
-void
-SmokeCloud::draw(DrawingContext& context)
-{
-  img_smoke_cloud->draw(context, position, LAYER_OBJECTS+1);
-}
-
-Particles::Particles(const Vector& epicenter, int min_angle, int max_angle,
-        const Vector& initial_velocity, const Vector& acceleration, int number,
-        Color color_, int size_, float life_time, int drawing_layer_)
-  : accel(acceleration), color(color_), size(size_), drawing_layer(drawing_layer_)
-{
-  if(life_time == 0) {
-    live_forever = true;
-  } else {
-    live_forever = false;
-    timer.start(life_time);
-  }
-
-  // create particles
-  for(int p = 0; p < number; p++)
-    {
-    Particle* particle = new Particle;
-    particle->pos = epicenter;
-
-    float angle = ((rand() % (max_angle-min_angle))+min_angle)
-                      * (M_PI / 180);  // convert to radius
-    particle->vel.x = /*fabs*/(sin(angle)) * initial_velocity.x;
-//    if(angle >= M_PI && angle < M_PI*2)
-//      particle->vel.x *= -1;  // work around to fix signal
-    particle->vel.y = /*fabs*/(cos(angle)) * initial_velocity.y;
-//    if(angle >= M_PI_2 && angle < 3*M_PI_2)
-//      particle->vel.y *= -1;
-
-    particles.push_back(particle);
-    }
-}
-
-Particles::~Particles()
-{
-  // free particles
-  for(std::vector<Particle*>::iterator i = particles.begin();
-      i < particles.end(); i++)
-    delete (*i);
+  delete sprite;
 }
 
 void
-Particles::action(float elapsed_time)
+SmokeCloud::update(float elapsed_time)
 {
-  Vector camera = Sector::current()->camera->get_translation();
-
-  // update particles
-  for(std::vector<Particle*>::iterator i = particles.begin();
-      i != particles.end(); ) {
-    (*i)->pos.x += (*i)->vel.x * elapsed_time;
-    (*i)->pos.y += (*i)->vel.y * elapsed_time;
-
-    (*i)->vel.x += accel.x * elapsed_time;
-    (*i)->vel.y += accel.y * elapsed_time;
-
-    if((*i)->pos.x < camera.x || (*i)->pos.x > screen->w + camera.x ||
-       (*i)->pos.y < camera.y || (*i)->pos.y > screen->h + camera.y) {
-      delete (*i);
-      i = particles.erase(i);
-    } else {
-      ++i;
-    }
-  }
+  position.y -= 120 * elapsed_time;
 
-  if((timer.check() && !live_forever) || particles.size() == 0)
+  if(timer.check())
     remove_me();
 }
 
 void
-Particles::draw(DrawingContext& context)
-{
-  // draw particles
-  for(std::vector<Particle*>::iterator i = particles.begin();
-      i != particles.end(); i++) {
-    context.draw_filled_rect((*i)->pos, Vector(size,size), color,drawing_layer);
-  }
-}
-
-void load_object_gfx()
-{
-#if 0
-  img_trampoline = sprite_manager->load("trampoline");
-  img_trampoline->start_animation(0);
-  img_flying_platform = sprite_manager->load("flying_platform");
-#endif
-  img_smoke_cloud = sprite_manager->create("stomp");
-}
-
-void free_object_gfx()
+SmokeCloud::draw(DrawingContext& context)
 {
-  delete img_smoke_cloud;
+  sprite->draw(context, position, LAYER_OBJECTS+1);
 }
-