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