Added to ghosttree: roots growing from below
[supertux.git] / src / badguy / root.cpp
1 //  $Id$
2 //
3 //  SuperTux - "Will-O-Wisp" Badguy
4 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20 #include <config.h>
21
22 #include "root.hpp"
23 #include "sprite/sprite_manager.hpp"
24
25 static const float SPEED_GROW = 256;
26 static const float SPEED_SHRINK = 128;
27
28 Root::Root(const Vector& pos)
29   : BadGuy(pos, "images/creatures/ghosttree/root.sprite", LAYER_TILES-1),
30     mystate(STATE_APPEARING), offset_y(0)
31 {
32   base_sprite.reset(sprite_manager->create("images/creatures/ghosttree/root-base.sprite"));
33   base_sprite->set_action("appearing", 1);
34   base_sprite->set_animation_loops(1); // TODO: necessary because set_action ignores loops for default action
35   physic.enable_gravity(false);
36 }
37
38 Root::~Root()
39 {
40 }
41
42 void
43 Root::activate()
44 {
45   set_group(COLGROUP_TOUCHABLE);
46 }
47
48 void
49 Root::deactivate()
50 {
51   remove_me(); 
52 }
53
54 void
55 Root::active_update(float elapsed_time)
56 {
57   if (mystate == STATE_APPEARING) {  
58     if (base_sprite->animation_done()) mystate = STATE_GROWING;
59   }
60   else if (mystate == STATE_GROWING) {
61     offset_y -= elapsed_time * SPEED_GROW;
62     if (offset_y < -sprite->get_height()) {
63       offset_y = -sprite->get_height();
64       mystate = STATE_SHRINKING;
65     }
66     set_pos(start_position + Vector(0, offset_y));
67   }
68   else if (mystate == STATE_SHRINKING) {
69     offset_y += elapsed_time * SPEED_SHRINK;
70     if (offset_y > 0) {
71       offset_y = 0;
72       mystate = STATE_VANISHING;
73       base_sprite->set_action("vanishing", 2);
74       base_sprite->set_animation_loops(2); // TODO: doesn't seem to work for loops=1 
75     }
76     set_pos(start_position + Vector(0, offset_y));
77   }
78   else if (mystate == STATE_VANISHING) {
79     if (base_sprite->animation_done()) remove_me();
80   }
81   BadGuy::active_update(elapsed_time);
82 }
83
84 void
85 Root::draw(DrawingContext& context)
86 {
87   base_sprite->draw(context, start_position, LAYER_TILES+1);
88   if ((mystate != STATE_APPEARING) && (mystate != STATE_VANISHING)) BadGuy::draw(context);
89 }
90