Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / supertux / game_object.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
17 #include "supertux/game_object.hpp"
18 #include "supertux/object_remove_listener.hpp"
19
20 GameObject::GameObject() :
21   wants_to_die(false), 
22   remove_listeners(NULL)
23 {
24 }
25
26 GameObject::GameObject(const GameObject& rhs) :
27   wants_to_die(rhs.wants_to_die),
28   remove_listeners(NULL)
29 {  
30 }
31
32 GameObject::~GameObject()
33 {
34   // call remove listeners (and remove them from the list)
35   RemoveListenerListEntry* entry = remove_listeners;
36   while(entry != NULL) {
37     RemoveListenerListEntry* next = entry->next;
38     entry->listener->object_removed(this);
39     delete entry;
40     entry = next;
41   }
42 }
43  
44 void 
45 GameObject::add_remove_listener(ObjectRemoveListener* listener)
46 {
47   RemoveListenerListEntry* entry = new RemoveListenerListEntry();
48   entry->next = remove_listeners;
49   entry->listener = listener;
50   remove_listeners = entry;
51 }
52
53 void
54 GameObject::del_remove_listener(ObjectRemoveListener* listener)
55 {
56   RemoveListenerListEntry* entry = remove_listeners;
57   if (entry->listener == listener) {
58     remove_listeners = entry->next;
59     delete entry;
60     return;
61   }
62   RemoveListenerListEntry* next = entry->next;
63   while(next != NULL) {
64     if (next->listener == listener) {
65       entry->next = next->next;
66       delete next;
67       break;
68     }
69     entry = next;
70     next = next->next;
71   }
72 }
73
74 /* EOF */