home · contact · privacy
More refactoring.
[plomrogue2-experiments] / new / plomrogue / things.py
index 5d1c7bce1b72bf21b7fdd23a64a67998236019aa..aded05c60d777918d975023968c8dd1959111d56 100644 (file)
@@ -147,15 +147,14 @@ class ThingAnimate(Thing):
 
     def hunt_player(self):
         visible_things = self.get_visible_things()
-        offset = self.get_surroundings_offset()
         target = None
         for t in visible_things:
             if t.type_ == 'human':
-                target = t.position[1] - offset
+                target = t.position[1] - self.view_offset
                 break
         if target is not None:
             try:
-                offset_self_pos = self.position[1] - offset
+                offset_self_pos = self.position[1] - self.view_offset
                 target_dir = self.move_on_dijkstra_map(offset_self_pos,
                                                        [target])
                 if target_dir is not None:
@@ -177,12 +176,11 @@ class ThingAnimate(Thing):
                 self.set_task('PICKUP', (id_,))
                 return True
         visible_things = self.get_visible_things()
-        offset = self.get_surroundings_offset()
         food_targets = []
         for t in visible_things:
             if t.type_ == 'food':
-                food_targets += [t.position[1] - offset]
-        offset_self_pos = self.position[1] - offset
+                food_targets += [t.position[1] - self.view_offset]
+        offset_self_pos = self.position[1] - self.view_offset
         target_dir = self.move_on_dijkstra_map(offset_self_pos,
                                                food_targets)
         if target_dir:
@@ -248,39 +246,30 @@ class ThingAnimate(Thing):
 
     def unset_surroundings(self):
         self._stencil = None
-        self._surrounding_map = None
-        self._surroundings_offset = None
-
-    def get_surroundings_offset(self):
-        if self._surroundings_offset is not None:
-            return self._surroundings_offset
-        y_long = self.position[0].y * self.game.map_size.y + self.position[1].y
-        x_long = self.position[0].x * self.game.map_size.x + self.position[1].x
-        yx_to_origin = YX(y_long, x_long)
-        self._surroundings_offset = yx_to_origin - YX(self._radius, self._radius)
-        return self._surroundings_offset
-
-    def get_surrounding_map(self):
-        if self._surrounding_map is not None:
-            return self._surrounding_map
-        self._surrounding_map = Map(size=YX(self._radius*2+1, self._radius*2+1))
-        offset = self.get_surroundings_offset()
-        for pos in self._surrounding_map:
-            correct = self.game.map_geometry.correct_double_coordinate
-            big_yx, small_yx = correct(self.game.map_size, (0,0), pos + offset)
-            map_ = self.game.get_map(big_yx, False)
-            if map_ is None:
-                map_ = Map(size=self.game.map_size)
-            self._surrounding_map[pos] = map_[small_yx]
-        return self._surrounding_map
+        self._surroundings = None
+
+    @property
+    def view_offset(self):
+        return self.game.map_geometry.get_view_offset(self.game.map_size,
+                                                      self.position,
+                                                      self._radius)
+
+    @property
+    def surroundings(self):
+        if self._surroundings is not None:
+            return self._surroundings
+        s = self.game.map_geometry.get_view(self.game.map_size,
+                                            self.game.get_map,
+                                            self._radius, self.view_offset)
+        self._surroundings = s
+        return self._surroundings
 
     def get_stencil(self):
         if self._stencil is not None:
             return self._stencil
-        surrounding_map = self.get_surrounding_map()
-        m = Map(surrounding_map.size, ' ')
-        for pos in surrounding_map:
-            if surrounding_map[pos] in {'.', '~'}:
+        m = Map(self.surroundings.size, ' ')
+        for pos in self.surroundings:
+            if self.surroundings[pos] in {'.', '~'}:
                 m[pos] = '.'
         fov_center = YX((m.size.y) // 2, m.size.x // 2)
         self._stencil = FovMapHex(m, fov_center)
@@ -288,20 +277,19 @@ class ThingAnimate(Thing):
 
     def get_visible_map(self):
         stencil = self.get_stencil()
-        m = Map(self.get_surrounding_map().size, ' ')
+        m = Map(self.surroundings.size, ' ')
         for pos in m:
             if stencil[pos] == '.':
-                m[pos] = self._surrounding_map[pos]
+                m[pos] = self.surroundings[pos]
         return m
 
     def get_visible_things(self):
         stencil = self.get_stencil()
-        offset = self.get_surroundings_offset()
         visible_things = []
         for thing in self.game.things:
-            pos = self.game.map_geometry.pos_in_projection(thing.position,
-                                                           offset,
-                                                           self.game.map_size)
+            pos = self.game.map_geometry.pos_in_view(thing.position,
+                                                     self.view_offset,
+                                                     self.game.map_size)
             if pos.y < 0 or pos.x < 0 or\
                pos.y >= stencil.size.y or pos.x >= stencil.size.x:
                 continue