41ccb2e34ebed46ac3a2afd5b2a4d8aecd592b16
[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 static const float FLYTIME = 1.2;
26 static const float FLYSPEED = -100.0;
27
28 SpiderMite::SpiderMite(const lisp::Lisp& reader)
29         : BadGuy(reader, "images/creatures/spidermite/spidermite.sprite")
30 {
31   physic.enable_gravity(false);
32 }
33
34 SpiderMite::SpiderMite(const Vector& pos)
35         : BadGuy(pos, "images/creatures/spidermite/spidermite.sprite")
36 {
37   physic.enable_gravity(false);
38 }
39
40 void
41 SpiderMite::write(lisp::Writer& writer)
42 {
43   writer.start_list("spidermite");
44
45   writer.write_float("x", start_position.x);
46   writer.write_float("y", start_position.y);
47
48   writer.end_list("spidermite");
49 }
50
51 void 
52 SpiderMite::activate()
53 {
54   sprite->set_action(dir == LEFT ? "left" : "right");
55   mode = FLY_UP;
56   physic.set_velocity_y(FLYSPEED);
57   timer.start(FLYTIME/2);
58 }
59
60 bool
61 SpiderMite::collision_squished(Player& player)
62 {
63   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
64   kill_squished(player);
65   return true;
66 }
67
68 HitResponse
69 SpiderMite::collision_solid(GameObject& , const CollisionHit& hit)
70 {
71   if(fabsf(hit.normal.y) > 1.5) { // hit floor or roof?
72     physic.set_velocity_y(0);
73   }
74
75   return CONTINUE;
76 }
77
78 void
79 SpiderMite::active_update(float elapsed_time) 
80 {
81   if(timer.check()) {
82     if(mode == FLY_UP) {
83       mode = FLY_DOWN;
84       physic.set_velocity_y(-FLYSPEED);
85     } else if(mode == FLY_DOWN) {
86       mode = FLY_UP;
87       physic.set_velocity_y(FLYSPEED);
88     }
89     timer.start(FLYTIME);
90   }
91   movement=physic.get_movement(elapsed_time);
92
93   Player* player = this->get_nearest_player();
94   if (player) {
95     dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
96     sprite->set_action(dir == LEFT ? "left" : "right");
97   }
98 }
99
100 IMPLEMENT_FACTORY(SpiderMite, "spidermite")