Rework outline drawing and extract tile setup.

This commit is contained in:
Dorian 2016-11-15 11:16:19 -05:00
parent 06e9128ff0
commit f4494af342
2 changed files with 14 additions and 15 deletions

View File

@ -12,33 +12,30 @@ from terrain import choose_random_terrain, Terrains
MapCoords = collections.namedtuple('MapCoords', ['row', 'col']) MapCoords = collections.namedtuple('MapCoords', ['row', 'col'])
class HexMapCell(Label): class Tile(Label):
def __init__(self, row=0, col=0, **kwargs): def __init__(self, row=0, col=0, **kwargs):
super(HexMapCell, self).__init__(**kwargs) super(Tile, self).__init__(**kwargs)
self.coords = MapCoords(row, col) self.coords = MapCoords(row, col)
# set the cube coordinates of the hexagon # set the cube coordinates of the hexagon
# as [x, y, z] # as [x, y, z]
self.cube_coords = self.even_r_to_cube(self.coords.row // 3, self.coords.col // 2) self.cube_coords = self.even_r_to_cube(self.coords.row, self.coords.col)
self.selected = False self.selected = False
# Pick a random terrain for each hex. # Pick a random terrain for each hex.
self.terrain = choose_random_terrain() self.terrain = choose_random_terrain()
self.terrain_colour = kivy.utils.get_color_from_hex(Terrains[self.terrain]['color']) self.terrain_colour = kivy.utils.get_color_from_hex(Terrains[self.terrain]['color'])
# Determine the location of the solid hexagon cell. Needs to be offset from the centre of the hex.
radius = self.height / 2 radius = self.height / 2
solid_size = (self.height, self.height)
self.bind(pos=self.update_pos, size=self.update_pos) self.bind(pos=self.update_pos, size=self.update_pos)
with self.canvas.after: with self.canvas.after:
# Create the solid background of the hexagon, from the bottom left coordinate of the hex. # Create the solid background of the hexagon, from the bottom left coordinate of the hex.
Color(*self.terrain_colour) Color(*self.terrain_colour)
self.solid = Ellipse(pos=(self.x, self.y), size=solid_size, segments=6) self.solid = Ellipse(pos=(self.x, self.y), size=(self.height, self.height), segments=6)
# Create the outline of hexagon, based off the centre of the hex. # Create the outline of hexagon, based off the centre of the hex.
Color(*kivy.utils.get_color_from_hex('#000000')) Color(*kivy.utils.get_color_from_hex('#000000'))
self.ell = Line(circle=(self.x, self.y, radius, 0, 360, 6), width=2) self.ell = Line(circle=(self.center_x, self.center_y, radius, 0, 360, 6), width=2)
Color(0, 0, 0, 1) Color(0, 0, 0, 1)
self.coord_label = Label( self.coord_label = Label(
@ -79,11 +76,13 @@ class HexMapCell(Label):
return "{}\n{} \n {}".format(self.even_r_coordinate_text(), self.cube_coordinate_text(), self.terrain) return "{}\n{} \n {}".format(self.even_r_coordinate_text(), self.cube_coordinate_text(), self.terrain)
def update_pos(self, instance, value): def update_pos(self, instance, value):
# Determine the location of the solid hexagon cell. Needs to be offset from the centre of the hex.
self.canvas.clear()
radius = self.height / 2 radius = self.height / 2
# Resize the outline of the cell. # Resize the outline of the cell.
self.ell.circle = (self.x, self.y, radius, 0, 360, 6) self.ell.circle = (self.center_x, self.center_y, radius, 0, 360, 6)
# Resize the actual cell. # Resize the actual cell.
self.solid.pos = (self.x, self.y) self.solid.pos = (self.x, self.y)
@ -93,7 +92,7 @@ class HexMapCell(Label):
self.coord_label.center_y = self.center_y self.coord_label.center_y = self.center_y
def on_touch_down(self, touch): def on_touch_down(self, touch):
if super(HexMapCell, self).on_touch_down(touch): if super(Tile, self).on_touch_down(touch):
return False return False
coord_x, coord_y = self.even_r_coords coord_x, coord_y = self.even_r_coords
@ -101,7 +100,7 @@ class HexMapCell(Label):
with self.canvas.after: with self.canvas.after:
Color(*kivy.utils.get_color_from_hex('#000000')) Color(*kivy.utils.get_color_from_hex('#000000'))
radius = self.height / 2 radius = self.height / 2
self.ell = Line(circle=(self.x, self.y, radius, 0, 360, 6), width=2) self.ell = Line(circle=(self.center_x, self.center_y, radius, 0, 360, 6), width=2)
if not self.collide_with_bounding_circle(touch.x, touch.y): if not self.collide_with_bounding_circle(touch.x, touch.y):
return False return False
@ -115,7 +114,7 @@ class HexMapCell(Label):
# TODO Will refactor to have separate on_touch_up for selected target hex instead. # TODO Will refactor to have separate on_touch_up for selected target hex instead.
Color(*kivy.utils.get_color_from_hex('#FF0000')) Color(*kivy.utils.get_color_from_hex('#FF0000'))
radius = self.height / 2 radius = self.height / 2
self.ell = Line(circle=(self.x, self.y, radius, 0, 360, 6), width=2) self.ell = Line(circle=(self.center_x, self.center_y, radius, 0, 360, 6), width=2)
self.parent.parent.game.update_selected_cell(self) self.parent.parent.game.update_selected_cell(self)
return True return True

View File

@ -3,7 +3,7 @@ from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout from kivy.uix.boxlayout import BoxLayout
from kivy.graphics import Color, Rectangle from kivy.graphics import Color, Rectangle
from hexmap import HexMapCell from hexmap import Tile
class HexSpace(Label): class HexSpace(Label):
@ -28,7 +28,7 @@ class StrategyGame(BoxLayout):
hex_map_row.add_widget(HexSpace()) hex_map_row.add_widget(HexSpace())
for col in range(0, self.map_cols): for col in range(0, self.map_cols):
map_tile = HexMapCell(row=row, col=col) map_tile = Tile(row=row, col=col)
hex_map_row.add_widget(map_tile) hex_map_row.add_widget(map_tile)
if row % 2 == 0: if row % 2 == 0: