converted player to new object system
[supertux.git] / src / gameobjs.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
5 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 // 
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 //  02111-1307, USA.
21 #include <algorithm>
22 #include <iostream>
23 #include "world.h"
24 #include "tile.h"
25 #include "gameloop.h"
26 #include "gameobjs.h"
27 #include "sprite_manager.h"
28 #include "resources.h"
29 #include "level.h"
30 #include "display_manager.h"
31
32 BouncyDistro::BouncyDistro(DisplayManager& displaymanager, const Vector& pos)
33   : position(pos)
34 {
35   ym = -2;
36   displaymanager.add_drawable(this, LAYER_OBJECTS);
37 }
38
39 void
40 BouncyDistro::action(float elapsed_time)
41 {
42   position.y += ym * elapsed_time;
43
44   ym += 0.1 * elapsed_time; // not framerate independent... but who really cares
45   if(ym >= 0)
46     remove_me();
47 }
48
49 void
50 BouncyDistro::draw(ViewPort& viewport, int )
51 {
52   img_distro[0]->draw(viewport.world2screen(position));
53 }
54
55
56 BrokenBrick::BrokenBrick(DisplayManager& displaymanager, Tile* ntile,
57     const Vector& pos, const Vector& nmovement)
58   : tile(ntile), position(pos), movement(nmovement)
59 {
60   displaymanager.add_drawable(this, LAYER_OBJECTS);
61   timer.start(200);
62 }
63
64 void
65 BrokenBrick::action(float elapsed_time)
66 {
67   position += movement * elapsed_time;
68
69   if (!timer.check())
70     remove_me();
71 }
72
73 void
74 BrokenBrick::draw(ViewPort& viewport, int )
75 {
76   SDL_Rect src, dest;
77   src.x = rand() % 16;
78   src.y = rand() % 16;
79   src.w = 16;
80   src.h = 16;
81
82   dest.x = (int)(position.x - viewport.get_translation().x);
83   dest.y = (int)(position.y - viewport.get_translation().y);
84   dest.w = 16;
85   dest.h = 16;
86   
87   if (tile->images.size() > 0)
88     tile->images[0]->draw_part(src.x,src.y,dest.x,dest.y,dest.w,dest.h);
89 }
90
91 BouncyBrick::BouncyBrick(DisplayManager& displaymanager, const Vector& pos)
92   : position(pos), offset(0), offset_m(-BOUNCY_BRICK_SPEED)
93 {
94   displaymanager.add_drawable(this, LAYER_OBJECTS);
95   shape    = World::current()->get_level()->gettileid(pos.x, pos.y);
96 }
97
98 void
99 BouncyBrick::action(float elapsed_time)
100 {
101   offset += offset_m * elapsed_time;
102
103   /* Go back down? */
104   if (offset < -BOUNCY_BRICK_MAX_OFFSET)
105     offset_m = BOUNCY_BRICK_SPEED;
106
107   /* Stop bouncing? */
108   if (offset >= 0)
109     remove_me();
110 }
111
112 void
113 BouncyBrick::draw(ViewPort& viewport, int)
114 {
115   Tile::draw(viewport.world2screen(position + Vector(0, offset)), shape);
116 }
117
118 FloatingScore::FloatingScore(DisplayManager& displaymanager, 
119     const Vector& pos, int score)
120   : position(pos)
121 {
122   displaymanager.add_drawable(this, LAYER_OBJECTS);
123   timer.start(1000);
124   snprintf(str, 10, "%d", score);
125   position.x -= strlen(str) * 8;
126 }
127
128 void
129 FloatingScore::action(float elapsed_time)
130 {
131   position.y -= 2 * elapsed_time;
132
133   if(!timer.check())
134     remove_me();
135 }
136
137 void
138 FloatingScore::draw(ViewPort& viewport, int )
139 {
140   gold_text->draw(str, viewport.world2screen(position));
141 }
142
143 /* Trampoline */
144
145 #define TRAMPOLINE_FRAMES 4
146 Sprite *img_trampoline[TRAMPOLINE_FRAMES];
147
148 void load_object_gfx()
149 {
150   char sprite_name[16];
151
152   for (int i = 0; i < TRAMPOLINE_FRAMES; i++)
153   {
154     sprintf(sprite_name, "trampoline-%i", i+1);
155     img_trampoline[i] = sprite_manager->load(sprite_name);
156   }
157 }
158
159 void
160 Trampoline::init(float x, float y)
161 {
162   base.x = x;
163   base.y = y;
164   base.width = 32;
165   base.height = 32;
166
167   frame = 0;
168   mode = M_NORMAL;
169   physic.reset();
170 }
171
172 void
173 Trampoline::draw()
174 {
175   img_trampoline[frame]->draw((int)base.x, (int)base.y);
176
177   frame = 0;
178
179   if (debug_mode)
180     fillrect(base.x - scroll_x, base.y - scroll_y, base.width, base.height, 75, 75, 0, 150);
181 }
182
183 void
184 Trampoline::action(double frame_ratio)
185 {
186   // TODO: Remove if we're too far off the screen
187
188   // Falling
189   if (mode != M_HELD)
190   {
191     if (issolid(base.x + base.width/2, base.y + base.height))
192     {
193       base.y = int((base.y + base.height)/32) * 32 - base.height;
194
195       physic.enable_gravity(false);
196       physic.set_velocity_y(0.0f);
197
198       physic.set_velocity_x(0);
199     }
200     else
201     {
202       physic.enable_gravity(true);
203     }
204   }
205   else // Player is carrying us around
206   {
207     /* FIXME: The trampoline object shouldn't know about pplayer objects. */
208     /* If we're holding the iceblock */
209     Player& tux = *World::current()->get_tux();
210     Direction dir = tux.dir;
211
212     if(dir == RIGHT)
213     {
214       base.x = tux.base.x + 16;
215       base.y = tux.base.y + tux.base.height/1.5 - base.height;
216     }
217     else /* facing left */
218     {
219       base.x = tux.base.x - 16;
220       base.y = tux.base.y + tux.base.height/1.5 - base.height;
221     }
222
223     if(collision_object_map(base))
224     {
225       base.x = tux.base.x;
226       base.y = tux.base.y + tux.base.height/1.5 - base.height;
227     }
228   }
229
230   physic.apply(frame_ratio, base.x, base.y);
231   collision_swept_object_map(&old_base, &base);
232 }
233
234 void
235 Trampoline::collision(void *p_c_object, int c_object, CollisionType type)
236 {
237   Player* pplayer_c = NULL;
238   switch (c_object)
239   {
240     case CO_PLAYER:
241       pplayer_c = (Player*) p_c_object;
242
243       if (type == COLLISION_NORMAL)
244       {
245         // Pick up if HELD (done in Player)
246       }
247
248       else if (type == COLLISION_SQUISH)
249       {
250         int squish_amount = (32 - (int)pplayer_c->base.y % 32);
251
252         if (squish_amount < 24)
253           frame = 3;
254         else if (squish_amount < 28)
255           frame = 2;
256         else if (squish_amount < 30)
257           frame = 1;
258         else
259           frame = 0;
260
261         if (squish_amount < 20)
262           pplayer_c->physic.set_velocity_y(power);
263         else if (pplayer_c->physic.get_velocity_y() < 0)
264           pplayer_c->physic.set_velocity_y(-squish_amount/32);
265       }
266
267       break;
268
269     default:
270       break;
271     
272   }
273 }
274
275