Extract terrain definitions to standalone module.

This commit is contained in:
Dorian 2016-11-15 14:41:25 -05:00
parent 89e74bb7a6
commit ef675ed575
2 changed files with 125 additions and 55 deletions

View File

@ -1,38 +1,77 @@
import enum
import random import random
Terrains = {
'plain': { @enum.unique
'color': '#71CD00' class SubstrateTypes(enum.Enum):
}, grass = 0
'hill': { sand = 1
'color': '#505355' rock = 2
}, water = 3
'water': { ice = 4
'color': '#5D88F8' space = 5
},
'sand': {
'color': '#F9CF29'
},
'forest': {
'color': '#10A71E'
},
'city': {
'color': '#A1A5AA'
}
}
def choose_random_terrain(): @enum.unique
random_terrain_seed = random.randint(0, 100) class Features(enum.Enum):
terrain = 'plain' # TODO: Maybe separate out into individual types of features and their densities.
if 0 < random_terrain_seed < 20: empty = 0
terrain = 'forest' brush = 1
elif 20 < random_terrain_seed < 25: forest = 2
terrain = 'hill' jungle = 3
elif 50 < random_terrain_seed < 60: road = 4
terrain = 'water' ruins = 5
elif 70 < random_terrain_seed < 90: town = 6
terrain = 'sand' city = 7
elif 90 < random_terrain_seed < 100:
terrain = 'city'
return terrain class Terrain(object):
def __init__(self, substrate=SubstrateTypes.grass, features=Features.empty, elevation=0):
self.substrate = substrate
self.elevation = elevation
self.features = features
@staticmethod
def choose_random_terrain():
random_terrain_seed = random.randint(0, 100)
# TODO: Add in ability to do weighted randomness of terrain based on map, overall terrain type.
substrate = SubstrateTypes.grass
# if 0 < random_terrain_seed < 20:
# substrate = SubstrateTypes.space
# elif 20 < random_terrain_seed < 25:
# substrate = SubstrateTypes.rock
# elif 50 < random_terrain_seed < 60:
# substrate = SubstrateTypes.water
# elif 70 < random_terrain_seed < 90:
# substrate = SubstrateTypes.sand
# elif 90 < random_terrain_seed < 100:
# substrate = SubstrateTypes.ice
if 0 < random_terrain_seed < 20:
substrate = SubstrateTypes.sand
elif 20 < random_terrain_seed < 25:
substrate = SubstrateTypes.rock
elif 50 < random_terrain_seed < 60:
substrate = SubstrateTypes.water
elif 70 < random_terrain_seed < 90:
substrate = SubstrateTypes.sand
# TODO Add in features selection, add in heighted randomness
features = Features.empty
if 0 < random_terrain_seed < 20:
features = Features.forest
elif 20 < random_terrain_seed < 25:
features = Features.brush
elif 50 < random_terrain_seed < 60:
features = Features.jungle
elif 70 < random_terrain_seed < 90:
features = Features.ruins
elif 90 < random_terrain_seed < 100:
features = Features.city
elevation = random.randint(-5, 5)
return Terrain(substrate=substrate, features=features, elevation=elevation)
def description(self):
return ", ".join([self.substrate.name, self.features.name, "height: {}".format(self.elevation)])

View File

@ -5,7 +5,44 @@ from kivy.uix.label import Label
from kivy.vector import Vector from kivy.vector import Vector
from doric.map import Coordinates from doric.map import Coordinates
from doric.terrain import choose_random_terrain, Terrains 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)
class MapTile(Label): class MapTile(Label):
@ -13,12 +50,10 @@ class MapTile(Label):
super(MapTile, self).__init__(**kwargs) super(MapTile, self).__init__(**kwargs)
self.coords = Coordinates(row, col) self.coords = Coordinates(row, col)
self.selected = False
# Pick a random terrain for each hex. # Pick a random terrain for each hex.
self.terrain = choose_random_terrain() self.terrain = Terrain.choose_random_terrain()
self.terrain_colour = kivy.utils.get_color_from_hex(Terrains[self.terrain]['color']) self.terrain_colour = get_terrain_background_color(self.terrain)
radius = self.height / 2
self.bind(pos=self.update_pos, size=self.update_pos) self.bind(pos=self.update_pos, size=self.update_pos)
with self.canvas.after: with self.canvas.after:
@ -28,7 +63,7 @@ class MapTile(Label):
# Create the outline of hexagon, based off the centre of the hex. # Create the outline of hexagon, based off the centre of the hex.
Color(*kivy.utils.get_color_from_hex('#000000')) Color(*kivy.utils.get_color_from_hex('#000000'))
self.ell = Line(circle=(self.center_x, self.center_y, radius, 0, 360, 6), width=2) self.ell = Line(circle=(self.center_x, self.center_y, self.hex_radius, 0, 360, 6), width=2)
Color(0, 0, 0, 1) Color(0, 0, 0, 1)
self.coord_label = Label( self.coord_label = Label(
@ -36,18 +71,20 @@ class MapTile(Label):
center_x=self.center_x, center_x=self.center_x,
center_y=self.center_y) center_y=self.center_y)
@property
def hex_radius(self):
return self.height / 2
def map_display_text(self): def map_display_text(self):
return "{}\n{} \n {}".format( return "{}\n{} \n {}".format(
self.coords.even_r_coordinate_text(), self.coords.cube_coordinate_text(), self.terrain) self.coords.even_r_coordinate_text(), self.coords.cube_coordinate_text(), self.terrain.description())
def update_pos(self, instance, value): def update_pos(self, instance, value):
self.canvas.clear() self.canvas.clear()
radius = self.height / 2
# Resize the outline of the cell. # Resize the outline of the cell.
self.ell.circle = (self.center_x, self.center_y, radius, 0, 360, 6) self.ell.circle = (self.center_x, self.center_y, self.hex_radius, 0, 360, 6)
# Resize the actual cell. # Resize the actual cell.
self.solid.pos = (self.x, self.y) self.solid.pos = (self.x, self.y)
@ -60,17 +97,13 @@ class MapTile(Label):
if super(MapTile, self).on_touch_down(touch): if super(MapTile, self).on_touch_down(touch):
return False return False
coord_x, coord_y = self.coords.even_r_coords
with self.canvas.after: with self.canvas.after:
Color(*kivy.utils.get_color_from_hex('#000000')) Color(*kivy.utils.get_color_from_hex('#000000'))
radius = self.height / 2 self.ell = Line(circle=(self.center_x, self.center_y, self.hex_radius, 0, 360, 6), width=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): if not self.collide_with_bounding_circle(touch.x, touch.y):
return False return False
Logger.debug('Selected: ({}, {})'.format(coord_x, coord_y))
with self.canvas.after: with self.canvas.after:
if 'button' in touch.profile and touch.button == 'left': if 'button' in touch.profile and touch.button == 'left':
Color(*kivy.utils.get_color_from_hex('#00FF00')) Color(*kivy.utils.get_color_from_hex('#00FF00'))
@ -78,8 +111,7 @@ class MapTile(Label):
if 'button' in touch.profile and touch.button == 'right': if 'button' in touch.profile and touch.button == 'right':
# TODO Will refactor to have separate on_touch_up for selected target hex instead. # TODO Will refactor to have separate on_touch_up for selected target hex instead.
Color(*kivy.utils.get_color_from_hex('#FF0000')) Color(*kivy.utils.get_color_from_hex('#FF0000'))
radius = self.height / 2 self.ell = Line(circle=(self.center_x, self.center_y, self.hex_radius, 0, 360, 6), width=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) self.parent.parent.game.update_selected_cell(self)
return True return True
@ -87,13 +119,12 @@ class MapTile(Label):
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)) Logger.debug('Detected: ({}, {})'.format(coord_x, coord_y))
radius = self.height / 2
dist = Vector(self.x, self.y).distance((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)) Logger.debug('({}, {}) -> ({}, {})'.format(self.x, self.y, coord_x, coord_y))
Logger.debug('Dist: {} Diff: {}'.format(dist, dist - radius)) Logger.debug('Dist: {} Diff: {}'.format(dist, dist - self.hex_radius))
return dist - radius <= 0 return dist - self.hex_radius <= 0
class SpacerTile(Label): class SpacerTile(Label):
def __init__(self): def __init__(self):
super(SpacerTile, self).__init__(size_hint=(0.5, 1), text=':)') super(SpacerTile, self).__init__(size_hint=(0.5, 1))