From 89e74bb7a669e471263805b2b76fc8e79dfbc88e Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Tue, 15 Nov 2016 12:10:47 -0500 Subject: [PATCH] Refactor code into separate modules. --- doric/__init__.py | 0 doric/map.py | 39 ++++++++++++++++++++ terrain.py => doric/terrain.py | 0 hexmap.py => doric/tiles.py | 66 ++++++++++------------------------ main.py | 14 +++----- 5 files changed, 61 insertions(+), 58 deletions(-) create mode 100644 doric/__init__.py create mode 100644 doric/map.py rename terrain.py => doric/terrain.py (100%) rename hexmap.py => doric/tiles.py (68%) diff --git a/doric/__init__.py b/doric/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/doric/map.py b/doric/map.py new file mode 100644 index 0000000..181964f --- /dev/null +++ b/doric/map.py @@ -0,0 +1,39 @@ +from math import ceil + + +class Coordinates(object): + def __init__(self, row, col): + self.row = row + self.col = col + + # set the cube coordinates of the hexagon as [x, y, z] + self.cube_coords = self.even_r_to_cube(self.row, self.col) + + @staticmethod + def even_r_to_cube(row, col): + """compute cube coordinates from even-r hex coordinates""" + x = int(col - ceil(float(row) / 2)) + z = row + y = - x - z + return [x, y, z] + + @staticmethod + def cube_to_even_r(x, y, z): + row = int(x + ceil(z / 2)) + col = z + return [row, col] + + @property + def even_r_coords(self): + """return even-r coordinates of the hexagon.""" + return self.cube_to_even_r(*self.cube_coords) + + @even_r_coords.setter + def even_r_coords(self, value): + self.cube_coords = self.even_r_to_cube(*value) + + def even_r_coordinate_text(self): + return '{}'.format(self.even_r_coords) + + def cube_coordinate_text(self): + return '{!r}'.format(self.cube_coords) diff --git a/terrain.py b/doric/terrain.py similarity index 100% rename from terrain.py rename to doric/terrain.py diff --git a/hexmap.py b/doric/tiles.py similarity index 68% rename from hexmap.py rename to doric/tiles.py index e8a9585..5303b10 100644 --- a/hexmap.py +++ b/doric/tiles.py @@ -1,26 +1,19 @@ -import collections -from math import ceil - +import kivy.utils +from kivy.graphics import Color, Line, Ellipse from kivy.logger import Logger from kivy.uix.label import Label -from kivy.graphics import Color, Line, Ellipse -import kivy.utils from kivy.vector import Vector -from terrain import choose_random_terrain, Terrains - -MapCoords = collections.namedtuple('MapCoords', ['row', 'col']) +from doric.map import Coordinates +from doric.terrain import choose_random_terrain, Terrains -class Tile(Label): +class MapTile(Label): def __init__(self, row=0, col=0, **kwargs): - super(Tile, self).__init__(**kwargs) - self.coords = MapCoords(row, col) - # set the cube coordinates of the hexagon - # as [x, y, z] - self.cube_coords = self.even_r_to_cube(self.coords.row, self.coords.col) - self.selected = False + super(MapTile, self).__init__(**kwargs) + self.coords = Coordinates(row, col) + self.selected = False # Pick a random terrain for each hex. self.terrain = choose_random_terrain() self.terrain_colour = kivy.utils.get_color_from_hex(Terrains[self.terrain]['color']) @@ -39,41 +32,13 @@ class Tile(Label): Color(0, 0, 0, 1) self.coord_label = Label( - text=self.even_r_coordinate_text(), + text=self.coords.even_r_coordinate_text(), center_x=self.center_x, center_y=self.center_y) - @staticmethod - def even_r_to_cube(row, col): - """compute cube coordinates from even-r hex coordinates""" - x = int(col - ceil(float(row)/2)) - z = row - y = - x - z - return [x, y, z] - - @staticmethod - def cube_to_even_r(x, y, z): - row = int(x + ceil(z / 2)) - col = z - return [row, col] - - @property - def even_r_coords(self): - """return even-r coordinates of the hexagon.""" - return self.cube_to_even_r(*self.cube_coords) - - @even_r_coords.setter - def even_r_coords(self, value): - self.cube_coords = self.even_r_to_cube(*value) - - def even_r_coordinate_text(self): - return '{}'.format(self.even_r_coords) - - def cube_coordinate_text(self): - return '{!r}'.format(self.cube_coords) - def map_display_text(self): - return "{}\n{} \n {}".format(self.even_r_coordinate_text(), self.cube_coordinate_text(), self.terrain) + return "{}\n{} \n {}".format( + self.coords.even_r_coordinate_text(), self.coords.cube_coordinate_text(), self.terrain) def update_pos(self, instance, value): @@ -92,10 +57,10 @@ class Tile(Label): self.coord_label.center_y = self.center_y def on_touch_down(self, touch): - if super(Tile, self).on_touch_down(touch): + if super(MapTile, self).on_touch_down(touch): return False - coord_x, coord_y = self.even_r_coords + coord_x, coord_y = self.coords.even_r_coords with self.canvas.after: Color(*kivy.utils.get_color_from_hex('#000000')) @@ -127,3 +92,8 @@ class Tile(Label): Logger.debug('({}, {}) -> ({}, {})'.format(self.x, self.y, coord_x, coord_y)) Logger.debug('Dist: {} Diff: {}'.format(dist, dist - radius)) return dist - radius <= 0 + + +class SpacerTile(Label): + def __init__(self): + super(SpacerTile, self).__init__(size_hint=(0.5, 1), text=':)') diff --git a/main.py b/main.py index 727f334..8dcc274 100644 --- a/main.py +++ b/main.py @@ -1,14 +1,8 @@ from kivy import app, properties -from kivy.uix.label import Label from kivy.uix.boxlayout import BoxLayout from kivy.graphics import Color, Rectangle -from hexmap import Tile - - -class HexSpace(Label): - def __init__(self): - super(HexSpace, self).__init__(size_hint=(0.5, 1), text=':)') +from doric.tiles import MapTile, SpacerTile class StrategyGame(BoxLayout): @@ -25,14 +19,14 @@ class StrategyGame(BoxLayout): hex_map_row = BoxLayout(orientation='horizontal') if row % 2 == 1: - hex_map_row.add_widget(HexSpace()) + hex_map_row.add_widget(SpacerTile()) for col in range(0, self.map_cols): - map_tile = Tile(row=row, col=col) + map_tile = MapTile(row=row, col=col) hex_map_row.add_widget(map_tile) if row % 2 == 0: - hex_map_row.add_widget(HexSpace()) + hex_map_row.add_widget(SpacerTile()) self.main_map.add_widget(hex_map_row)