-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameObject.py
49 lines (41 loc) · 1.33 KB
/
GameObject.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
class GameObject(object):
"""docstring for GameObject"""
def __init__(self, state, (x, y), name, character, color, blocks=True, fighter=None, ai=None):
self.state = state
self.x = x
self.y = y
self.name = name
self.character = character
self.color = color
self.blocks = blocks
self.fighter = fighter
if self.fighter:
self.fighter.owner = self
self.ai = ai
if self.ai:
self.ai.owner = self
def move(self, (dx, dy), state):
if not state.is_blocked((self.x + dx, self.y + dy)):
self.x += dx
self.y += dy
return True
else:
return False
def set_location(self, (x, y)):
self.x = x
self.y = y
def position(self):
return (self.x, self.y)
def distance_to(self, other):
return self.state.distance(self, other)
def distance_to_player(self):
return self.state.distance_to_player(self)
def move_towards(self, target):
dx = target.x - self.x
dy = target.y - self.y
distance = self.distance_to(target)
dx = int(round(dx / distance))
dy = int(round(dy / distance))
self.move((dx, dy), self.state)
def move_towards_player(self):
self.move_towards(self.state.player)