#include <config.h>
#include "root.hpp"
+#include "sprite/sprite_manager.hpp"
-static const float SPEED = 5;
+static const float SPEED_GROW = 256;
+static const float SPEED_SHRINK = 128;
Root::Root(const Vector& pos)
- : BadGuy(pos, "images/creatures/ghosttree/root.sprite", LAYER_OBJECTS+20),
- ypos(0), speed(-SPEED)
+ : BadGuy(pos, "images/creatures/ghosttree/root.sprite", LAYER_TILES-1),
+ mystate(STATE_APPEARING), offset_y(0)
{
- //ypos = sprite->get_height();
+ base_sprite.reset(sprite_manager->create("images/creatures/ghosttree/root-base.sprite"));
+ base_sprite->set_action("appearing", 1);
+ base_sprite->set_animation_loops(1); // TODO: necessary because set_action ignores loops for default action
+ physic.enable_gravity(false);
}
Root::~Root()
void
Root::activate()
{
- set_pos(start_position + Vector(0, ypos));
+ set_group(COLGROUP_TOUCHABLE);
+}
+
+void
+Root::deactivate()
+{
+ remove_me();
}
void
Root::active_update(float elapsed_time)
{
- ypos += elapsed_time * speed;
- if(ypos < 0) {
- ypos = 0;
- speed = -speed;
+ if (mystate == STATE_APPEARING) {
+ if (base_sprite->animation_done()) mystate = STATE_GROWING;
+ }
+ else if (mystate == STATE_GROWING) {
+ offset_y -= elapsed_time * SPEED_GROW;
+ if (offset_y < -sprite->get_height()) {
+ offset_y = -sprite->get_height();
+ mystate = STATE_SHRINKING;
+ }
+ set_pos(start_position + Vector(0, offset_y));
}
- if(speed > 0 && ypos > sprite->get_height()) {
- remove_me();
+ else if (mystate == STATE_SHRINKING) {
+ offset_y += elapsed_time * SPEED_SHRINK;
+ if (offset_y > 0) {
+ offset_y = 0;
+ mystate = STATE_VANISHING;
+ base_sprite->set_action("vanishing", 2);
+ base_sprite->set_animation_loops(2); // TODO: doesn't seem to work for loops=1
+ }
+ set_pos(start_position + Vector(0, offset_y));
}
- set_pos(start_position + Vector(0, ypos));
+ else if (mystate == STATE_VANISHING) {
+ if (base_sprite->animation_done()) remove_me();
+ }
+ BadGuy::active_update(elapsed_time);
+}
+
+void
+Root::draw(DrawingContext& context)
+{
+ base_sprite->draw(context, start_position, LAYER_TILES+1);
+ if ((mystate != STATE_APPEARING) && (mystate != STATE_VANISHING)) BadGuy::draw(context);
}
#ifndef __ROOT_H__
#define __ROOT_H__
+#include <memory>
#include "badguy.hpp"
class Root : public BadGuy
~Root();
void activate();
+ void deactivate();
void active_update(float elapsed_time);
+ virtual void draw(DrawingContext& context);
+ virtual bool is_flammable() const { return false; }
+ virtual bool is_freezable() const { return false; }
+ virtual void kill_fall() { }
-private:
- float ypos;
- float speed;
+protected:
+ enum MyState {
+ STATE_APPEARING, STATE_GROWING, STATE_SHRINKING, STATE_VANISHING
+ };
+ MyState mystate;
+ std::auto_ptr<Sprite> base_sprite;
+ float offset_y;
};
#endif