9e83b5040fe8d9f1ddba1f70b1604ea62a311429
[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", LAYER_TILES-1), stop_y(0)
32 {
33   physic.enable_gravity(true);
34 }
35
36 Fish::Fish(const Vector& pos)
37   : BadGuy(pos, "images/creatures/fish/fish.sprite", LAYER_TILES-1), stop_y(0)
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("x", start_position.x);
48   writer.write("y", start_position.y);
49
50   writer.end_list("fish");
51 }
52
53 void
54 Fish::collision_solid(const CollisionHit& chit)
55 {
56   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   if (get_state() == STATE_FALLING) {
72     sprite->set_action("down");
73     sprite->draw(context, get_pos(), layer);
74   }
75   else if (get_state() == STATE_ACTIVE) {
76     sprite->draw(context, get_pos(), layer);
77   }
78 }
79
80 HitResponse
81 Fish::hit(const CollisionHit& hit)
82 {
83   if(hit.top) {
84     physic.set_velocity_y(0);
85   }
86
87   return CONTINUE;
88 }
89
90 void
91 Fish::collision_tile(uint32_t tile_attributes)
92 {
93   if ((tile_attributes & Tile::WATER) && (physic.get_velocity_y() >= 0)) {
94
95     // initialize stop position if uninitialized
96     if (stop_y == 0) stop_y = get_pos().y + get_bbox().get_height();
97
98     // stop when we have reached the stop position
99     if (get_pos().y >= stop_y) {
100       if(!frozen)
101         start_waiting();
102       movement = Vector(0, 0);
103     }
104
105   }
106 }
107
108 void
109 Fish::active_update(float elapsed_time)
110 {
111   BadGuy::active_update(elapsed_time);
112
113   // waited long enough?
114   if(waiting.check()) {
115     jump();
116   }
117
118   // set sprite
119   if(!frozen)
120     sprite->set_action(physic.get_velocity_y() < 0 ? "normal" : "down");
121
122   // we can't afford flying out of the tilemap, 'cause the engine would remove us.
123   if ((get_pos().y - 31.8) < 0) // too high, let us fall
124   {
125     physic.set_velocity_y(0);
126     physic.enable_gravity(true);
127   }
128 }
129
130 void
131 Fish::start_waiting()
132 {
133   waiting.start(FISH_WAIT_TIME);
134   set_colgroup_active(COLGROUP_DISABLED);
135   physic.enable_gravity(false);
136   physic.set_velocity_y(0);
137 }
138
139 void
140 Fish::jump()
141 {
142   physic.set_velocity_y(FISH_JUMP_POWER);
143   physic.enable_gravity(true);
144   set_colgroup_active(COLGROUP_MOVING);
145 }
146
147 void
148 Fish::freeze()
149 {
150   BadGuy::freeze();
151   sprite->set_action(physic.get_velocity_y() < 0 ? "iced" : "iced-down");
152   waiting.stop();
153 }
154
155 void
156 Fish::unfreeze()
157 { // does this happen at all? (or do fishes die when they fall frozen?)
158   BadGuy::unfreeze();
159   start_waiting();
160 }
161
162 bool
163 Fish::is_freezable() const
164 {
165   return true;
166 }
167
168 IMPLEMENT_FACTORY(Fish, "fish")