Update CMake to 3.2.1 in .travis.yml
[supertux.git] / src / object / powerup.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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 "audio/sound_manager.hpp"
18 #include "math/random_generator.hpp"
19 #include "object/player.hpp"
20 #include "object/powerup.hpp"
21 #include "object/sprite_particle.hpp"
22 #include "scripting/level.hpp"
23 #include "sprite/sprite.hpp"
24 #include "sprite/sprite_manager.hpp"
25 #include "supertux/object_factory.hpp"
26 #include "supertux/sector.hpp"
27 #include "util/reader.hpp"
28
29 #include <sstream>
30
31 PowerUp::PowerUp(const Reader& lisp) :
32   MovingSprite(lisp, LAYER_OBJECTS, COLGROUP_MOVING),
33   physic(),
34   script(),
35   no_physics(),
36   light(0.0f,0.0f,0.0f),
37   lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-small.sprite"))
38 {
39   lisp.get("script", script);
40   no_physics = false;
41   lisp.get("disable-physics", no_physics);
42   physic.enable_gravity(true);
43   SoundManager::current()->preload("sounds/grow.ogg");
44   SoundManager::current()->preload("sounds/fire-flower.wav");
45   //set default light for glow effect for standard sprites
46   lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
47   lightsprite->set_color(Color(0.0f, 0.0f, 0.0f));
48   if (sprite_name == "images/powerups/egg/egg.sprite") {
49     lightsprite->set_color(Color(0.2f, 0.2f, 0.0f));
50   } else if (sprite_name == "images/powerups/fireflower/fireflower.sprite") {
51     lightsprite->set_color(Color(0.3f, 0.0f, 0.0f));
52   } else if (sprite_name == "images/powerups/iceflower/iceflower.sprite") {
53     lightsprite->set_color(Color(0.0f, 0.1f, 0.2f));
54   } else if (sprite_name == "images/powerups/airflower/airflower.sprite") {
55     lightsprite->set_color(Color(0.15f, 0.0f, 0.15f));
56   } else if (sprite_name == "images/powerups/earthflower/earthflower.sprite") {
57     lightsprite->set_color(Color(0.0f, 0.3f, 0.0f));
58   } else if (sprite_name == "images/powerups/star/star.sprite") {
59     lightsprite->set_color(Color(0.4f, 0.4f, 0.4f));
60   }
61
62 }
63
64 PowerUp::PowerUp(const Vector& pos, const std::string& sprite_name_) :
65   MovingSprite(pos, sprite_name_, LAYER_OBJECTS, COLGROUP_MOVING),
66   physic(),
67   script(),
68   no_physics(false),
69   light(0.0f,0.0f,0.0f),
70   lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-small.sprite"))
71 {
72   physic.enable_gravity(true);
73   SoundManager::current()->preload("sounds/grow.ogg");
74   SoundManager::current()->preload("sounds/fire-flower.wav");
75   //set default light for glow effect for standard sprites
76   lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
77   lightsprite->set_color(Color(0.0f, 0.0f, 0.0f));
78   if (sprite_name == "images/powerups/egg/egg.sprite") {
79     lightsprite->set_color(Color(0.2f, 0.2f, 0.0f));
80   } else if (sprite_name == "images/powerups/fireflower/fireflower.sprite") {
81     lightsprite->set_color(Color(0.3f, 0.0f, 0.0f));
82   } else if (sprite_name == "images/powerups/iceflower/iceflower.sprite") {
83     lightsprite->set_color(Color(0.0f, 0.1f, 0.2f));
84   } else if (sprite_name == "images/powerups/airflower/airflower.sprite") {
85     lightsprite->set_color(Color(0.15f, 0.0f, 0.15f));
86   } else if (sprite_name == "images/powerups/earthflower/earthflower.sprite") {
87     lightsprite->set_color(Color(0.0f, 0.3f, 0.0f));
88   } else if (sprite_name == "images/powerups/star/star.sprite") {
89     lightsprite->set_color(Color(0.4f, 0.4f, 0.4f));
90   }
91 }
92
93 void
94 PowerUp::collision_solid(const CollisionHit& hit)
95 {
96   if(hit.bottom) {
97     physic.set_velocity_y(0);
98   }
99   if(hit.right || hit.left) {
100     physic.set_velocity_x(-physic.get_velocity_x());
101   }
102 }
103
104 HitResponse
105 PowerUp::collision(GameObject& other, const CollisionHit&)
106 {
107   Player* player = dynamic_cast<Player*>(&other);
108   if(player == 0)
109     return FORCE_MOVE;
110
111   if (script != "") {
112     std::istringstream stream(script);
113     Sector::current()->run_script(stream, "powerup-script");
114     remove_me();
115     return ABORT_MOVE;
116   }
117
118   // some defaults if no script has been set
119   if (sprite_name == "images/powerups/egg/egg.sprite") {
120     if(!player->add_bonus(GROWUP_BONUS, true))
121       return FORCE_MOVE;
122     SoundManager::current()->play("sounds/grow.ogg");
123   } else if (sprite_name == "images/powerups/fireflower/fireflower.sprite") {
124     if(!player->add_bonus(FIRE_BONUS, true))
125       return FORCE_MOVE;
126     SoundManager::current()->play("sounds/fire-flower.wav");
127   } else if (sprite_name == "images/powerups/iceflower/iceflower.sprite") {
128     if(!player->add_bonus(ICE_BONUS, true))
129       return FORCE_MOVE;
130     SoundManager::current()->play("sounds/fire-flower.wav");
131   } else if (sprite_name == "images/powerups/airflower/airflower.sprite") {
132     if(!player->add_bonus(AIR_BONUS, true))
133       return FORCE_MOVE;
134     SoundManager::current()->play("sounds/fire-flower.wav");
135   } else if (sprite_name == "images/powerups/earthflower/earthflower.sprite") {
136     if(!player->add_bonus(EARTH_BONUS, true))
137       return FORCE_MOVE;
138     SoundManager::current()->play("sounds/fire-flower.wav");
139   } else if (sprite_name == "images/powerups/star/star.sprite") {
140     player->make_invincible();
141   } else if (sprite_name == "images/powerups/1up/1up.sprite") {
142     player->get_status()->add_coins(100);
143   } else if (sprite_name == "images/powerups/potions/red-potion.sprite") {
144     scripting::Level_flip_vertically();
145   }
146
147   remove_me();
148   return ABORT_MOVE;
149 }
150
151 void
152 PowerUp::update(float elapsed_time)
153 {
154   if (!no_physics)
155     movement = physic.get_movement(elapsed_time);
156   //Stars sparkle when close to Tux
157   if (sprite_name == "images/powerups/star/star.sprite"){
158     Player* player = Sector::current()->get_nearest_player (this->get_bbox ());
159     if (player) {
160       float disp_x = player->get_bbox().p1.x - bbox.p1.x;
161       float disp_y = player->get_bbox().p1.y - bbox.p1.y;
162       if (disp_x*disp_x + disp_y*disp_y <= 256*256)
163       {
164         if (graphicsRandom.rand(0, 2) == 0) {
165           float px = graphicsRandom.randf(bbox.p1.x+0, bbox.p2.x-0);
166           float py = graphicsRandom.randf(bbox.p1.y+0, bbox.p2.y-0);
167           Vector ppos = Vector(px, py);
168           Vector pspeed = Vector(0, 0);
169           Vector paccel = Vector(0, 0);
170           Sector::current()->add_object(std::make_shared<SpriteParticle>(
171                                           "images/objects/particles/sparkle.sprite",
172                                           // draw bright sparkles when very close to Tux, dark sparkles when slightly further
173                                           (disp_x*disp_x + disp_y*disp_y <= 128*128) ?
174                                           // make every other a longer sparkle to make trail a bit fuzzy
175                                           (size_t(game_time*20)%2) ? "small" : "medium" : "dark",
176                                           ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
177         }
178       }
179     }
180   }
181 }
182
183 void
184 PowerUp::draw(DrawingContext& context){
185   //Draw the Sprite.
186   sprite->draw(context, get_pos(), layer);
187   //Draw light when dark for defaults
188   context.get_light( get_bbox().get_middle(), &light );
189   if (light.red + light.green + light.blue < 3.0){
190     //Stars are brighter
191     if (sprite_name == "images/powerups/star/star.sprite") {
192       sprite->draw(context, get_pos(), layer);
193     }
194     context.push_target();
195     context.set_target(DrawingContext::LIGHTMAP);
196     lightsprite->draw(context, get_bbox().get_middle(), 0);
197     context.pop_target();
198   }
199 }
200 /* EOF */