Re-implement the hex-map drawing code.

This commit is contained in:
Dorian 2016-11-15 09:19:41 -05:00
parent f0fdbc18bd
commit 06e9128ff0
2 changed files with 23 additions and 66 deletions

View File

@ -18,25 +18,23 @@ class HexMapCell(Label):
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)
self.cube_coords = self.even_r_to_cube(self.coords.row // 3, self.coords.col // 2)
self.selected = False
self.visible_on_map = False
# Pick a random terrain for each hex.
self.terrain = choose_random_terrain()
self.terrain_colour = Color(0, 0, 0, 1)
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
solid_x = self.x - self.height
solid_y = self.y - self.height
solid_size = (self.height, self.height)
self.bind(pos=self.update_pos, size=self.update_pos)
with self.canvas.after:
# Create the solid background of the hexagon, from the bottom left coordinate of the hex.
self.terrain_colour = kivy.utils.get_color_from_hex(Terrains[self.terrain]['color'])
Color(*self.terrain_colour)
self.solid = Ellipse(pos=(solid_x, solid_y), size=solid_size, segments=6)
self.solid = Ellipse(pos=(self.x, self.y), size=solid_size, segments=6)
# Create the outline of hexagon, based off the centre of the hex.
Color(*kivy.utils.get_color_from_hex('#000000'))
@ -44,9 +42,9 @@ class HexMapCell(Label):
Color(0, 0, 0, 1)
self.coord_label = Label(
text=self.map_display_text(),
center_x=self.x,
center_y=self.y)
text=self.even_r_coordinate_text(),
center_x=self.center_x,
center_y=self.center_y)
@staticmethod
def even_r_to_cube(row, col):
@ -60,7 +58,7 @@ class HexMapCell(Label):
def cube_to_even_r(x, y, z):
row = int(x + ceil(z / 2))
col = z
return ([row, col])
return [row, col]
@property
def even_r_coords(self):
@ -82,32 +80,27 @@ class HexMapCell(Label):
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)
radius = self.height / 2
# 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.solid.pos = (self.x, self.y)
self.solid.size = (self.height, self.height)
self.coord_label.center_x = self.x
self.coord_label.center_y = self.y
self.coord_label.center_x = self.center_x
self.coord_label.center_y = self.center_y
def on_touch_down(self, touch):
if super(HexMapCell, self).on_touch_down(touch):
return False
coord_x, coord_y = self.even_r_coords
if not self.visible_on_map:
return False
with self.canvas.after:
Color(*kivy.utils.get_color_from_hex('#000000'))
radius = 2 * self.height
radius = self.height / 2
self.ell = Line(circle=(self.x, self.y, radius, 0, 360, 6), width=2)
if not self.collide_with_bounding_circle(touch.x, touch.y):
@ -121,16 +114,16 @@ class HexMapCell(Label):
if 'button' in touch.profile and touch.button == 'right':
# TODO Will refactor to have separate on_touch_up for selected target hex instead.
Color(*kivy.utils.get_color_from_hex('#FF0000'))
radius = 2 * self.height
radius = self.height / 2
self.ell = Line(circle=(self.x, self.y, radius, 0, 360, 6), width=2)
self.parent.game.update_selected_cell(self.even_r_coords, self.terrain_colour)
self.parent.parent.game.update_selected_cell(self)
return True
def collide_with_bounding_circle(self, coord_x, coord_y):
# Register if within bounds of circle that the hex is inscribed in.
Logger.debug('Detected: ({}, {})'.format(coord_x, coord_y))
radius = 2 * self.height
radius = self.height / 2
dist = Vector(self.x, self.y).distance((coord_x, coord_y))
Logger.debug('({}, {}) -> ({}, {})'.format(self.x, self.y, coord_x, coord_y))
Logger.debug('Dist: {} Diff: {}'.format(dist, dist - radius))

46
main.py
View File

@ -1,10 +1,7 @@
import random
from kivy import app, properties
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics import Color, Rectangle, Ellipse
from kivy.graphics import Color, Rectangle
from hexmap import HexMapCell
@ -31,21 +28,7 @@ class StrategyGame(BoxLayout):
hex_map_row.add_widget(HexSpace())
for col in range(0, self.map_cols):
# map_tile = HexMapCell(col=col, row=row)
# map_tile.visible_on_map = True
# map_tile.disabled = False
# map_tile.bind(pos=map_tile.update_pos, size=map_tile.update_pos)
map_tile = Button(text="{} {}".format(col, row), background_color=(
random.random(), random.random(), random.random(), 0.6))
with map_tile.canvas.after:
Color(random.random(), random.random(), random.random(), 0.6)
solid_x = map_tile.x - map_tile.height
solid_y = map_tile.y - map_tile.height
solid_size = (map_tile.height, map_tile.height)
Ellipse(pos=(solid_x, solid_y), size=solid_size, segments=6)
map_tile = HexMapCell(row=row, col=col)
hex_map_row.add_widget(map_tile)
if row % 2 == 0:
@ -53,29 +36,10 @@ class StrategyGame(BoxLayout):
self.main_map.add_widget(hex_map_row)
# number_of_regions = self.map_rows * self.map_cols
# for region in range(0, number_of_regions):
# row = region / self.map_cols
# col = region % self.map_cols
#
# # Add hex cells to make up the map.
# hex_cell = HexMapCell(row, col)
# hex_cell.disabled = True
# self.main_map.add_widget(hex_cell)
#
# # Add overlay conditionally.
# if (row % 6 == 1 and col % 2 == 1) or (row % 6 == 4 and col % 2 == 0) and (col > 0):
# hex_cell.disabled = False
# hex_cell.visible_on_map = True
#
# # Bind the cell code so as to update its position and size when the parent widget resizes.
# hex_cell.bind(pos=hex_cell.update_pos, size=hex_cell.update_pos)
#
def update_selected_cell(self, coords, terrain_colour, *args):
self.status.text = 'Coords: ({}, {})'.format(coords[0], coords[1])
def update_selected_cell(self, selected_tile, *args):
self.status.text = selected_tile.map_display_text()
with self.status.canvas.before:
Color(*terrain_colour)
Color(*selected_tile.terrain_colour)
Rectangle(pos=self.status.pos, size=self.status.size)
return True