Added torch code
[supertux.git] / src / object / torch.cpp
1 //  SuperTux
2 //  Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
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/torch.hpp"
18
19 #include "object/player.hpp"
20 #include "sprite/sprite.hpp"
21 #include "sprite/sprite_manager.hpp"
22 #include "util/reader.hpp"
23
24 Torch::Torch(const Reader& reader) :
25   m_torch(),
26   m_flame(),
27   m_flame_glow(),
28   m_flame_light(),
29   m_burning(true)
30 {
31   reader.get("x", bbox.p1.x);
32   reader.get("y", bbox.p1.y);
33
34   std::string sprite_name = "images/objects/torch/torch1.sprite";
35   reader.get("sprite", sprite_name);
36
37   bbox.p2.x = bbox.p1.x + 50;
38   bbox.p2.y = bbox.p1.y + 50;
39
40   m_torch = SpriteManager::current()->create(sprite_name);
41   m_flame = SpriteManager::current()->create("images/objects/torch/flame.sprite");
42   m_flame_glow = SpriteManager::current()->create("images/objects/torch/flame_glow.sprite");
43   m_flame_glow->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
44   m_flame_light = SpriteManager::current()->create("images/objects/torch/flame_light.sprite");
45   m_flame_light->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
46   set_group(COLGROUP_TOUCHABLE);
47 }
48
49 void
50 Torch::draw(DrawingContext& context)
51 {
52   if (m_burning)
53   {
54     m_flame->draw(context, get_pos(), LAYER_TILES - 1);
55
56     context.push_target();
57     context.set_target(DrawingContext::LIGHTMAP);
58     m_flame_light->draw(context, get_pos(), 0);
59     context.pop_target();
60   }
61
62   m_torch->draw(context, get_pos(), LAYER_TILES - 1);
63
64   if (m_burning)
65   {
66     m_flame_glow->draw(context, get_pos(), LAYER_TILES - 1);
67   }
68 }
69
70 void
71 Torch::update(float)
72 {
73 }
74
75 HitResponse
76 Torch::collision(GameObject& other, const CollisionHit& )
77 {
78   // FIXME: this doesn't work, as bbox is wrong
79   Player* player = dynamic_cast<Player*>(&other);
80   if(player == 0)
81   {
82     return ABORT_MOVE;
83   }
84   else
85   {
86     m_burning = true;
87     return ABORT_MOVE;
88   }
89 }
90
91 /* EOF */