Moved HexMapCell out of main.py into its own module.

This commit is contained in:
Amy Wooding 2016-07-16 17:49:58 -04:00
parent cf6ef569a5
commit acf52537a2
2 changed files with 67 additions and 63 deletions

66
hexmap.py Normal file
View File

@ -0,0 +1,66 @@
import collections
from math import ceil
from kivy.uix.label import Label
MapCoords = collections.namedtuple('MapCoords', ['row', 'col'])
class HexMapCell(Label):
def __init__(self, row=0, col=0, **kwargs):
super(HexMapCell, 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 / 3, self.coords.col / 2)
def even_r_to_cube(self, 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])
def cube_to_even_r(self, 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 coordinate_text(self):
return '({}, {})'.format(self.coords.row, self.coords.col)
def even_r_coordinate_text(self):
return '{}'.format(self.even_r_coords)
def cube_coordinate_text(self):
return '{}\n{}\n{}'.format(*self.cube_coords)
def map_display_text(self):
return "{}\n{}".format(self.even_r_coordinate_text(), self.cube_coordinate_text())
def update_pos(self, instance, value):
# Determine the location of the solid hexagon cell. Needs to be offset from the centre of the hex.
radius = 2 * self.height
solid_x = self.x - self.height*2
solid_y = self.y - self.height*2
solid_size = (4*self.height, 4*self.height)
# Resize the outline of the cell.
self.ell.circle = (self.x, self.y, radius, 0, 360, 6)
# Resize the actual cell.
self.solid.pos = (solid_x, solid_y)
self.solid.size = solid_size
self.coord_label.center_x = self.x
self.coord_label.center_y = self.y

64
main.py
View File

@ -1,13 +1,11 @@
import collections
from kivy import app, properties
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import Color, Ellipse, Line
import kivy.utils
from math import ceil
from hexmap import HexMapCell
MapCoords = collections.namedtuple('MapCoords', ['row', 'col'])
class StrategyGame(FloatLayout):
@ -57,66 +55,6 @@ class StrategyGame(FloatLayout):
hex_cell.bind(pos=hex_cell.update_pos, size=hex_cell.update_pos)
class HexMapCell(Label):
def __init__(self, row=0, col=0, **kwargs):
super(HexMapCell, 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 / 3, self.coords.col / 2)
def even_r_to_cube(self, 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])
def cube_to_even_r(self, 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 coordinate_text(self):
return '({}, {})'.format(self.coords.row, self.coords.col)
def even_r_coordinate_text(self):
return '{}'.format(self.even_r_coords)
def cube_coordinate_text(self):
return '{}\n{}\n{}'.format(*self.cube_coords)
def map_display_text(self):
return "{}\n{}".format(self.even_r_coordinate_text(), self.cube_coordinate_text())
def update_pos(self, instance, value):
# Determine the location of the solid hexagon cell. Needs to be offset from the centre of the hex.
radius = 2 * self.height
solid_x = self.x - self.height*2
solid_y = self.y - self.height*2
solid_size = (4*self.height, 4*self.height)
# Resize the outline of the cell.
self.ell.circle = (self.x, self.y, radius, 0, 360, 6)
# Resize the actual cell.
self.solid.pos = (solid_x, solid_y)
self.solid.size = solid_size
self.coord_label.center_x = self.x
self.coord_label.center_y = self.y
class StrategyGameApp(app.App):
def build(self):
return StrategyGame()