Add basic terrain to map.

This commit is contained in:
Dorian 2016-07-16 22:17:23 -04:00
parent dd2864186e
commit 7fab78fa8a
1 changed files with 51 additions and 7 deletions

58
main.py
View File

@ -1,4 +1,5 @@
import collections import collections
import random
from kivy import app, properties from kivy import app, properties
from kivy.logger import Logger from kivy.logger import Logger
@ -9,6 +10,43 @@ from kivy.vector import Vector
import kivy.utils import kivy.utils
MapCoords = collections.namedtuple('MapCoords', ['row', 'col']) MapCoords = collections.namedtuple('MapCoords', ['row', 'col'])
Terrains = {
'plain': {
'color': '#71CD00'
},
'hill':{
'color': '#505355'
},
'water':{
'color': '#5D88F8'
},
'sand':{
'color': '#F9CF29'
},
'forest':{
'color': '#10A71E'
},
'city':{
'color': '#A1A5AA'
}
}
def choose_random_terrain():
random_terrain_seed = random.randint(0, 100)
terrain = 'plain'
if 0 < random_terrain_seed < 20:
terrain = 'forest'
elif 20 < random_terrain_seed < 25:
terrain = 'hill'
elif 50 < random_terrain_seed < 60:
terrain = 'water'
elif 70 < random_terrain_seed < 90:
terrain = 'sand'
elif 90 < random_terrain_seed < 100:
terrain = 'city'
return terrain
class StrategyGame(FloatLayout): class StrategyGame(FloatLayout):
@ -27,10 +65,12 @@ class StrategyGame(FloatLayout):
# Add hex cells to make up the map. # Add hex cells to make up the map.
hex_cell = HexMapCell(row, col) hex_cell = HexMapCell(row, col)
hex_cell.disabled = True
self.main_map.add_widget(hex_cell) self.main_map.add_widget(hex_cell)
# Add overlay conditionally. # Add overlay conditionally.
if (row % 6 == 1 and col % 2 == 1) or (row % 6 == 4 and col % 2 == 0) and (col > 0): if (row % 6 == 1 and col % 2 == 1) or (row % 6 == 4 and col % 2 == 0) and (col > 0):
hex_cell.disabled = False
hex_cell.visible_on_map = True hex_cell.visible_on_map = True
# Determine the location of the solid hexagon cell. Needs to be offset from the centre of the hex. # Determine the location of the solid hexagon cell. Needs to be offset from the centre of the hex.
@ -45,8 +85,11 @@ class StrategyGame(FloatLayout):
Color(*kivy.utils.get_color_from_hex('#A1A5AA')) Color(*kivy.utils.get_color_from_hex('#A1A5AA'))
hex_cell.ell = Line(circle=(hex_cell.x, hex_cell.y, radius, 0, 360, 6), width=2) hex_cell.ell = Line(circle=(hex_cell.x, hex_cell.y, radius, 0, 360, 6), width=2)
# Pick a random terrain for each hex.
hex_cell.terrain = choose_random_terrain()
# Create the solid background of the hexagon, from the bottom left coordinate of the hex. # Create the solid background of the hexagon, from the bottom left coordinate of the hex.
hex_cell.terrain_colour = kivy.utils.get_random_color(alpha=.5) hex_cell.terrain_colour = kivy.utils.get_color_from_hex(Terrains[hex_cell.terrain]['color'])
Color(*hex_cell.terrain_colour) Color(*hex_cell.terrain_colour)
hex_cell.solid = Ellipse(pos=(solid_x, solid_y), size=solid_size, segments=6) hex_cell.solid = Ellipse(pos=(solid_x, solid_y), size=solid_size, segments=6)
@ -74,13 +117,14 @@ class HexMapCell(Label):
self.selected = False self.selected = False
self.visible_on_map = False self.visible_on_map = False
self.terrain_colour = Color(0, 0, 0, 1) self.terrain_colour = Color(0, 0, 0, 1)
self.terrain = ''
def map_coordinates(self): def map_coordinates(self):
return self.coords.row / 3, self.coords.col / 2 return self.coords.row / 3, self.coords.col / 2
def map_display_text(self): def map_display_text(self):
map_x, map_y = self.map_coordinates() map_x, map_y = self.map_coordinates()
return "({}, {})".format(map_x, map_y) return "({}, {}) \n {}".format(map_x, map_y, self.terrain)
def update_pos(self, instance, value): def update_pos(self, instance, value):
# Determine the location of the solid hexagon cell. Needs to be offset from the centre of the hex. # Determine the location of the solid hexagon cell. Needs to be offset from the centre of the hex.
@ -107,12 +151,12 @@ class HexMapCell(Label):
if not self.visible_on_map: if not self.visible_on_map:
return False return False
Logger.debug("Visibly Touched! {}, {} at {}, {}".format(touch.x, touch.y, coord_x, coord_y)) with self.canvas.after:
Color(*kivy.utils.get_color_from_hex('#A1A5AA'))
radius = 2 * self.height
self.ell = Line(circle=(self.x, self.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):
with self.canvas.after:
Color(*kivy.utils.get_color_from_hex('#A1A5AA'))
radius = 2 * self.height
self.ell = Line(circle=(self.x, self.y, radius, 0, 360, 6), width=2)
return False return False
Logger.debug('Selected: ({}, {})'.format(coord_x, coord_y)) Logger.debug('Selected: ({}, {})'.format(coord_x, coord_y))