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