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