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