d061f676f3def7d51db4edb87588a63c7fa3b12f
[supertux.git] / src / badguy / flame.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 "badguy/flame.hpp"
18
19 #include <math.h>
20
21 #include "audio/sound_manager.hpp"
22 #include "math/random_generator.hpp"
23 #include "sprite/sprite.hpp"
24 #include "sprite/sprite_manager.hpp"
25 #include "object/sprite_particle.hpp"
26 #include "supertux/object_factory.hpp"
27 #include "supertux/sector.hpp"
28 #include "util/reader.hpp"
29
30 static const std::string FLAME_SOUND = "sounds/flame.wav";
31
32 Flame::Flame(const Reader& reader) :
33   BadGuy(reader, "images/creatures/flame/flame.sprite", LAYER_FLOATINGOBJECTS), 
34   angle(0), 
35   radius(100), 
36   speed(2),
37   fading(false),
38   light(0.0f,0.0f,0.0f),
39   lightsprite(sprite_manager->create("images/objects/lightmap_light/lightmap_light-small.sprite")),
40   sound_source()
41 {
42   reader.get("radius", radius);
43   reader.get("speed", speed);
44   bbox.set_pos(Vector(start_position.x + cos(angle) * radius,
45                       start_position.y + sin(angle) * radius));
46   countMe = false;
47   sound_manager->preload(FLAME_SOUND);
48
49   set_colgroup_active(COLGROUP_TOUCHABLE);
50   
51   lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
52   lightsprite->set_color(Color(0.21f, 0.13f, 0.08f));
53 }
54
55 void
56 Flame::active_update(float elapsed_time)
57 {
58   angle = fmodf(angle + elapsed_time * speed, (float) (2*M_PI));
59   Vector newpos(start_position.x + cos(angle) * radius,
60                 start_position.y + sin(angle) * radius);
61   movement = newpos - get_pos();
62
63   sound_source->set_position(get_pos());
64   
65   if(fading)
66     if (sprite->animation_done()) remove_me();
67 }
68
69 void
70 Flame::draw(DrawingContext& context)
71 {
72   //Draw the Sprite.
73   sprite->draw(context, get_pos(), LAYER_OBJECTS);
74   //Draw the light if dark
75   if(true){
76     context.get_light( get_bbox().get_middle(), &light );
77     if (light.red + light.green < 2.0){
78       context.push_target();
79       context.set_target(DrawingContext::LIGHTMAP);
80       sprite->draw(context, get_pos(), layer);
81       lightsprite->draw(context, get_bbox().get_middle(), 0);
82       context.pop_target();
83     }
84   }
85 }
86
87 void
88 Flame::activate()
89 {
90   sound_source.reset(sound_manager->create_sound_source(FLAME_SOUND));
91   sound_source->set_position(get_pos());
92   sound_source->set_looping(true);
93   sound_source->set_gain(2.0);
94   sound_source->set_reference_distance(32);
95   sound_source->play();
96 }
97
98 void
99 Flame::deactivate()
100 {
101   sound_source.reset();
102 }
103
104
105 void
106 Flame::kill_fall()
107 {
108 }
109
110 void
111 Flame::freeze()
112 {
113   //TODO: get unique death sound
114   sound_manager->play("sounds/fizz.wav", get_pos());
115   sprite->set_action("fade", 1);
116   Vector ppos = bbox.get_middle();
117   Vector pspeed = Vector(0, -150);
118   Vector paccel = Vector(0,0);
119   Sector::current()->add_object(new SpriteParticle("images/objects/particles/smoke.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_BACKGROUNDTILES+2));
120   fading = true;
121   
122   // start dead-script
123   run_dead_script();
124 }
125
126 bool
127 Flame::is_freezable() const
128 {
129   return true;
130 }
131
132 /* EOF */