From: Christian Heller Date: Tue, 30 Apr 2019 18:56:56 +0000 (+0200) Subject: More refactoring. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%7B%7Bprefix%7D%7D/move_down?a=commitdiff_plain;h=14d027c893d5576d54a86db2168f5b43dd5f9773;p=plomrogue2-experiments More refactoring. --- diff --git a/new/plomrogue/game.py b/new/plomrogue/game.py index 6b2c74b..948c97e 100755 --- a/new/plomrogue/game.py +++ b/new/plomrogue/game.py @@ -105,22 +105,22 @@ class Game(GameBase): def send_gamestate(self, connection_id=None): """Send out game state data relevant to clients.""" - def send_thing(offset, thing): - offset_pos = self.map_geometry.pos_in_projection(thing.position, - offset, - self.map_size) + def send_thing(thing): + view_pos = self.map_geometry.pos_in_view(thing.position, + self.player.view_offset, + self.map_size) self.io.send('THING_TYPE %s %s' % (thing.id_, thing.type_)) - self.io.send('THING_POS %s %s' % (thing.id_, offset_pos)) + self.io.send('THING_POS %s %s' % (thing.id_, view_pos)) self.io.send('TURN ' + str(self.turn)) visible_map = self.player.get_visible_map() - offset = self.player.get_surroundings_offset() - self.io.send('VISIBLE_MAP %s %s' % (offset, visible_map.size)) + self.io.send('VISIBLE_MAP %s %s' % (self.player.view_offset, + visible_map.size)) for y, line in visible_map.lines(): self.io.send('VISIBLE_MAP_LINE %5s %s' % (y, quote(line))) visible_things = self.player.get_visible_things() for thing in visible_things: - send_thing(offset, thing) + send_thing(thing) if hasattr(thing, 'health'): self.io.send('THING_HEALTH %s %s' % (thing.id_, thing.health)) @@ -131,7 +131,7 @@ class Game(GameBase): self.io.send('PLAYER_INVENTORY ,') for id_ in self.player.inventory: thing = self.get_thing(id_) - send_thing(offset, thing) + send_thing(thing) self.io.send('GAME_STATE_COMPLETE') def proceed(self): diff --git a/new/plomrogue/mapping.py b/new/plomrogue/mapping.py index 9826e68..e6ae987 100644 --- a/new/plomrogue/mapping.py +++ b/new/plomrogue/mapping.py @@ -85,10 +85,17 @@ class MapGeometry(): self.neighbors_to[map_size][pos] = neighbors return neighbors - def pos_in_projection(self, pos, offset, maps_size): - pos_y = pos[1].y + (maps_size.y * pos[0].y) - offset.y - pos_x = pos[1].x + (maps_size.x * pos[0].x) - offset.x - return YX(pos_y, pos_x) + def undouble_coordinate(self, maps_size, coordinate): + y = maps_size.y * coordinate[0].y + coordinate[1].y + x = maps_size.x * coordinate[0].x + coordinate[1].x + return YX(y, x) + + def get_view_offset(self, maps_size, center, radius): + yx_to_origin = self.undouble_coordinate(maps_size, center) + return yx_to_origin - YX(radius, radius) + + def pos_in_view(self, pos, offset, maps_size): + return self.undouble_coordinate(maps_size, pos) - offset def correct_double_coordinate(self, map_size, big_yx, little_yx): diff --git a/new/plomrogue/things.py b/new/plomrogue/things.py index 5d1c7bc..db37a04 100644 --- a/new/plomrogue/things.py +++ b/new/plomrogue/things.py @@ -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: @@ -249,25 +247,21 @@ 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 + @property + def view_offset(self): + return self.game.map_geometry.get_view_offset(self.game.map_size, + self.position, + self._radius) 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) + big_yx, small_yx = correct(self.game.map_size, (0,0), + pos + self.view_offset) map_ = self.game.get_map(big_yx, False) if map_ is None: map_ = Map(size=self.game.map_size) @@ -296,12 +290,11 @@ class ThingAnimate(Thing): 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