Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / badguy / root.cpp
1 //  SuperTux - "Will-O-Wisp" Badguy
2 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "badguy/root.hpp"
18 #include "sprite/sprite.hpp"
19 #include "sprite/sprite_manager.hpp"
20
21 static const float SPEED_GROW = 256;
22 static const float SPEED_SHRINK = 128;
23 static const float HATCH_TIME = 0.75;
24
25 Root::Root(const Vector& pos) :
26   BadGuy(pos, "images/creatures/ghosttree/root.sprite", LAYER_TILES-1),
27   mystate(STATE_APPEARING), 
28   base_sprite(),
29   offset_y(0),
30   hatch_timer()
31 {
32   base_sprite = 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   set_colgroup_active(COLGROUP_TOUCHABLE);
37 }
38
39 Root::~Root()
40 {
41 }
42
43 void
44 Root::deactivate()
45 {
46   remove_me();
47   //no dead script
48 }
49
50 void
51 Root::active_update(float elapsed_time)
52 {
53   if (mystate == STATE_APPEARING) {  
54     if (base_sprite->animation_done()) {
55       hatch_timer.start(HATCH_TIME);
56       mystate = STATE_HATCHING;
57     }
58   }
59   if (mystate == STATE_HATCHING) {
60     if (!hatch_timer.started()) mystate = STATE_GROWING;
61   }
62   else if (mystate == STATE_GROWING) {
63     offset_y -= elapsed_time * SPEED_GROW;
64     if (offset_y < -sprite->get_height()) {
65       offset_y = -sprite->get_height();
66       mystate = STATE_SHRINKING;
67     }
68     set_pos(start_position + Vector(0, offset_y));
69   }
70   else if (mystate == STATE_SHRINKING) {
71     offset_y += elapsed_time * SPEED_SHRINK;
72     if (offset_y > 0) {
73       offset_y = 0;
74       mystate = STATE_VANISHING;
75       base_sprite->set_action("vanishing", 2);
76       base_sprite->set_animation_loops(2); // TODO: doesn't seem to work for loops=1 
77     }
78     set_pos(start_position + Vector(0, offset_y));
79   }
80   else if (mystate == STATE_VANISHING) {
81     if (base_sprite->animation_done()) remove_me();
82   }
83   BadGuy::active_update(elapsed_time);
84 }
85
86 void
87 Root::draw(DrawingContext& context)
88 {
89   base_sprite->draw(context, start_position, LAYER_TILES+1);
90   if ((mystate != STATE_APPEARING) && (mystate != STATE_VANISHING)) BadGuy::draw(context);
91 }
92
93 /* EOF */