doric-engine/doric/tiles.py

102 lines
2.9 KiB
Python
Raw Permalink Normal View History

2016-11-15 12:10:47 -05:00
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
2016-11-15 12:10:47 -05:00
from doric.map import Coordinates
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'
}
}
2016-11-15 12:10:47 -05:00
class MapTile(Label):
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)
self.terrain = Terrain.choose_random_terrain()
2016-11-15 09:19:41 -05:00
2016-11-15 19:54:46 -05:00
with self.canvas:
Color(*self.terrain_colour)
2016-11-15 19:54:46 -05:00
self.tile_background = Ellipse(pos=(self.x, self.y), size=(self.height, self.height), segments=6)
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-11-15 19:54:46 -05:00
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
def hex_radius(self):
return self.height / 2
def map_display_text(self):
2016-11-15 12:10:47 -05:00
return "{}\n{} \n {}".format(
self.coords.even_r_coordinate_text(), self.coords.cube_coordinate_text(), self.terrain.description())
2016-11-15 19:54:46 -05:00
def update_positions(self, instance, value):
self.tile_background.pos = (self.x, self.y)
self.tile_background.size = (self.height, self.height)
2016-11-15 09:19:41 -05:00
self.coord_label.center_x = self.center_x
self.coord_label.center_y = self.center_y
def on_touch_down(self, touch):
2016-11-15 12:10:47 -05:00
if super(MapTile, self).on_touch_down(touch):
return False
if not self.collide_with_bounding_circle(touch.x, touch.y):
return False
2016-11-15 19:54:46 -05:00
self.parent.parent.game.on_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.
2016-11-15 19:54:46 -05:00
dist = Vector(self.center_x, self.center_y).distance((coord_x, coord_y))
Logger.debug('Distance: ({:.2f}, {:.2f}) -> ({:.2f}, {:.2f})'.format(
self.center_x, self.center_y, coord_x, coord_y))
Logger.debug('Distance: {:.2f} / Diff: {:.2f}'.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):
super(SpacerTile, self).__init__(size_hint=(0.5, 1))