New sounds: cracking, icecrash, pop, and sizzle.
[supertux.git] / src / badguy / livefire.cpp
1 //  SuperTux badguy - walking flame that glows
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 "badguy/livefire.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "object/player.hpp"
21 #include "object/sprite_particle.hpp"
22 #include "sprite/sprite.hpp"
23 #include "supertux/object_factory.hpp"
24 #include "supertux/sector.hpp"
25
26 static const float WALKSPEED = 80;
27 static const float MAXDROPHEIGHT = 20;
28
29 LiveFire::LiveFire(const Reader& reader) :
30   WalkingBadguy(reader, "images/creatures/livefire/livefire.sprite", "left", "right"),
31   lightsprite(sprite_manager->create("images/objects/lightmap_light/lightmap_light-medium.sprite")),
32   state(STATE_WALKING)  
33 {
34   walk_speed = WALKSPEED;
35   max_drop_height = MAXDROPHEIGHT;
36   lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
37   lightsprite->set_color(Color(1.0f, 0.9f, 0.8f));
38   death_sound = "sounds/fall.wav";
39 }
40
41 void
42 LiveFire::collision_solid(const CollisionHit& hit)
43 {
44   if(state != STATE_WALKING) {
45     BadGuy::collision_solid(hit);
46     return;
47   }
48   WalkingBadguy::collision_solid(hit);
49 }
50
51 HitResponse
52 LiveFire::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
53 {
54   if(state != STATE_WALKING) {
55     return BadGuy::collision_badguy(badguy, hit);
56   }
57   return WalkingBadguy::collision_badguy(badguy, hit);
58 }
59
60 void
61 LiveFire::active_update(float elapsed_time) {
62
63   // Remove when extinguish animation is done
64   if((sprite->get_action() == "extinguish-left" || sprite->get_action() == "extinguish-right" )
65     && sprite->animation_done()) remove_me();
66
67   if(state == STATE_WALKING) {
68     WalkingBadguy::active_update(elapsed_time);
69     return;
70   }
71
72   if(state == STATE_SLEEPING && get_group() == COLGROUP_MOVING) {
73
74     Player* player = this->get_nearest_player();
75     if (player) {
76       Rectf mb = this->get_bbox();
77       Rectf pb = player->get_bbox();
78
79       bool inReach_left = (pb.p2.x >= mb.p2.x-((dir == LEFT) ? 256 : 0));
80       bool inReach_right = (pb.p1.x <= mb.p1.x+((dir == RIGHT) ? 256 : 0));
81       bool inReach_top = (pb.p2.y >= mb.p1.y);
82       bool inReach_bottom = (pb.p1.y <= mb.p2.y);
83
84       if (inReach_left && inReach_right && inReach_top && inReach_bottom) {
85         // wake up
86         sprite->set_action(dir == LEFT ? "waking-left" : "waking-right", 1);
87         state = STATE_WAKING;
88       }
89     }
90   }
91   else if(state == STATE_WAKING) {
92     if(sprite->animation_done()) {
93       // start walking
94       state = STATE_WALKING;
95       WalkingBadguy::initialize();
96     }
97   }
98
99   BadGuy::active_update(elapsed_time);
100 }
101
102 void
103 LiveFire::draw(DrawingContext& context)
104 {
105   //Draw the Sprite.
106   sprite->draw(context, get_pos(), LAYER_OBJECTS);
107   //Draw the light
108   context.push_target();
109   context.set_target(DrawingContext::LIGHTMAP);
110   lightsprite->draw(context, get_bbox().get_middle(), 0);
111   context.pop_target();
112 }
113
114 void
115 LiveFire::freeze()
116 {
117   // attempting to freeze a flame causes it to go out
118   death_sound = "sounds/sizzle.ogg";
119   kill_fall();
120 }
121
122 bool
123 LiveFire::is_freezable() const
124 {
125   return true;
126 }
127
128 bool
129 LiveFire::is_flammable() const
130 {
131   return false;
132 }
133
134 void
135 LiveFire::kill_fall()
136 {
137   sound_manager->play(death_sound, get_pos());
138   // throw a puff of smoke
139   Vector ppos = bbox.get_middle();
140   Vector pspeed = Vector(0, -150);
141   Vector paccel = Vector(0,0);
142   Sector::current()->add_object(new SpriteParticle("images/objects/particles/smoke.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_BACKGROUNDTILES+2));
143   // extinguish the flame
144   sprite->set_action(dir == LEFT ? "extinguish-left" : "extinguish-right", 1);
145   physic.set_velocity_y(0);
146   physic.set_acceleration_y(0);
147   physic.enable_gravity(false);
148   lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
149   lightsprite->set_color(Color(0.5f, 0.4f, 0.3f));
150   set_group(COLGROUP_DISABLED);
151
152   // start dead-script
153   run_dead_script();
154 }
155
156 /* The following defines a sleeping version */
157
158 LiveFireAsleep::LiveFireAsleep(const Reader& reader) :
159   LiveFire(reader)
160 {
161   state = STATE_SLEEPING;
162 }
163
164 void
165 LiveFireAsleep::initialize()
166 {
167   physic.set_velocity_x(0);
168   sprite->set_action(dir == LEFT ? "sleeping-left" : "sleeping-right");
169 }
170
171 /* The following defines a dormant version that never wakes */
172 LiveFireDormant::LiveFireDormant(const Reader& reader) :
173   LiveFire(reader)
174 {
175   walk_speed = 0;
176   state = STATE_DORMANT;
177 }
178
179 void
180 LiveFireDormant::initialize()
181 {
182   physic.set_velocity_x(0);
183   sprite->set_action(dir == LEFT ? "sleeping-left" : "sleeping-right");
184 }
185
186 /* EOF */