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
Terrains = {
'plain': {
'color': '#71CD00'
},
'hill': {
'color': '#505355'
},
'water': {
'color': '#5D88F8'
},
'sand': {
'color': '#F9CF29'
},
'forest': {
'color': '#10A71E'
},
'city': {
'color': '#A1A5AA'
}
}
@enum.unique
class SubstrateTypes(enum.Enum):
grass = 0
sand = 1
rock = 2
water = 3
ice = 4
space = 5
def choose_random_terrain():
@enum.unique
class Features(enum.Enum):
# TODO: Maybe separate out into individual types of features and their densities.
empty = 0
brush = 1
forest = 2
jungle = 3
road = 4
ruins = 5
town = 6
city = 7
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)
terrain = 'plain'
# 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:
terrain = 'forest'
substrate = SubstrateTypes.sand
elif 20 < random_terrain_seed < 25:
terrain = 'hill'
substrate = SubstrateTypes.rock
elif 50 < random_terrain_seed < 60:
terrain = 'water'
substrate = SubstrateTypes.water
elif 70 < random_terrain_seed < 90:
terrain = 'sand'
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:
terrain = 'city'
return terrain
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 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):
@ -13,12 +50,10 @@ class MapTile(Label):
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'])
self.terrain = Terrain.choose_random_terrain()
self.terrain_colour = get_terrain_background_color(self.terrain)
radius = self.height / 2
self.bind(pos=self.update_pos, size=self.update_pos)
with self.canvas.after:
@ -28,7 +63,7 @@ class MapTile(Label):
# 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)
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(
@ -36,18 +71,20 @@ class MapTile(Label):
center_x=self.center_x,
center_y=self.center_y)
@property
def hex_radius(self):
return self.height / 2
def map_display_text(self):
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):
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)
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)
@ -60,17 +97,13 @@ class MapTile(Label):
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)
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):
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'))
@ -78,8 +111,7 @@ class MapTile(Label):
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.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
@ -87,13 +119,12 @@ class MapTile(Label):
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
Logger.debug('Dist: {} Diff: {}'.format(dist, dist - self.hex_radius))
return dist - self.hex_radius <= 0
class SpacerTile(Label):
def __init__(self):
super(SpacerTile, self).__init__(size_hint=(0.5, 1), text=':)')
super(SpacerTile, self).__init__(size_hint=(0.5, 1))