Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / badguy / spidermite.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.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/spidermite.hpp"
18
19 #include "object/player.hpp"
20 #include "sprite/sprite.hpp"
21 #include "supertux/object_factory.hpp"
22
23 static const float FLYTIME = 1.2f;
24 static const float FLYSPEED = -100.0f;
25
26 SpiderMite::SpiderMite(const Reader& reader) :
27   BadGuy(reader, "images/creatures/spidermite/spidermite.sprite"),
28   mode(),
29   timer()
30 {
31   physic.enable_gravity(false);
32 }
33
34 SpiderMite::SpiderMite(const Vector& pos) :
35   BadGuy(pos, "images/creatures/spidermite/spidermite.sprite"), 
36   mode(),
37   timer()
38 {
39   physic.enable_gravity(false);
40 }
41
42 void
43 SpiderMite::initialize()
44 {
45   sprite->set_action(dir == LEFT ? "left" : "right");
46   mode = FLY_UP;
47   physic.set_velocity_y(FLYSPEED);
48   timer.start(FLYTIME/2);
49 }
50
51 bool
52 SpiderMite::collision_squished(GameObject& object)
53 {
54   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
55   kill_squished(object);
56   return true;
57 }
58
59 void
60 SpiderMite::collision_solid(const CollisionHit& hit)
61 {
62   if(hit.top || hit.bottom) { // hit floor or roof?
63     physic.set_velocity_y(0);
64   }
65 }
66
67 void
68 SpiderMite::active_update(float elapsed_time)
69 {
70   if(timer.check()) {
71     if(mode == FLY_UP) {
72       mode = FLY_DOWN;
73       physic.set_velocity_y(-FLYSPEED);
74     } else if(mode == FLY_DOWN) {
75       mode = FLY_UP;
76       physic.set_velocity_y(FLYSPEED);
77     }
78     timer.start(FLYTIME);
79   }
80   movement=physic.get_movement(elapsed_time);
81
82   Player* player = this->get_nearest_player();
83   if (player) {
84     dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
85     sprite->set_action(dir == LEFT ? "left" : "right");
86   }
87 }
88
89 IMPLEMENT_FACTORY(SpiderMite, "spidermite");
90
91 /* EOF */