70feda7698cf7a2f2d52b7678eb257e6ee76b1d8
[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 void
69 SpiderMite::collision_solid(const CollisionHit& hit)
70 {
71   if(hit.top || hit.bottom) { // hit floor or roof?
72     physic.set_velocity_y(0);
73   }
74 }
75
76 void
77 SpiderMite::active_update(float elapsed_time) 
78 {
79   if(timer.check()) {
80     if(mode == FLY_UP) {
81       mode = FLY_DOWN;
82       physic.set_velocity_y(-FLYSPEED);
83     } else if(mode == FLY_DOWN) {
84       mode = FLY_UP;
85       physic.set_velocity_y(FLYSPEED);
86     }
87     timer.start(FLYTIME);
88   }
89   movement=physic.get_movement(elapsed_time);
90
91   Player* player = this->get_nearest_player();
92   if (player) {
93     dir = (player->get_pos().x > get_pos().x) ? RIGHT : LEFT;
94     sprite->set_action(dir == LEFT ? "left" : "right");
95   }
96 }
97
98 IMPLEMENT_FACTORY(SpiderMite, "spidermite")