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