b041fb89de7b9d64119561fe85a3369068a0c67a
[supertux.git] / src / object / lantern.cpp
1 //  $Id$
2 //
3 //  SuperTux - Lantern
4 //  Copyright (C) 2006 Wolfgang Becker <uafr@gmx.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include "lantern.hpp"
23
24 #include "sprite/sprite_manager.hpp"
25 #include "object_factory.hpp"
26 #include "badguy/willowisp.hpp"
27 #include "badguy/treewillowisp.hpp"
28 #include "audio/sound_manager.hpp"
29 #include "sprite/sprite.hpp"
30 #include "video/drawing_context.hpp"
31
32 Lantern::Lantern(const lisp::Lisp& reader)
33   : Rock(reader, "images/objects/lantern/lantern.sprite"),
34     lightcolor(1.0f, 1.0f, 1.0f)
35 {
36   //get color from lisp
37   std::vector<float> vColor;
38   reader.get("color", vColor);
39   lightcolor = Color(vColor);
40   lightsprite = sprite_manager->create("images/objects/lightmap_light/lightmap_light.sprite");
41   lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
42   updateColor();
43   sound_manager->preload("sounds/willocatch.wav");
44 }
45
46 Lantern::Lantern(const Vector& pos)
47   : Rock(pos, "images/objects/lantern/lantern.sprite"),
48     lightcolor(0.0f, 0.0f, 0.0f)
49 {
50   lightsprite = sprite_manager->create("images/objects/lightmap_light/lightmap_light.sprite");
51   lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
52   updateColor();
53   sound_manager->preload("sounds/willocatch.wav");
54 }
55
56 Lantern::~Lantern()
57 {
58   delete lightsprite;
59 }
60
61 void
62 Lantern::updateColor(){
63   lightsprite->set_color(lightcolor);
64   //Turn lantern off if light is black
65   if(lightcolor.red == 0 && lightcolor.green == 0 && lightcolor.blue == 0){
66      sprite->set_action("off");
67   } else {
68      sprite->set_action("normal");
69      sprite->set_color(lightcolor);
70   }
71 }
72
73 void
74 Lantern::draw(DrawingContext& context){
75   //Draw the Sprite.
76   MovingSprite::draw(context);
77   //Let there be light.
78   context.push_target();
79   context.set_target(DrawingContext::LIGHTMAP);
80
81   lightsprite->draw(context, get_bbox().get_middle(), 0);
82
83   context.pop_target();
84 }
85
86 HitResponse Lantern::collision(GameObject& other, const CollisionHit& hit) {
87   if (is_open()) {
88     WillOWisp* wow = dynamic_cast<WillOWisp*>(&other);
89     if (wow) {
90       // collided with WillOWisp while grabbed and unlit
91       sound_manager->play("sounds/willocatch.wav");
92       lightcolor = Color(0,1,0);
93       updateColor();
94       wow->vanish();
95     }
96     TreeWillOWisp* twow = dynamic_cast<TreeWillOWisp*>(&other);
97     if (twow) {
98       // collided with TreeWillOWisp while grabbed and unlit
99       sound_manager->play("sounds/willocatch.wav");
100       lightcolor = twow->get_color();
101       updateColor();
102       twow->vanish();
103     }
104   }
105   return Rock::collision(other, hit);
106 }
107
108 void
109 Lantern::grab(MovingObject& object, const Vector& pos, Direction dir)
110 {
111   Rock::grab(object, pos, dir);
112
113   // if lantern is not lit, draw it as opened
114   if (is_open()) {
115     sprite->set_action("off-open");
116   }
117
118 }
119
120 void
121 Lantern::ungrab(MovingObject& object, Direction dir)
122 {
123   // if lantern is not lit, it was drawn as opened while grabbed. Now draw it as closed again
124   if (is_open()) {
125     sprite->set_action("off");
126   }
127
128   Rock::ungrab(object, dir);
129 }
130   
131 bool 
132 Lantern::is_open()
133 {
134   return ((grabbed) && lightcolor.red == 0 && lightcolor.green == 0 && lightcolor.blue == 0);
135 }
136
137 IMPLEMENT_FACTORY(Lantern, "lantern");
138