caf93ed7c7fcfb7e6c2422baee0333a45eef3403
[supertux.git] / src / object / gameobjs.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 <algorithm>
23 #include <iostream>
24 #include <cmath>
25
26 #include "tile.hpp"
27 #include "tile_manager.hpp"
28 #include "game_session.hpp"
29 #include "gameobjs.hpp"
30 #include "sprite/sprite_manager.hpp"
31 #include "sprite/sprite.hpp"
32 #include "resources.hpp"
33 #include "sector.hpp"
34 #include "tilemap.hpp"
35 #include "video/drawing_context.hpp"
36 #include "camera.hpp"
37 #include "main.hpp"
38 #include "random_generator.hpp"
39
40 BouncyCoin::BouncyCoin(const Vector& pos)
41   : position(pos)
42 {
43   timer.start(.3);
44   sprite = sprite_manager->create("images/objects/coin/coin.sprite");
45   sprite->set_action("still");
46 }
47
48 BouncyCoin::~BouncyCoin()
49 {
50   delete sprite;
51 }
52
53 void
54 BouncyCoin::update(float elapsed_time)
55 {
56   position.y += -200 * elapsed_time;
57
58   if(timer.check())
59     remove_me();
60 }
61
62 void
63 BouncyCoin::draw(DrawingContext& context)
64 {
65   sprite->draw(context, position, LAYER_OBJECTS + 5);
66 }
67
68 //---------------------------------------------------------------------------
69
70 BrokenBrick::BrokenBrick(Sprite* nsprite,
71     const Vector& pos, const Vector& nmovement)
72   : sprite(new Sprite(*nsprite)), position(pos), movement(nmovement)
73 {
74   timer.start(.2);
75 }
76
77 BrokenBrick::~BrokenBrick()
78 {
79   delete sprite;
80 }
81
82 void
83 BrokenBrick::update(float elapsed_time)
84 {
85   position += movement * elapsed_time;
86
87   if (timer.check())
88     remove_me();
89 }
90
91 void
92 BrokenBrick::draw(DrawingContext& context)
93 {
94   sprite->draw_part(context,
95       Vector(systemRandom.rand(16), systemRandom.rand(16)), Vector(16, 16),
96       position, LAYER_OBJECTS + 1);
97 }
98
99 //---------------------------------------------------------------------------
100
101 FloatingText::FloatingText(const Vector& pos, const std::string& text_)
102   : position(pos), text(text_)
103 {
104   timer.start(.1);
105   position.x -= text.size() * 8;
106 }
107
108 FloatingText::FloatingText(const Vector& pos, int score)
109   : position(pos)
110 {
111   timer.start(.1);
112
113   // turn int into a string
114   char str[10];
115   snprintf(str, 10, "%d", score);
116   text = str;
117
118   position.x -= text.size() * 8;
119 }
120
121 void
122 FloatingText::update(float elapsed_time)
123 {
124   position.y -= 1.4 * elapsed_time;
125
126   if(timer.check())
127     remove_me();
128 }
129
130 #define FADING_TIME .350
131
132 void
133 FloatingText::draw(DrawingContext& context)
134 {
135   // make an alpha animation when disapearing
136   int alpha;
137   if(timer.get_timeleft() < FADING_TIME)
138     alpha = int(timer.get_timeleft() * 255 / FADING_TIME);
139   else
140     alpha = 255;
141
142   context.push_transform();
143   context.set_alpha(alpha);
144
145   context.draw_text(gold_text, text, position, LEFT_ALLIGN, LAYER_OBJECTS+1);
146
147   context.pop_transform();
148 }
149
150 Sprite *img_smoke_cloud = 0;
151
152 SmokeCloud::SmokeCloud(const Vector& pos)
153   : position(pos)
154 {
155   timer.start(.3);
156   sprite = sprite_manager->create("images/objects/particles/stomp.sprite");
157 }
158
159 SmokeCloud::~SmokeCloud()
160 {
161   delete sprite;
162 }
163
164 void
165 SmokeCloud::update(float elapsed_time)
166 {
167   position.y -= 120 * elapsed_time;
168
169   if(timer.check())
170     remove_me();
171 }
172
173 void
174 SmokeCloud::draw(DrawingContext& context)
175 {
176   sprite->draw(context, position, LAYER_OBJECTS+1);
177 }
178