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