/
gameboard.py
91 lines (69 loc) · 2.63 KB
/
gameboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from tobiont import globals
from tobiont import resource
from tobiont import weather
class Gameboard(object):
"""Manages the gameboard representing the simulation world."""
_board = []
def __init__(self):
super(Gameboard, self).__init__()
for x in range(0, globals.BOARD_X_SIZE):
grid_column = []
for y in range(0, globals.BOARD_Y_SIZE):
grid_column.append(self._createGameSquare(x, y))
self._board.append(grid_column)
def _createGameSquare(self, x, y):
square_weather = weather.RaineyWeather('defaultRegion', 'RaineyBiome')
return GameSquare({'basic': 1}, square_weather, (x, y))
def getSquare(self, x, y):
return self._board[x][y]
def processGameboard(self):
for x in range(0, globals.BOARD_X_SIZE):
for y in range(0, globals.BOARD_Y_SIZE):
self._board[x][y].processWeather()
class GameSquare(object):
"""A single location square in the gameboard."""
_observers = []
def __init__(self, resources, weather, location_tuple):
super(GameSquare, self).__init__()
self._resources = resources.copy()
self._weather_rules = weather
self._grid_x_y_tuple = location_tuple
self._contents = None
def moveCreatureToSquare(self, creature):
"""Adds a creature to this square or throws SquareOccupiedError."""
self._contents = creature
def getResource(self, type):
"""Returns current square resources"""
return self._resources[type]
def withdrawResource(self, resource):
"""Removes a resource from the square and returns it."""
if resource.getResource() == 0:
return
elif self._resources[resource.getType()] <= resource.getResource():
self._resources[resource.getType()] == 0
else:
self._resources[resource.getType()] -= resource.getResource()
def addResource(self, resource):
"""Adds a resource to this square."""
if resource.getResource() == 0:
return
else:
self._resources[resource.getType()] += resource.getResource()
def processWeather(self):
"""Runs the weather rules of the square."""
self._weather_rules.run(self)
def changeWeather(self, new_weather_rules):
"""Sets new weather rules."""
self._weather_rules = new_weather_rules
def registerObserver(self, observer):
"""Adds an observer to this square.
observers will be notified of actions taken by this square.
"""
self._observers.append(observer)
def removeObserver(self, observer):
"""Removes a registered observer."""
self._observers.remove(observer)
def _alertObservers(self, message):
"""Internal alert method."""
for observer in self._observers:
observer.alert(message)