2 // Copyright (C) 2006 Wolfgang Becker <uafr@gmx.de>
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.
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.
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/>.
17 #include "object/lantern.hpp"
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"
26 Lantern::Lantern(const Reader& reader) :
27 Rock(reader, "images/objects/lantern/lantern.sprite"),
28 lightcolor(1.0f, 1.0f, 1.0f),
32 std::vector<float> vColor;
33 reader.get("color", vColor);
34 lightcolor = Color(vColor);
35 lightsprite = sprite_manager->create("images/objects/lightmap_light/lightmap_light.sprite");
36 lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
38 sound_manager->preload("sounds/willocatch.wav");
41 Lantern::Lantern(const Vector& pos) :
42 Rock(pos, "images/objects/lantern/lantern.sprite"),
43 lightcolor(0.0f, 0.0f, 0.0f),
46 lightsprite = sprite_manager->create("images/objects/lightmap_light/lightmap_light.sprite");
47 lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
49 sound_manager->preload("sounds/willocatch.wav");
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");
63 sprite->set_action("normal");
64 sprite->set_color(lightcolor);
69 Lantern::draw(DrawingContext& context){
71 MovingSprite::draw(context);
73 context.push_target();
74 context.set_target(DrawingContext::LIGHTMAP);
76 lightsprite->draw(context, get_bbox().get_middle(), 0);
81 HitResponse Lantern::collision(GameObject& other, const CollisionHit& hit) {
83 WillOWisp* wow = dynamic_cast<WillOWisp*>(&other);
85 // collided with WillOWisp while grabbed and unlit
86 sound_manager->play("sounds/willocatch.wav");
87 lightcolor = Color(0,1,0);
91 TreeWillOWisp* twow = dynamic_cast<TreeWillOWisp*>(&other);
93 // collided with TreeWillOWisp while grabbed and unlit
94 sound_manager->play("sounds/willocatch.wav");
95 lightcolor = twow->get_color();
100 return Rock::collision(other, hit);
104 Lantern::grab(MovingObject& object, const Vector& pos, Direction dir)
106 Rock::grab(object, pos, dir);
108 // if lantern is not lit, draw it as opened
110 sprite->set_action("off-open");
116 Lantern::ungrab(MovingObject& object, Direction dir)
118 // if lantern is not lit, it was drawn as opened while grabbed. Now draw it as closed again
120 sprite->set_action("off");
123 Rock::ungrab(object, dir);
129 return ((grabbed) && lightcolor.red == 0 && lightcolor.green == 0 && lightcolor.blue == 0);
132 IMPLEMENT_FACTORY(Lantern, "lantern");