2016-06-05 02:04:53 -04:00
|
|
|
|
2016-06-04 20:37:39 -04:00
|
|
|
from kivy import app, properties
|
2016-07-16 23:33:51 -04:00
|
|
|
from kivy.uix.label import Label
|
2016-06-04 14:31:07 -04:00
|
|
|
from kivy.uix.floatlayout import FloatLayout
|
2016-06-05 01:20:00 -04:00
|
|
|
from kivy.graphics import Color, Ellipse, Line
|
2016-06-05 02:04:53 -04:00
|
|
|
import kivy.utils
|
2016-07-16 17:49:58 -04:00
|
|
|
from hexmap import HexMapCell
|
2016-06-04 14:59:40 -04:00
|
|
|
|
2016-06-04 15:35:33 -04:00
|
|
|
|
2016-06-04 14:59:40 -04:00
|
|
|
class StrategyGame(FloatLayout):
|
2016-06-04 15:23:40 -04:00
|
|
|
main_map = properties.ObjectProperty(None)
|
2016-07-16 19:35:29 -04:00
|
|
|
status = properties.ObjectProperty(None)
|
2016-06-04 15:23:40 -04:00
|
|
|
map_rows = properties.NumericProperty(0)
|
|
|
|
map_cols = properties.NumericProperty(0)
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
super(StrategyGame, self).__init__(**kwargs)
|
|
|
|
|
|
|
|
number_of_regions = self.map_rows * self.map_cols
|
|
|
|
for region in xrange(0, number_of_regions):
|
|
|
|
row = region / self.map_cols
|
|
|
|
col = region % self.map_cols
|
2016-06-04 15:35:33 -04:00
|
|
|
|
2016-06-04 20:37:39 -04:00
|
|
|
# Add hex cells to make up the map.
|
2016-07-16 15:11:41 -04:00
|
|
|
hex_cell = HexMapCell(row, col)
|
2016-07-16 22:17:23 -04:00
|
|
|
hex_cell.disabled = True
|
2016-06-04 20:37:39 -04:00
|
|
|
self.main_map.add_widget(hex_cell)
|
2016-06-04 15:35:33 -04:00
|
|
|
|
2016-06-04 20:37:39 -04:00
|
|
|
# Add overlay conditionally.
|
2016-07-16 15:52:01 -04:00
|
|
|
if (row % 6 == 1 and col % 2 == 1) or (row % 6 == 4 and col % 2 == 0) and (col > 0):
|
2016-07-16 22:17:23 -04:00
|
|
|
hex_cell.disabled = False
|
2016-07-16 17:52:11 -04:00
|
|
|
hex_cell.visible_on_map = True
|
2016-07-16 14:00:48 -04:00
|
|
|
|
2016-07-16 14:12:56 -04:00
|
|
|
# Determine the location of the solid hexagon cell. Needs to be offset from the centre of the hex.
|
2016-07-16 13:51:50 -04:00
|
|
|
radius = 2 * hex_cell.height
|
2016-06-05 02:04:53 -04:00
|
|
|
solid_x = hex_cell.x - hex_cell.height*2
|
|
|
|
solid_y = hex_cell.y - hex_cell.height*2
|
|
|
|
solid_size = (4*hex_cell.height, 4*hex_cell.height)
|
2016-07-16 14:00:48 -04:00
|
|
|
|
2016-06-05 01:20:00 -04:00
|
|
|
with hex_cell.canvas.after:
|
2016-07-16 22:17:23 -04:00
|
|
|
# Pick a random terrain for each hex.
|
|
|
|
hex_cell.terrain = choose_random_terrain()
|
|
|
|
|
2016-07-16 14:12:56 -04:00
|
|
|
# Create the solid background of the hexagon, from the bottom left coordinate of the hex.
|
2016-07-16 22:17:23 -04:00
|
|
|
hex_cell.terrain_colour = kivy.utils.get_color_from_hex(Terrains[hex_cell.terrain]['color'])
|
2016-07-16 19:35:29 -04:00
|
|
|
Color(*hex_cell.terrain_colour)
|
2016-07-16 13:51:50 -04:00
|
|
|
hex_cell.solid = Ellipse(pos=(solid_x, solid_y), size=solid_size, segments=6)
|
2016-07-16 14:12:56 -04:00
|
|
|
|
2016-07-16 23:33:51 -04:00
|
|
|
# Create the outline of hexagon, based off the centre of the hex.
|
|
|
|
Color(*kivy.utils.get_color_from_hex('#000000'))
|
|
|
|
hex_cell.ell = Line(circle=(hex_cell.x, hex_cell.y, radius, 0, 360, 6), width=2)
|
|
|
|
|
|
|
|
Color(0, 0, 0, 1)
|
2016-07-16 15:11:41 -04:00
|
|
|
hex_cell.coord_label = Label(
|
2016-07-16 16:11:26 -04:00
|
|
|
text=hex_cell.map_display_text(),
|
|
|
|
center_x=hex_cell.x,
|
|
|
|
center_y=hex_cell.y)
|
2016-07-16 15:11:41 -04:00
|
|
|
|
2016-07-16 14:12:56 -04:00
|
|
|
# Bind the cell code so as to update its position and size when the parent widget resizes.
|
2016-06-05 01:20:00 -04:00
|
|
|
hex_cell.bind(pos=hex_cell.update_pos, size=hex_cell.update_pos)
|
|
|
|
|
2016-06-04 20:37:39 -04:00
|
|
|
|
2016-07-16 19:40:30 -04:00
|
|
|
def update_selected_cell(self, coords, terrain_colour, *args):
|
2016-07-16 19:35:29 -04:00
|
|
|
self.status.text = 'Coords: ({}, {})'.format(coords[0], coords[1])
|
2016-07-16 20:09:28 -04:00
|
|
|
with self.status.canvas.before:
|
|
|
|
Color(*terrain_colour)
|
|
|
|
Rectangle(pos=self.status.pos, size=self.status.size)
|
2016-07-16 19:35:29 -04:00
|
|
|
return True
|
|
|
|
|
2016-06-04 20:37:39 -04:00
|
|
|
|
|
|
|
class StrategyGameApp(app.App):
|
2016-06-04 14:31:07 -04:00
|
|
|
def build(self):
|
2016-06-04 14:59:40 -04:00
|
|
|
return StrategyGame()
|
2016-06-04 14:31:07 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2016-06-04 14:59:40 -04:00
|
|
|
StrategyGameApp().run()
|