/
entities.py
199 lines (178 loc) · 5.39 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# TODO: destroy method, remove all references
# TODO: monsters
# TODO: player stats
# TODO: monster stats
from bitstring import BitString
from constants import *
def create_feature(x, y, t):
if not t in FEATURE_TYPES:
print 'Invalid feature ' + t
return None
if t == '+' or t == "'":
if t == '+':
opened = False
else:
opened = True
feature = Door(x, y, opened)
elif t == 'T':
feature = Tree(x, y)
return feature
# BASE CLASSES
class Entity(object):
def __init__(self, x, y):
self.x = x
self.y = y
self.t = ''
self.tile = None
self.properties = BitString('0b00')
self.color = None
self.parent = None
def update_tile(self):
if self.tile:
self.tile.properties[PASSABLE] = self.properties[PASSABLE]
self.tile.properties[BLOCK_SIGHT] = self.properties[BLOCK_SIGHT]
def destroy(self):
try:
x, y = self.x, self.y
self.parent.remove_child(self)
return x, y
except:
print 'Destroy method failed. '
def __repr__(self):
return self.t
class Feature(Entity):
def __init__(self, x, y, t):
self.t = '+'
class Item(Entity):
# pickable, destructible, container,
pass
class Being(Entity):
def __init__(self, x, y, name):
Entity.__init__(self, x, y)
self.name = name
self.properties[PASSABLE] = 0
self.ap = 1
self.hp = 100
self.energy = 100
def move(self, direction):
if self.parent:
if direction == UP:
x = self.x
y = self.y - 1
elif direction == UP_RIGHT:
x = self.x + 1
y = self.y - 1
elif direction == RIGHT:
x = self.x + 1
y = self.y
elif direction == DOWN_RIGHT:
x = self.x + 1
y = self.y + 1
elif direction == DOWN:
x = self.x
y = self.y + 1
elif direction == DOWN_LEFT:
x = self.x - 1
y = self.y + 1
elif direction == LEFT:
x = self.x - 1
y = self.y
elif direction == UP_LEFT:
x = self.x - 1
y = self.y - 1
else:
print 'Invalid move direction. '
return
target_tile = self.parent.mapobj.get_tile(x, y)
if not target_tile:
return
if target_tile.properties[PASSABLE]:
self.tile.remove_child(self)
target_tile.add_child(self)
self.x = x
self.y = y
self.ap -= .5
else:
#TODO: if a move failed, do not take a turn
print 'not passable'
class Overlay(Entity):
def __init__(self, x, y):
Entity.__init__(self, x, y)
# BEINGS
class Player(Being):
def __init__(self, x, y, name):
Being.__init__(self, x, y, name)
self.t = '@'
self.weapon = 'fist'
def attack(self, target):
# check if in range
pass
class Monster(Being):
def __init__(self, x, y, namet):
Being.__init__(self, x, y, name)
self.t = 'z'
# FEATURES
class Door(Entity):
def __init__(self, x, y, opened):
Entity.__init__(self, x, y)
self.opened = opened
if opened:
self.t = "'"
self.properties[PASSABLE] = 1
self.properties[BLOCK_SIGHT] = 0
else:
self.t = '+'
self.properties[PASSABLE] = 0
self.properties[BLOCK_SIGHT] = 1
def open(self):
self.opened = True
self.t = "'"
self.properties[PASSABLE] = 1
self.properties[BLOCK_SIGHT] = 0
self.update_tile()
def close(self):
self.opened = False
self.t = "+"
self.properties[PASSABLE] = 0
self.properties[BLOCK_SIGHT] = 1
self.update_tile()
class Cursor(Overlay):
def __init__(self, x, y):
Overlay.__init__(self, x, y)
self.t = '0'
def move(self, direction):
if self.parent:
if direction == UP:
x = self.x
y = self.y - 1
elif direction == UP_RIGHT:
x = self.x + 1
y = self.y - 1
elif direction == RIGHT:
x = self.x + 1
y = self.y
elif direction == DOWN_RIGHT:
x = self.x + 1
y = self.y + 1
elif direction == DOWN:
x = self.x
y = self.y + 1
elif direction == DOWN_LEFT:
x = self.x - 1
y = self.y + 1
elif direction == LEFT:
x = self.x - 1
y = self.y
elif direction == UP_LEFT:
x = self.x - 1
y = self.y - 1
else:
print 'Invalid move direction. '
return
area = self.parent.area
#if ((x >= area['start_x'] and x < area['end_x'])
#and (y >= area['start_y'] and y < area['end_y'])):
if (x in range(area['start_x'], area['end_x']) and
y in range(area['start_y'], area['end_y'])):
self.x = x
self.y = y