Another round of fixes for squirrel
[supertux.git] / src / object / lantern.cpp
1 //  SuperTux - Lantern
2 //  Copyright (C) 2006 Wolfgang Becker <uafr@gmx.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 "object/lantern.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "badguy/treewillowisp.hpp"
21 #include "badguy/willowisp.hpp"
22 #include "sprite/sprite.hpp"
23 #include "sprite/sprite_manager.hpp"
24 #include "supertux/object_factory.hpp"
25 #include "util/reader.hpp"
26
27 Lantern::Lantern(const Reader& reader) :
28   Rock(reader, "images/objects/lantern/lantern.sprite"),
29   lightcolor(1.0f, 1.0f, 1.0f),
30   lightsprite()
31 {
32   //get color from lisp
33   std::vector<float> vColor;
34   reader.get("color", vColor);
35   lightcolor = Color(vColor);
36   lightsprite = SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light.sprite");
37   lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
38   updateColor();
39   SoundManager::current()->preload("sounds/willocatch.wav");
40 }
41
42 Lantern::Lantern(const Vector& pos) :
43   Rock(pos, "images/objects/lantern/lantern.sprite"),
44   lightcolor(0.0f, 0.0f, 0.0f),
45   lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light.sprite"))
46 {
47   lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
48   updateColor();
49   SoundManager::current()->preload("sounds/willocatch.wav");
50 }
51
52 Lantern::~Lantern()
53 {
54 }
55
56 void
57 Lantern::updateColor(){
58   lightsprite->set_color(lightcolor);
59   //Turn lantern off if light is black
60   if(lightcolor.red == 0 && lightcolor.green == 0 && lightcolor.blue == 0){
61     sprite->set_action("off");
62   } else {
63     sprite->set_action("normal");
64     sprite->set_color(lightcolor);
65   }
66 }
67
68 void
69 Lantern::draw(DrawingContext& context){
70   //Draw the Sprite.
71   MovingSprite::draw(context);
72   //Let there be light.
73   context.push_target();
74   context.set_target(DrawingContext::LIGHTMAP);
75
76   lightsprite->draw(context, get_bbox().get_middle(), 0);
77
78   context.pop_target();
79 }
80
81 HitResponse Lantern::collision(GameObject& other, const CollisionHit& hit) {
82   if (is_open()) {
83     WillOWisp* wow = dynamic_cast<WillOWisp*>(&other);
84     if (wow) {
85       // collided with WillOWisp while grabbed and unlit
86       SoundManager::current()->play("sounds/willocatch.wav");
87       lightcolor = Color(0,1,0);
88       updateColor();
89       wow->vanish();
90     }
91     TreeWillOWisp* twow = dynamic_cast<TreeWillOWisp*>(&other);
92     if (twow) {
93       // collided with TreeWillOWisp while grabbed and unlit
94       SoundManager::current()->play("sounds/willocatch.wav");
95       lightcolor = twow->get_color();
96       updateColor();
97       twow->vanish();
98     }
99   }
100   return Rock::collision(other, hit);
101 }
102
103 void
104 Lantern::grab(MovingObject& object, const Vector& pos, Direction dir)
105 {
106   Rock::grab(object, pos, dir);
107
108   // if lantern is not lit, draw it as opened
109   if (is_open()) {
110     sprite->set_action("off-open");
111   }
112
113 }
114
115 void
116 Lantern::ungrab(MovingObject& object, Direction dir)
117 {
118   // if lantern is not lit, it was drawn as opened while grabbed. Now draw it as closed again
119   if (is_open()) {
120     sprite->set_action("off");
121   }
122
123   Rock::ungrab(object, dir);
124 }
125
126 bool
127 Lantern::is_open()
128 {
129   return ((grabbed) && lightcolor.red == 0 && lightcolor.green == 0 && lightcolor.blue == 0);
130 }
131
132 /* EOF */