restore trunk
[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 #include "timer.hpp"
25
26 static const float SPEED_GROW = 256;
27 static const float SPEED_SHRINK = 128;
28 static const float HATCH_TIME = 0.75;
29
30 Root::Root(const Vector& pos)
31   : BadGuy(pos, "images/creatures/ghosttree/root.sprite", LAYER_TILES-1),
32     mystate(STATE_APPEARING), offset_y(0)
33 {
34   base_sprite.reset(sprite_manager->create("images/creatures/ghosttree/root-base.sprite"));
35   base_sprite->set_action("appearing", 1);
36   base_sprite->set_animation_loops(1); // TODO: necessary because set_action ignores loops for default action
37   physic.enable_gravity(false);
38 }
39
40 Root::~Root()
41 {
42 }
43
44 void
45 Root::activate()
46 {
47   set_group(COLGROUP_TOUCHABLE);
48 }
49
50 void
51 Root::deactivate()
52 {
53   remove_me(); 
54 }
55
56 void
57 Root::active_update(float elapsed_time)
58 {
59   if (mystate == STATE_APPEARING) {  
60     if (base_sprite->animation_done()) {
61       hatch_timer.start(HATCH_TIME);
62       mystate = STATE_HATCHING;
63     }
64   }
65   if (mystate == STATE_HATCHING) {
66     if (!hatch_timer.started()) mystate = STATE_GROWING;
67   }
68   else if (mystate == STATE_GROWING) {
69     offset_y -= elapsed_time * SPEED_GROW;
70     if (offset_y < -sprite->get_height()) {
71       offset_y = -sprite->get_height();
72       mystate = STATE_SHRINKING;
73     }
74     set_pos(start_position + Vector(0, offset_y));
75   }
76   else if (mystate == STATE_SHRINKING) {
77     offset_y += elapsed_time * SPEED_SHRINK;
78     if (offset_y > 0) {
79       offset_y = 0;
80       mystate = STATE_VANISHING;
81       base_sprite->set_action("vanishing", 2);
82       base_sprite->set_animation_loops(2); // TODO: doesn't seem to work for loops=1 
83     }
84     set_pos(start_position + Vector(0, offset_y));
85   }
86   else if (mystate == STATE_VANISHING) {
87     if (base_sprite->animation_done()) remove_me();
88   }
89   BadGuy::active_update(elapsed_time);
90 }
91
92 void
93 Root::draw(DrawingContext& context)
94 {
95   base_sprite->draw(context, start_position, LAYER_TILES+1);
96   if ((mystate != STATE_APPEARING) && (mystate != STATE_VANISHING)) BadGuy::draw(context);
97 }
98