2016-11-15 12:10:47 -05:00
|
|
|
import kivy.utils
|
|
|
|
from kivy.graphics import Color, Line, Ellipse
|
2016-07-16 23:41:44 -04:00
|
|
|
from kivy.logger import Logger
|
|
|
|
from kivy.uix.label import Label
|
|
|
|
from kivy.vector import Vector
|
|
|
|
|
2016-11-15 12:10:47 -05:00
|
|
|
from doric.map import Coordinates
|
2016-11-15 14:41:25 -05:00
|
|
|
from doric.terrain import Terrain
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: Migrate this into a better setup via inheritance or composition of MapTile info.
|
|
|
|
Terrains = {
|
|
|
|
'grass': {
|
|
|
|
'color': '#71CD00'
|
|
|
|
},
|
|
|
|
'hill': {
|
|
|
|
'color': '#505355'
|
|
|
|
},
|
|
|
|
'water': {
|
|
|
|
'color': '#5D88F8'
|
|
|
|
},
|
|
|
|
'sand': {
|
|
|
|
'color': '#F9CF29'
|
|
|
|
},
|
|
|
|
'forest': {
|
|
|
|
'color': '#10A71E'
|
|
|
|
},
|
|
|
|
'space': {
|
|
|
|
'color': '#000000'
|
|
|
|
},
|
|
|
|
'rock': {
|
|
|
|
'color': '#A1A5AA'
|
|
|
|
},
|
|
|
|
'city': {
|
|
|
|
'color': '#A1A5AA'
|
|
|
|
},
|
|
|
|
'ice': {
|
|
|
|
'color': '#EEEEEE'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def get_terrain_background_color(terrain):
|
|
|
|
hex_colour = Terrains[terrain.substrate.name]['color']
|
|
|
|
return kivy.utils.get_color_from_hex(hex_colour)
|
2016-07-16 23:41:44 -04:00
|
|
|
|
|
|
|
|
2016-11-15 12:10:47 -05:00
|
|
|
class MapTile(Label):
|
2016-07-16 23:41:44 -04:00
|
|
|
def __init__(self, row=0, col=0, **kwargs):
|
2016-11-15 12:10:47 -05:00
|
|
|
super(MapTile, self).__init__(**kwargs)
|
|
|
|
self.coords = Coordinates(row, col)
|
2016-11-09 22:36:05 -05:00
|
|
|
|
|
|
|
# Pick a random terrain for each hex.
|
2016-11-15 14:41:25 -05:00
|
|
|
self.terrain = Terrain.choose_random_terrain()
|
|
|
|
self.terrain_colour = get_terrain_background_color(self.terrain)
|
2016-11-09 22:36:05 -05:00
|
|
|
|
2016-11-15 09:19:41 -05:00
|
|
|
self.bind(pos=self.update_pos, size=self.update_pos)
|
|
|
|
|
2016-11-09 22:36:05 -05:00
|
|
|
with self.canvas.after:
|
|
|
|
# Create the solid background of the hexagon, from the bottom left coordinate of the hex.
|
|
|
|
Color(*self.terrain_colour)
|
2016-11-15 11:16:19 -05:00
|
|
|
self.solid = Ellipse(pos=(self.x, self.y), size=(self.height, self.height), segments=6)
|
2016-11-09 22:36:05 -05:00
|
|
|
|
|
|
|
# Create the outline of hexagon, based off the centre of the hex.
|
|
|
|
Color(*kivy.utils.get_color_from_hex('#000000'))
|
2016-11-15 14:41:25 -05:00
|
|
|
self.ell = Line(circle=(self.center_x, self.center_y, self.hex_radius, 0, 360, 6), width=2)
|
2016-11-09 22:36:05 -05:00
|
|
|
|
|
|
|
Color(0, 0, 0, 1)
|
|
|
|
self.coord_label = Label(
|
2016-11-15 12:10:47 -05:00
|
|
|
text=self.coords.even_r_coordinate_text(),
|
2016-11-15 09:19:41 -05:00
|
|
|
center_x=self.center_x,
|
|
|
|
center_y=self.center_y)
|
2016-07-16 23:41:44 -04:00
|
|
|
|
2016-11-15 14:41:25 -05:00
|
|
|
@property
|
|
|
|
def hex_radius(self):
|
|
|
|
return self.height / 2
|
|
|
|
|
2016-07-16 23:41:44 -04:00
|
|
|
def map_display_text(self):
|
2016-11-15 12:10:47 -05:00
|
|
|
return "{}\n{} \n {}".format(
|
2016-11-15 14:41:25 -05:00
|
|
|
self.coords.even_r_coordinate_text(), self.coords.cube_coordinate_text(), self.terrain.description())
|
2016-07-16 23:41:44 -04:00
|
|
|
|
|
|
|
def update_pos(self, instance, value):
|
2016-11-15 11:16:19 -05:00
|
|
|
|
|
|
|
self.canvas.clear()
|
|
|
|
|
2016-07-16 23:41:44 -04:00
|
|
|
# Resize the outline of the cell.
|
2016-11-15 14:41:25 -05:00
|
|
|
self.ell.circle = (self.center_x, self.center_y, self.hex_radius, 0, 360, 6)
|
2016-07-16 23:41:44 -04:00
|
|
|
|
2016-07-16 17:49:58 -04:00
|
|
|
# Resize the actual cell.
|
2016-11-15 09:19:41 -05:00
|
|
|
self.solid.pos = (self.x, self.y)
|
|
|
|
self.solid.size = (self.height, self.height)
|
2016-07-16 23:41:44 -04:00
|
|
|
|
2016-11-15 09:19:41 -05:00
|
|
|
self.coord_label.center_x = self.center_x
|
|
|
|
self.coord_label.center_y = self.center_y
|
2016-07-16 23:41:44 -04:00
|
|
|
|
|
|
|
def on_touch_down(self, touch):
|
2016-11-15 12:10:47 -05:00
|
|
|
if super(MapTile, self).on_touch_down(touch):
|
2016-07-16 23:41:44 -04:00
|
|
|
return False
|
|
|
|
|
|
|
|
with self.canvas.after:
|
|
|
|
Color(*kivy.utils.get_color_from_hex('#000000'))
|
2016-11-15 14:41:25 -05:00
|
|
|
self.ell = Line(circle=(self.center_x, self.center_y, self.hex_radius, 0, 360, 6), width=2)
|
2016-07-16 23:41:44 -04:00
|
|
|
|
|
|
|
if not self.collide_with_bounding_circle(touch.x, touch.y):
|
|
|
|
return False
|
|
|
|
|
|
|
|
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'))
|
2016-11-15 14:41:25 -05:00
|
|
|
self.ell = Line(circle=(self.center_x, self.center_y, self.hex_radius, 0, 360, 6), width=2)
|
2016-07-16 23:41:44 -04:00
|
|
|
|
2016-11-15 09:19:41 -05:00
|
|
|
self.parent.parent.game.update_selected_cell(self)
|
2016-07-16 23:41:44 -04:00
|
|
|
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))
|
|
|
|
dist = Vector(self.x, self.y).distance((coord_x, coord_y))
|
|
|
|
Logger.debug('({}, {}) -> ({}, {})'.format(self.x, self.y, coord_x, coord_y))
|
2016-11-15 14:41:25 -05:00
|
|
|
Logger.debug('Dist: {} Diff: {}'.format(dist, dist - self.hex_radius))
|
|
|
|
return dist - self.hex_radius <= 0
|
2016-11-15 12:10:47 -05:00
|
|
|
|
|
|
|
|
|
|
|
class SpacerTile(Label):
|
|
|
|
def __init__(self):
|
2016-11-15 14:41:25 -05:00
|
|
|
super(SpacerTile, self).__init__(size_hint=(0.5, 1))
|