home · contact · privacy
Refactor thing addition / removal.
[plomrogue2] / plomrogue / game.py
index 6e8afdf83182dded8a25c735b94dc7cdc2a7095a..10fcb12b19f3c7f644d11eb8376368d99c15028c 100755 (executable)
@@ -210,6 +210,22 @@ class Game(GameBase):
                 return '/O  O\\' + '| oo |' + '\\>--</'
         return None
 
+    def remove_thing(self, t):
+        self.things.remove(t)
+        self.changed_fovs = True
+
+    def add_thing(self, type_, position, id_=0):
+        t_old = None
+        if id_ > 0:
+            t_old = self.get_thing(id_)
+        t = self.thing_types[type_](self, id_=id_, position=position)
+        if t_old:
+            self.things[self.things.index(t_old)] = t
+        else:
+            self.things += [t]
+        self.changed_fovs = True
+        return t
+
     def send_gamestate(self, connection_id=None):
         """Send out game state data relevant to clients."""
 
@@ -294,8 +310,7 @@ class Game(GameBase):
                 t = self.get_player(connection_id)
                 if hasattr(t, 'name'):
                     self.io.send('CHAT ' + quote(t.name + ' left the map.'))
-                self.things.remove(t)
-                self.changed_fovs = True
+                self.remove_thing(t)
                 to_delete += [connection_id]
         for connection_id in to_delete:
             del self.sessions[connection_id]