Badguys now inherit from MovingSprite
[supertux.git] / src / badguy / fish.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
22 #include "fish.hpp"
23 #include "tile.hpp"
24 #include "object/tilemap.hpp"
25
26 static const float FISH_JUMP_POWER = 600;
27 static const float FISH_WAIT_TIME = 1;
28
29 Fish::Fish(const lisp::Lisp& reader)
30         : BadGuy(reader, "images/creatures/fish/fish.sprite")
31 {
32   physic.enable_gravity(true);
33 }
34
35 Fish::Fish(const Vector& pos)
36         : BadGuy(pos, "images/creatures/fish/fish.sprite")
37 {
38   physic.enable_gravity(true);
39 }
40
41 void
42 Fish::write(lisp::Writer& writer)
43 {
44   writer.start_list("fish");
45
46   writer.write_float("x", start_position.x);
47   writer.write_float("y", start_position.y);
48
49   writer.end_list("fish");
50 }
51
52 HitResponse
53 Fish::collision_solid(GameObject& , const CollisionHit& chit)
54 {
55   return hit(chit);
56 }
57
58 HitResponse
59 Fish::collision_badguy(BadGuy& , const CollisionHit& chit)
60 {
61   return hit(chit);
62 }
63
64 void
65 Fish::draw(DrawingContext& context)
66 {
67   if(waiting.started())
68     return;
69
70   BadGuy::draw(context);
71 }
72
73 HitResponse
74 Fish::hit(const CollisionHit& chit)
75 {
76   if(chit.normal.y < .5) { // hit ceiling
77     physic.set_velocity_y(0);
78   }
79
80   return CONTINUE;
81 }
82
83 void
84 Fish::collision_tile(uint32_t tile_attributes)
85 {
86   if(tile_attributes & Tile::WATER) {
87     start_waiting();
88     movement = Vector(0, 0);
89   }
90 }
91
92 void
93 Fish::active_update(float elapsed_time)
94 {
95   BadGuy::active_update(elapsed_time);
96
97   // waited long enough?
98   if(waiting.check()) {
99     jump();
100   }
101   
102   // set sprite
103   sprite->set_action(physic.get_velocity_y() > 0 ? "normal" : "down");
104   
105   // we can't afford flying out of the tilemap, 'cause the engine would remove us.
106   if ((get_pos().y - 31.8) < 0) // too high, let us fall
107   {
108     physic.set_velocity_y(0);
109     physic.enable_gravity(true);
110   }
111 }
112
113 void
114 Fish::start_waiting()
115 {
116   waiting.start(FISH_WAIT_TIME);
117   set_group(COLGROUP_DISABLED);
118   physic.enable_gravity(false);
119   physic.set_velocity_y(0);
120 }
121
122 void
123 Fish::jump()
124 {
125   physic.set_velocity_y(FISH_JUMP_POWER);
126   physic.enable_gravity(true);
127   set_group(COLGROUP_MOVING);
128 }
129
130 IMPLEMENT_FACTORY(Fish, "fish")