doric-engine/doric/tiles.py

100 lines
3.8 KiB
Python

import kivy.utils
from kivy.graphics import Color, Line, Ellipse
from kivy.logger import Logger
from kivy.uix.label import Label
from kivy.vector import Vector
from doric.map import Coordinates
from doric.terrain import choose_random_terrain, Terrains
class MapTile(Label):
def __init__(self, row=0, col=0, **kwargs):
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'])
radius = self.height / 2
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.
Color(*self.terrain_colour)
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.
Color(*kivy.utils.get_color_from_hex('#000000'))
self.ell = Line(circle=(self.center_x, self.center_y, radius, 0, 360, 6), width=2)
Color(0, 0, 0, 1)
self.coord_label = Label(
text=self.coords.even_r_coordinate_text(),
center_x=self.center_x,
center_y=self.center_y)
def map_display_text(self):
return "{}\n{} \n {}".format(
self.coords.even_r_coordinate_text(), self.coords.cube_coordinate_text(), self.terrain)
def update_pos(self, instance, value):
self.canvas.clear()
radius = self.height / 2
# Resize the outline of the cell.
self.ell.circle = (self.center_x, self.center_y, 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_y = self.center_y
def on_touch_down(self, touch):
if super(MapTile, self).on_touch_down(touch):
return False
coord_x, coord_y = self.coords.even_r_coords
with self.canvas.after:
Color(*kivy.utils.get_color_from_hex('#000000'))
radius = self.height / 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):
return False
Logger.debug('Selected: ({}, {})'.format(coord_x, coord_y))
with self.canvas.after:
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'))
radius = self.height / 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)
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 = 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))
return dist - radius <= 0
class SpacerTile(Label):
def __init__(self):
super(SpacerTile, self).__init__(size_hint=(0.5, 1), text=':)')