-
Notifications
You must be signed in to change notification settings - Fork 0
/
entities.py
138 lines (97 loc) · 3.48 KB
/
entities.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from state_machine import StateMachine
class BaseGameEntity:
_next_id = 0
def __init__(self):
BaseGameEntity._next_id += 1
self._id = BaseGameEntity._next_id
# Entities has a "pointer" to the game containing it.
# This allows to use missage_dispatcher in the game avoiding
# global variables or cross modules.
self.game = None
@property
def id(self):
return self._id
def update(self):
raise NotImplementedError('Entity does not implement update function')
def handle_message(self, t):
raise NotImplementedError('Entity does not implement message handling')
# A superclass "Character" could be valuable to group data and functions common
# to Miner and Wife, and make easier to add new characters to the game.
class Miner(BaseGameEntity):
max_fatigue = 10
max_thirst = 10
max_gold = 10
comfort_level = 20
def __init__(self, name, global_state=None, current_state=None):
BaseGameEntity.__init__(self)
self._name = name
self.state_machine = StateMachine(self)
self.location = None
self._gold_carried = 0
self._money_in_bank = 0
self._thirst = 0
self._fatigue = 0
# Relations
self.wife = None
# Initial states
self.state_machine.global_state = global_state
self.state_machine.current_state = current_state
@property
def name(self):
return self._name
@property
def money_in_bank(self):
return self._money_in_bank
def update(self):
self._thirst += 1
self.state_machine.update()
def handle_message(self, t):
self.state_machine.handle_message(t)
def change_location(self, new_location):
self.location = new_location
def add_to_gold_carried(self, value):
self._gold_carried += value
# Assuming you can add negative amount of gold...
if self._gold_carried < 0:
self._gold_carried = 0
def add_to_wealth(self):
self._money_in_bank += self._gold_carried
self._gold_carried = 0
def increase_fatigue(self):
self._fatigue += 1
def decrease_fatigue(self):
self._fatigue -= 1
def buy_and_drink_wiskey(self):
self._thirst = 0
self._money_in_bank -= 2
def pockets_full(self):
return self._gold_carried >= Miner.max_gold
def thirsty(self):
return self._thirst >= Miner.max_thirst
def wealthy(self):
return self._money_in_bank >= Miner.comfort_level
def fatigued(self):
return self._fatigue >= Miner.max_fatigue
def say(self, s):
print('\x1b[0;32;40m' + '{0} : '.format(self.name) + s + '\x1b[0m')
class Wife(BaseGameEntity):
def __init__(self, name, global_state=None, current_state=None):
BaseGameEntity.__init__(self)
self._name = name
self.state_machine = StateMachine(self)
self.location = None
self.cooking = False
self.husband = None
self.state_machine.global_state = global_state
self.state_machine.current_state = current_state
@property
def name(self):
return self._name
def update(self):
self.state_machine.update()
def handle_message(self, t):
self.state_machine.handle_message(t)
def change_location(self, new_location):
self.location = new_location
def say(self, s):
print('\x1b[0;31;40m' + '{0} : '.format(self.name) + s + '\x1b[0m')