-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity.py
More file actions
48 lines (39 loc) · 1.56 KB
/
entity.py
File metadata and controls
48 lines (39 loc) · 1.56 KB
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
import math;
from render_functions import RenderOrder
class Entity:
"""
A generic object to represent players, enemies, items, etc.
"""
def __init__(self, x, y, char, color, name, blocks=False, render_order=RenderOrder.CORPSE, fighter=None, ai=None):
self.x = x;
self.y = y;
self.char = char;
self.color = color;
self.name = name;
self.blocks = blocks;
self.render_order = render_order
self.fighter = fighter;
self.ai = ai;
if self.fighter:
self.fighter.owner = self;
if self.ai:
self.ai.owner = self;
def move(self, dx, dy):
# Move the entity by a given amount
self.x += dx;
self.y += dy;
def move_towards(self, target_x, target_y, game_map, entities):
path = game_map.compute_path(self.x, self.y, target_x, target_y);
dx = path[0][0] - self.x;
dy = path[0][1] - self.y;
if game_map.walkable[path[0][0], path[0][1]] and not get_blocking_entities_at_location(entities, self.x + dx, self.y + dy):
self.move(dx, dy);
def distance_to(self, other):
dx = other.x - self.x;
dy = other.y - self.y;
return math.sqrt(dx ** 2 + dy ** 2);
def get_blocking_entities_at_location(entities, destination_x, destination_y):
for entity in entities:
if entity.blocks and entity.x == destination_x and entity.y == destination_y:
return entity;
return None;