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