Refactor code into separate modules.
This commit is contained in:
parent
f4494af342
commit
89e74bb7a6
|
@ -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)
|
|
@ -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=':)')
|
14
main.py
14
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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue