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)
31 std::vector<float> vColor;
32 reader.get("color", vColor);
33 lightcolor = Color(vColor);
34 lightsprite = sprite_manager->create("images/objects/lightmap_light/lightmap_light.sprite");
35 lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
37 sound_manager->preload("sounds/willocatch.wav");
40 Lantern::Lantern(const Vector& pos)
41 : Rock(pos, "images/objects/lantern/lantern.sprite"),
42 lightcolor(0.0f, 0.0f, 0.0f)
44 lightsprite = sprite_manager->create("images/objects/lightmap_light/lightmap_light.sprite");
45 lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
47 sound_manager->preload("sounds/willocatch.wav");
55 Lantern::updateColor(){
56 lightsprite->set_color(lightcolor);
57 //Turn lantern off if light is black
58 if(lightcolor.red == 0 && lightcolor.green == 0 && lightcolor.blue == 0){
59 sprite->set_action("off");
61 sprite->set_action("normal");
62 sprite->set_color(lightcolor);
67 Lantern::draw(DrawingContext& context){
69 MovingSprite::draw(context);
71 context.push_target();
72 context.set_target(DrawingContext::LIGHTMAP);
74 lightsprite->draw(context, get_bbox().get_middle(), 0);
79 HitResponse Lantern::collision(GameObject& other, const CollisionHit& hit) {
81 WillOWisp* wow = dynamic_cast<WillOWisp*>(&other);
83 // collided with WillOWisp while grabbed and unlit
84 sound_manager->play("sounds/willocatch.wav");
85 lightcolor = Color(0,1,0);
89 TreeWillOWisp* twow = dynamic_cast<TreeWillOWisp*>(&other);
91 // collided with TreeWillOWisp while grabbed and unlit
92 sound_manager->play("sounds/willocatch.wav");
93 lightcolor = twow->get_color();
98 return Rock::collision(other, hit);
102 Lantern::grab(MovingObject& object, const Vector& pos, Direction dir)
104 Rock::grab(object, pos, dir);
106 // if lantern is not lit, draw it as opened
108 sprite->set_action("off-open");
114 Lantern::ungrab(MovingObject& object, Direction dir)
116 // if lantern is not lit, it was drawn as opened while grabbed. Now draw it as closed again
118 sprite->set_action("off");
121 Rock::ungrab(object, dir);
127 return ((grabbed) && lightcolor.red == 0 && lightcolor.green == 0 && lightcolor.blue == 0);
130 IMPLEMENT_FACTORY(Lantern, "lantern");