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 <algorithm>
20
21#include "audio/sound_manager.hpp"
22#include "badguy/treewillowisp.hpp"
23#include "badguy/willowisp.hpp"
24#include "editor/editor.hpp"
25#include "sprite/sprite.hpp"
26#include "sprite/sprite_manager.hpp"
27#include "util/reader_mapping.hpp"
28
29Lantern::Lantern(const ReaderMapping& reader) :
30 Rock(reader, "images/objects/lantern/lantern.sprite"),
31 lightcolor(1.0f, 1.0f, 1.0f),
32 lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light.sprite"))
33{
34 std::vector<float> vColor;
35 if (reader.get("color", vColor)) {
36 lightcolor = Color(vColor);
37 } else {
38 if (!Editor::is_active()) {
39 lightcolor = Color(0, 0, 0);
40 }
41 }
42 lightsprite->set_blend(Blend::ADD);
43 updateColor();
44 SoundManager::current()->preload("sounds/willocatch.wav");
45}
46
47Lantern::Lantern(const Vector& pos) :
48 Rock(pos, "images/objects/lantern/lantern.sprite"),
49 lightcolor(0.0f, 0.0f, 0.0f),
50 lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light.sprite"))
51{
52 lightsprite->set_blend(Blend::ADD);
53 updateColor();
54 SoundManager::current()->preload("sounds/willocatch.wav");
55}
56
57ObjectSettings
58Lantern::get_settings()
59{
60 ObjectSettings result = Rock::get_settings();
61
62 result.add_color(_("Color"), &lightcolor, "color", Color::WHITE);
63
64 result.reorder({"color", "name", "x", "y"});
65
66 return result;
67}
68
69void
70Lantern::after_editor_set()
71{
72 updateColor();
73}
74
75void
76Lantern::updateColor(){
77 lightsprite->set_color(lightcolor);
78 //Turn lantern off if light is black
79 if (lightcolor.red == 0 && lightcolor.green == 0 && lightcolor.blue == 0){
80 m_sprite->set_action("off");
81 m_sprite->set_color(Color(1.0f, 1.0f, 1.0f));
82 } else {
83 m_sprite->set_action("normal");
84 m_sprite->set_color(lightcolor);
85 }
86}
87
88void
89Lantern::draw(DrawingContext& context){
90 //Draw the Sprite.
91 MovingSprite::draw(context);
92 //Let there be light.
93 lightsprite->draw(context.light(), m_col.m_bbox.get_middle(), 0);
94}
95
96HitResponse Lantern::collision(GameObject& other, const CollisionHit& hit) {
97 if (is_open()) {
98 WillOWisp* wow = dynamic_cast<WillOWisp*>(&other);
99 if (wow) {
100 // collided with WillOWisp while grabbed and unlit
101 SoundManager::current()->play("sounds/willocatch.wav");
102 lightcolor = Color(0,1,0);
103 updateColor();
104 wow->vanish();
105 }
106 TreeWillOWisp* twow = dynamic_cast<TreeWillOWisp*>(&other);
107 if (twow) {
108 // collided with TreeWillOWisp while grabbed and unlit
109 SoundManager::current()->play("sounds/willocatch.wav");
110 lightcolor = twow->get_color();
111 updateColor();
112 twow->vanish();
113 }
114 }
115 return Rock::collision(other, hit);
116}
117
118void
119Lantern::grab(MovingObject& object, const Vector& pos, Direction dir)
120{
121 Rock::grab(object, pos, dir);
122
123 // if lantern is not lit, draw it as opened
124 if (is_open()) {
125 m_sprite->set_action("off-open");
126 }
127
128}
129
130void
131Lantern::ungrab(MovingObject& object, Direction dir)
132{
133 // if lantern is not lit, it was drawn as opened while grabbed. Now draw it as closed again
134 if (is_open()) {
135 m_sprite->set_action("off");
136 }
137
138 Rock::ungrab(object, dir);
139}
140
141bool
142Lantern::is_open() const
143{
144 return (is_grabbed() && lightcolor.red == 0 && lightcolor.green == 0 && lightcolor.blue == 0);
145}
146
147void
148Lantern::add_color(const Color& c)
149{
150 lightcolor.red = std::min(1.0f, lightcolor.red + c.red);
151 lightcolor.green = std::min(1.0f, lightcolor.green + c.green);
152 lightcolor.blue = std::min(1.0f, lightcolor.blue + c.blue);
153 lightcolor.alpha = std::min(1.0f, lightcolor.alpha + c.alpha);
154 updateColor();
155}
156
157/* EOF */
158