Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / worldmap / sprite_change.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.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 #include <config.h>
17
18 #include "sprite/sprite.hpp"
19 #include "sprite/sprite_manager.hpp"
20 #include "video/drawing_context.hpp"
21 #include "worldmap/sprite_change.hpp"
22
23 namespace WorldMapNS {
24
25 SpriteChange::SpriteChange(const lisp::Lisp* lisp)
26   : change_on_touch(false), in_stay_action(false)
27 {
28   lisp->get("x", pos.x);
29   lisp->get("y", pos.y);
30   lisp->get("change-on-touch", change_on_touch);
31
32   std::string spritefile = "";
33   lisp->get("sprite", spritefile);
34   sprite = sprite_manager->create(spritefile);
35
36   lisp->get("stay-action", stay_action);
37   lisp->get("initial-stay-action", in_stay_action);
38
39   lisp->get("stay-group", stay_group);
40
41   all_sprite_changes.push_back(this);
42 }
43
44 SpriteChange::~SpriteChange()
45 {
46   all_sprite_changes.remove(this);
47 }
48
49 void
50 SpriteChange::draw(DrawingContext& context)
51 {
52   if(in_stay_action && stay_action != "") {
53     sprite->set_action(stay_action);
54     sprite->draw(context, pos * 32, LAYER_OBJECTS-1);
55   }
56 }
57
58 void
59 SpriteChange::update(float )
60 {
61 }
62
63 void
64 SpriteChange::set_stay_action()
65 {
66   in_stay_action = true;
67 }
68
69 void
70 SpriteChange::clear_stay_action()
71 {
72   in_stay_action = false;
73
74   // if we are in a stay_group, also clear all stay actions in this group
75   if (stay_group != "") {
76     for (std::list<SpriteChange*>::iterator i = all_sprite_changes.begin(); i != all_sprite_changes.end(); i++) {
77       SpriteChange* sc = *i;
78       if (sc->stay_group != stay_group) continue;
79       sc->in_stay_action = false;
80     }
81   }
82 }
83
84 std::list<SpriteChange*> SpriteChange::all_sprite_changes;
85
86 }
87
88 /* EOF */