Rework tile map painting.
This commit is contained in:
parent
ef675ed575
commit
277ad2a664
|
@ -40,37 +40,28 @@ Terrains = {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def get_terrain_background_color(terrain):
|
|
||||||
hex_colour = Terrains[terrain.substrate.name]['color']
|
|
||||||
return kivy.utils.get_color_from_hex(hex_colour)
|
|
||||||
|
|
||||||
|
|
||||||
class MapTile(Label):
|
class MapTile(Label):
|
||||||
def __init__(self, row=0, col=0, **kwargs):
|
def __init__(self, row=0, col=0, **kwargs):
|
||||||
super(MapTile, self).__init__(**kwargs)
|
super(MapTile, self).__init__(**kwargs)
|
||||||
self.coords = Coordinates(row, col)
|
self.coords = Coordinates(row, col)
|
||||||
|
|
||||||
# Pick a random terrain for each hex.
|
|
||||||
self.terrain = Terrain.choose_random_terrain()
|
self.terrain = Terrain.choose_random_terrain()
|
||||||
self.terrain_colour = get_terrain_background_color(self.terrain)
|
|
||||||
|
|
||||||
self.bind(pos=self.update_pos, size=self.update_pos)
|
with self.canvas:
|
||||||
|
|
||||||
with self.canvas.after:
|
|
||||||
# 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=(self.height, self.height), segments=6)
|
self.tile_background = 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.
|
|
||||||
Color(*kivy.utils.get_color_from_hex('#000000'))
|
|
||||||
self.ell = Line(circle=(self.center_x, self.center_y, self.hex_radius, 0, 360, 6), width=2)
|
|
||||||
|
|
||||||
Color(0, 0, 0, 1)
|
|
||||||
self.coord_label = Label(
|
self.coord_label = Label(
|
||||||
text=self.coords.even_r_coordinate_text(),
|
text=self.coords.even_r_coordinate_text(),
|
||||||
center_x=self.center_x,
|
center_x=self.center_x,
|
||||||
center_y=self.center_y)
|
center_y=self.center_y)
|
||||||
|
|
||||||
|
self.bind(pos=self.update_positions, size=self.update_positions)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def terrain_colour(self):
|
||||||
|
hex_colour = Terrains[self.terrain.substrate.name]['color']
|
||||||
|
return kivy.utils.get_color_from_hex(hex_colour)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def hex_radius(self):
|
def hex_radius(self):
|
||||||
return self.height / 2
|
return self.height / 2
|
||||||
|
@ -79,16 +70,9 @@ class MapTile(Label):
|
||||||
return "{}\n{} \n {}".format(
|
return "{}\n{} \n {}".format(
|
||||||
self.coords.even_r_coordinate_text(), self.coords.cube_coordinate_text(), self.terrain.description())
|
self.coords.even_r_coordinate_text(), self.coords.cube_coordinate_text(), self.terrain.description())
|
||||||
|
|
||||||
def update_pos(self, instance, value):
|
def update_positions(self, instance, value):
|
||||||
|
self.tile_background.pos = (self.x, self.y)
|
||||||
self.canvas.clear()
|
self.tile_background.size = (self.height, self.height)
|
||||||
|
|
||||||
# Resize the outline of the cell.
|
|
||||||
self.ell.circle = (self.center_x, self.center_y, self.hex_radius, 0, 360, 6)
|
|
||||||
|
|
||||||
# Resize the actual cell.
|
|
||||||
self.solid.pos = (self.x, self.y)
|
|
||||||
self.solid.size = (self.height, self.height)
|
|
||||||
|
|
||||||
self.coord_label.center_x = self.center_x
|
self.coord_label.center_x = self.center_x
|
||||||
self.coord_label.center_y = self.center_y
|
self.coord_label.center_y = self.center_y
|
||||||
|
@ -97,31 +81,18 @@ class MapTile(Label):
|
||||||
if super(MapTile, self).on_touch_down(touch):
|
if super(MapTile, self).on_touch_down(touch):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
with self.canvas.after:
|
|
||||||
Color(*kivy.utils.get_color_from_hex('#000000'))
|
|
||||||
self.ell = Line(circle=(self.center_x, self.center_y, self.hex_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
|
||||||
|
|
||||||
with self.canvas.after:
|
self.parent.parent.game.on_selected_cell(self)
|
||||||
if 'button' in touch.profile and touch.button == 'left':
|
|
||||||
Color(*kivy.utils.get_color_from_hex('#00FF00'))
|
|
||||||
|
|
||||||
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'))
|
|
||||||
self.ell = Line(circle=(self.center_x, self.center_y, self.hex_radius, 0, 360, 6), width=2)
|
|
||||||
|
|
||||||
self.parent.parent.game.update_selected_cell(self)
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def collide_with_bounding_circle(self, coord_x, coord_y):
|
def collide_with_bounding_circle(self, coord_x, coord_y):
|
||||||
# Register if within bounds of circle that the hex is inscribed in.
|
# Register if within bounds of circle that the hex is inscribed in.
|
||||||
Logger.debug('Detected: ({}, {})'.format(coord_x, coord_y))
|
dist = Vector(self.center_x, self.center_y).distance((coord_x, coord_y))
|
||||||
dist = Vector(self.x, self.y).distance((coord_x, coord_y))
|
Logger.debug('Distance: ({:.2f}, {:.2f}) -> ({:.2f}, {:.2f})'.format(
|
||||||
Logger.debug('({}, {}) -> ({}, {})'.format(self.x, self.y, coord_x, coord_y))
|
self.center_x, self.center_y, coord_x, coord_y))
|
||||||
Logger.debug('Dist: {} Diff: {}'.format(dist, dist - self.hex_radius))
|
Logger.debug('Distance: {:.2f} / Diff: {:.2f}'.format(dist, dist - self.hex_radius))
|
||||||
return dist - self.hex_radius <= 0
|
return dist - self.hex_radius <= 0
|
||||||
|
|
||||||
|
|
||||||
|
|
2
main.py
2
main.py
|
@ -30,7 +30,7 @@ class StrategyGame(BoxLayout):
|
||||||
|
|
||||||
self.main_map.add_widget(hex_map_row)
|
self.main_map.add_widget(hex_map_row)
|
||||||
|
|
||||||
def update_selected_cell(self, selected_tile, *args):
|
def on_selected_cell(self, selected_tile, *args):
|
||||||
self.status.text = selected_tile.map_display_text()
|
self.status.text = selected_tile.map_display_text()
|
||||||
with self.status.canvas.before:
|
with self.status.canvas.before:
|
||||||
Color(*selected_tile.terrain_colour)
|
Color(*selected_tile.terrain_colour)
|
||||||
|
|
|
@ -5,8 +5,8 @@
|
||||||
id: _game
|
id: _game
|
||||||
main_map: _main_map
|
main_map: _main_map
|
||||||
status: _stats
|
status: _stats
|
||||||
map_rows: 10
|
map_rows: 5
|
||||||
map_cols: 10
|
map_cols: 5
|
||||||
BoxLayout:
|
BoxLayout:
|
||||||
orientation: 'horizontal'
|
orientation: 'horizontal'
|
||||||
BoxLayout:
|
BoxLayout:
|
||||||
|
|
Loading…
Reference in New Issue