home · contact · privacy
Make door closing visible.
[plomrogue2] / plomrogue / things.py
index f066ae660f685a0a8ba05503b1c75e6b4eb25de4..b757157d99cef4375f344ec0038f83599503cae6 100644 (file)
@@ -6,9 +6,9 @@ from plomrogue.mapping import YX
 class ThingBase:
     type_ = '?'
 
-    def __init__(self, game, id_=None, position=(YX(0,0))):
+    def __init__(self, game, id_=0, position=(YX(0, 0), YX(0, 0))):
         self.game = game
-        if id_ is None:
+        if id_ == 0:
             self.id_ = self.game.new_thing_id()
         else:
             self.id_ = id_
@@ -17,6 +17,9 @@ class ThingBase:
 
 
 class Thing(ThingBase):
+    blocking = False
+    portable = False
+    protection = '.'
 
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
@@ -24,14 +27,83 @@ class Thing(ThingBase):
     def proceed(self):
         pass
 
+    @property
+    def type_(self):
+        return self.__class__.get_type()
+
+    @classmethod
+    def get_type(cls):
+        return cls.__name__[len('Thing_'):]
+
+
+
+class Thing_Item(Thing):
+    symbol_hint = 'i'
+    portable = True
+
+
+
+class ThingSpawner(Thing):
+    symbol_hint = 'S'
+
+    def proceed(self):
+        for t in [t for t in self.game.things
+                  if t != self and t.position == self.position]:
+            return
+        t = self.game.thing_types[self.child_type](self.game,
+                                                   position=self.position)
+        self.game.things += [t]
+        self.game.changed = True
+
+
+
+class Thing_ItemSpawner(ThingSpawner):
+    child_type = 'Item'
+
+
+
+class Thing_SpawnPointSpawner(ThingSpawner):
+    child_type = 'SpawnPoint'
+
+
+
+class Thing_SpawnPoint(Thing):
+    symbol_hint = 's'
+    portable = True
+    name = 'username'
+
+
+
+class Thing_DoorSpawner(ThingSpawner):
+    child_type = 'Door'
+
+
+
+class Thing_Door(Thing):
+    symbol_hint = 'D'
+    blocking = False
+    portable = True
+
+    def open(self):
+        self.blocking = False
+        self.portable = True
+        del self.thing_char
+
+    def close(self):
+        self.blocking = True
+        self.portable = False
+        self.thing_char = '#'
+
 
 
 class ThingAnimate(Thing):
+    blocking = True
 
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
         self.next_tasks = []
         self.set_task('WAIT')
+        self._fov = None
 
     def set_task(self, task_name, args=()):
         task_class = self.game.tasks[task_name]
@@ -51,6 +123,7 @@ class ThingAnimate(Thing):
             return None
 
     def proceed(self):
+        self._fov = None
         if self.task is None:
             self.task = self.get_next_task()
             return
@@ -59,20 +132,45 @@ class ThingAnimate(Thing):
             self.task.check()
         except GameError as e:
             self.task = None
-            raise GameError
-            return
+            raise e
         self.task.todo -= 1
         if self.task.todo <= 0:
             self._last_task_result = self.task.do()
             self.game.changed = True
             self.task = self.get_next_task()
 
-
-
-class ThingPlayer(ThingAnimate):
-    type_ = 'player'
+    @property
+    def fov_stencil(self):
+        if self._fov:
+            return self._fov
+        fov_map_class = self.game.map_geometry.fov_map_class
+        self._fov = fov_map_class(self.game.things, self.game.maps, self.position,
+                                  12, self.game.get_map)
+        return self._fov
+
+    def fov_test(self, big_yx, little_yx):
+        test_position = self.fov_stencil.target_yx(big_yx, little_yx)
+        if self.fov_stencil.inside(test_position):
+            if self.fov_stencil[test_position] == '.':
+                return True
+        return False
+
+    def fov_stencil_map(self, map_type='normal'):
+        visible_terrain = ''
+        for yx in self.fov_stencil:
+            if self.fov_stencil[yx] == '.':
+                big_yx, little_yx = self.fov_stencil.source_yxyx(yx)
+                map_ = self.game.get_map(big_yx, map_type)
+                visible_terrain += map_[little_yx]
+            else:
+                visible_terrain += ' '
+        return visible_terrain
+
+
+
+class Thing_Player(ThingAnimate):
+    symbol_hint = '@'
 
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
-        self.nickname = 'undefined'
-
+        self.carrying = None