1// SuperTux
2// Copyright (C) 2013 LMH <lmh.0013@gmail.com>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17#include "object/coin_rain.hpp"
18
19#include "math/random.hpp"
20#include "object/coin.hpp"
21#include "sprite/sprite.hpp"
22#include "sprite/sprite_manager.hpp"
23#include "supertux/sector.hpp"
24
25static const float DROP_TIME = .1f; // time duration between "drops" of coin rain
26
27CoinRain::CoinRain(const Vector& pos, bool emerge) :
28 sprite(SpriteManager::current()->create("images/objects/coin/coin.sprite")),
29 position(pos),
30 emerge_distance(0),
31 timer(),
32 counter(0),
33 drop(0)
34{
35 if (emerge) {
36 emerge_distance = static_cast<float>(sprite->get_height());
37 }
38}
39
40void
41CoinRain::update(float dt_sec)
42{
43 // first a single (untouchable) coin flies up above the sector
44 if (position.y > -32){
45 float dist = -500 * dt_sec;
46 position.y += dist;
47 emerge_distance += dist;
48 } // then the first collectable coin drops from one of ten random positions
49 else if (counter==0){
50 drop = gameRandom.rand(10);
51 Sector::get().add<HeavyCoin>(Vector(position.x + 32.0f * static_cast<float>((drop < 5) ? -drop - 1 : drop - 4), -32.0f),
52 Vector(0, 0));
53 counter++;
54 timer.start(DROP_TIME);
55 } // finally the remainder of the coins drop in a determined but appears to be a random order
56 else if (timer.check()){
57 if (counter<10){
58 drop += 7;
59 if (drop >= 10) drop -=10;
60 Sector::get().add<HeavyCoin>(Vector(position.x + 32.0f * static_cast<float>((drop < 5) ? -drop - 1 : drop - 4), -32.0f),
61 Vector(0, 0));
62 counter++;
63 timer.start(DROP_TIME);
64 } else {
65 remove_me();
66 }
67 }
68}
69
70void
71CoinRain::draw(DrawingContext& context)
72{
73 int layer;
74 if (emerge_distance > 0) {
75 layer = LAYER_OBJECTS - 5;
76 } else {
77 layer = LAYER_OBJECTS + 5;
78 }
79 sprite->draw(context.color(), position, layer);
80}
81
82/* EOF */
83