Renamed walkingflame to livefire including sleeping and dormant versions
[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 }
39
40 void
41 LiveFire::collision_solid(const CollisionHit& hit)
42 {
43   if(state != STATE_WALKING) {
44     BadGuy::collision_solid(hit);
45     return;
46   }
47   WalkingBadguy::collision_solid(hit);
48 }
49
50 HitResponse
51 LiveFire::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
52 {
53   if(state != STATE_WALKING) {
54     return BadGuy::collision_badguy(badguy, hit);
55   }
56   return WalkingBadguy::collision_badguy(badguy, hit);
57 }
58
59 void
60 LiveFire::active_update(float elapsed_time) {
61
62   if(state == STATE_WALKING) {
63     WalkingBadguy::active_update(elapsed_time);
64     return;
65   }
66
67   if(state == STATE_SLEEPING) {
68
69     Player* player = this->get_nearest_player();
70     if (player) {
71       Rectf mb = this->get_bbox();
72       Rectf pb = player->get_bbox();
73
74       bool inReach_left = (pb.p2.x >= mb.p2.x-((dir == LEFT) ? 256 : 0));
75       bool inReach_right = (pb.p1.x <= mb.p1.x+((dir == RIGHT) ? 256 : 0));
76       bool inReach_top = (pb.p2.y >= mb.p1.y);
77       bool inReach_bottom = (pb.p1.y <= mb.p2.y);
78
79       if (inReach_left && inReach_right && inReach_top && inReach_bottom) {
80         // wake up
81         sprite->set_action(dir == LEFT ? "waking-left" : "waking-right", 1);
82         state = STATE_WAKING;
83       }
84     }
85
86     BadGuy::active_update(elapsed_time);
87   }
88
89   if(state == STATE_WAKING) {
90     if(sprite->animation_done()) {
91       // start walking
92       state = STATE_WALKING;
93       WalkingBadguy::initialize();
94     }
95
96     BadGuy::active_update(elapsed_time);
97   }
98 }
99
100 void
101 LiveFire::draw(DrawingContext& context)
102 {
103   //Draw the Sprite.
104   sprite->draw(context, get_pos(), LAYER_OBJECTS);
105   //Draw the light
106   context.push_target();
107   context.set_target(DrawingContext::LIGHTMAP);
108   lightsprite->draw(context, get_bbox().get_middle(), 0);
109   context.pop_target();
110 }
111
112 void
113 LiveFire::freeze()
114 {
115   // attempting to freeze a flame causes it to go out
116   kill_fall();
117 }
118
119 bool
120 LiveFire::is_freezable() const
121 {
122   return true;
123 }
124
125 bool
126 LiveFire::is_flammable() const
127 {
128   return false;
129 }
130
131 void
132 LiveFire::kill_fall()
133 {
134   //TODO: get unique sound for ice-fire encounters
135   sound_manager->play("sounds/fall.wav", get_pos());
136   // throw a puff of smoke
137   Vector ppos = bbox.get_middle();
138   Vector pspeed = Vector(0, -150);
139   Vector paccel = Vector(0,0);
140   Sector::current()->add_object(new SpriteParticle("images/objects/particles/smoke.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_BACKGROUNDTILES+2));
141   // extinguish the flame
142   sprite->set_action(dir == LEFT ? "extinguish-left" : "extinguish-right");
143   physic.set_velocity_y(0);
144   physic.set_acceleration_y(0);
145   physic.enable_gravity(false);
146   set_state(STATE_SQUISHED); // used to nullify any threat and remove
147
148   // start dead-script
149   run_dead_script();
150 }
151
152 /* The following defines a sleeping version */
153
154 LiveFireAsleep::LiveFireAsleep(const Reader& reader) :
155   LiveFire(reader)
156 {
157   state = STATE_SLEEPING;
158 }
159
160 void
161 LiveFireAsleep::initialize()
162 {
163   physic.set_velocity_x(0);
164   sprite->set_action(dir == LEFT ? "sleeping-left" : "sleeping-right");
165 }
166
167 /* The following defines a dormant version that never wakes */
168 LiveFireDormant::LiveFireDormant(const Reader& reader) :
169   LiveFire(reader)
170 {
171   walk_speed = 0;
172   state = STATE_DORMANT;
173 }
174
175 void
176 LiveFireDormant::initialize()
177 {
178   physic.set_velocity_x(0);
179   sprite->set_action(dir == LEFT ? "sleeping-left" : "sleeping-right");
180 }
181
182 /* EOF */