src/object/lantern.cpp

Go to the documentation of this file.
00001 //  SuperTux - Lantern
00002 //  Copyright (C) 2006 Wolfgang Becker <uafr@gmx.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "object/lantern.hpp"
00018 
00019 #include "audio/sound_manager.hpp"
00020 #include "badguy/treewillowisp.hpp"
00021 #include "badguy/willowisp.hpp"
00022 #include "sprite/sprite.hpp"
00023 #include "sprite/sprite_manager.hpp"
00024 #include "supertux/object_factory.hpp"
00025 #include "util/reader.hpp"
00026 
00027 Lantern::Lantern(const Reader& reader) :
00028   Rock(reader, "images/objects/lantern/lantern.sprite"),
00029   lightcolor(1.0f, 1.0f, 1.0f),
00030   lightsprite()
00031 {
00032   //get color from lisp
00033   std::vector<float> vColor;
00034   reader.get("color", vColor);
00035   lightcolor = Color(vColor);
00036   lightsprite = sprite_manager->create("images/objects/lightmap_light/lightmap_light.sprite");
00037   lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
00038   updateColor();
00039   sound_manager->preload("sounds/willocatch.wav");
00040 }
00041 
00042 Lantern::Lantern(const Vector& pos) :
00043   Rock(pos, "images/objects/lantern/lantern.sprite"),
00044   lightcolor(0.0f, 0.0f, 0.0f),
00045   lightsprite()
00046 {
00047   lightsprite = sprite_manager->create("images/objects/lightmap_light/lightmap_light.sprite");
00048   lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
00049   updateColor();
00050   sound_manager->preload("sounds/willocatch.wav");
00051 }
00052 
00053 Lantern::~Lantern()
00054 {
00055 }
00056 
00057 void
00058 Lantern::updateColor(){
00059   lightsprite->set_color(lightcolor);
00060   //Turn lantern off if light is black
00061   if(lightcolor.red == 0 && lightcolor.green == 0 && lightcolor.blue == 0){
00062     sprite->set_action("off");
00063   } else {
00064     sprite->set_action("normal");
00065     sprite->set_color(lightcolor);
00066   }
00067 }
00068 
00069 void
00070 Lantern::draw(DrawingContext& context){
00071   //Draw the Sprite.
00072   MovingSprite::draw(context);
00073   //Let there be light.
00074   context.push_target();
00075   context.set_target(DrawingContext::LIGHTMAP);
00076 
00077   lightsprite->draw(context, get_bbox().get_middle(), 0);
00078 
00079   context.pop_target();
00080 }
00081 
00082 HitResponse Lantern::collision(GameObject& other, const CollisionHit& hit) {
00083   if (is_open()) {
00084     WillOWisp* wow = dynamic_cast<WillOWisp*>(&other);
00085     if (wow) {
00086       // collided with WillOWisp while grabbed and unlit
00087       sound_manager->play("sounds/willocatch.wav");
00088       lightcolor = Color(0,1,0);
00089       updateColor();
00090       wow->vanish();
00091     }
00092     TreeWillOWisp* twow = dynamic_cast<TreeWillOWisp*>(&other);
00093     if (twow) {
00094       // collided with TreeWillOWisp while grabbed and unlit
00095       sound_manager->play("sounds/willocatch.wav");
00096       lightcolor = twow->get_color();
00097       updateColor();
00098       twow->vanish();
00099     }
00100   }
00101   return Rock::collision(other, hit);
00102 }
00103 
00104 void
00105 Lantern::grab(MovingObject& object, const Vector& pos, Direction dir)
00106 {
00107   Rock::grab(object, pos, dir);
00108 
00109   // if lantern is not lit, draw it as opened
00110   if (is_open()) {
00111     sprite->set_action("off-open");
00112   }
00113 
00114 }
00115 
00116 void
00117 Lantern::ungrab(MovingObject& object, Direction dir)
00118 {
00119   // if lantern is not lit, it was drawn as opened while grabbed. Now draw it as closed again
00120   if (is_open()) {
00121     sprite->set_action("off");
00122   }
00123 
00124   Rock::ungrab(object, dir);
00125 }
00126   
00127 bool 
00128 Lantern::is_open()
00129 {
00130   return ((grabbed) && lightcolor.red == 0 && lightcolor.green == 0 && lightcolor.blue == 0);
00131 }
00132 
00133 /* EOF */

Generated on Mon Jun 9 03:38:19 2014 for SuperTux by  doxygen 1.5.1