-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.py
105 lines (87 loc) · 2.97 KB
/
entity.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
import pygame
class Entity(object):
TYPE_PLAYER = 0
TYPE_OBJECT = 1
TYPE_OBJECT_DYNAMIC = 2
TYPE_ENEMY_STATIC = 3
TYPE_ENEMY_DYNAMIC = 4
TYPE_PLAYER_MELEE = 5
TYPE_PLAYER_RANGED = 6
TYPE_POTION = 7
TYPE_CHAMELEON = 8
# Parent class of all entities in the game
#
# Member variables:
# Surface image
# Rect collider
# Vec2 position
# Vec2 velocity
# int type (TYPE_[PLAYER,OBJECT,ENEMY])
# Animation animation
#
# Abstract methods:
# __init__()
# collide()
# update_logic()
def __init__(self):
raise NotImplementedError("Attempt to instantiate abstract entity")
def update_logic(self, input_m, entities, dt):
raise NotImplementedError()
def collide(self, other, axis):
raise NotImplementedError()
def collides_with(self, other):
if self == other:
return False
for c in self.colliders():
for o in other.colliders():
if c.colliderect(o):
return True
return False
def die():
pass
def take_damage(self, damage, source):
pass
def match(self, other, axis):
if axis == 'y':
if self.velocity.y < 0:
self.match_top(other)
else:
self.match_bottom(other)
self.velocity.y = 0
else:
if self.velocity.x < 0:
self.match_right(other)
else:
self.match_left(other)
self.velocity.x = 0
def resolve_collision(self, other, axis, dt):
if axis == 'y':
self.position.y -= self.velocity.y * dt / 1000
if self.type == Entity.TYPE_PLAYER \
and self.velocity.y > 0:
self.can_jump = True
self.match_bottom(other)
self.velocity.y = 0
else:
self.position.x -= self.velocity.x * dt / 1000
self.velocity.x = 0
def match_top(self, other):
self.position.y = other.position.y + other.animation.current_frame().get_height()
self.reset_collider()
def match_bottom(self, other):
self.position.y = other.position.y - self.animation.current_frame().get_height()
if self.type == Entity.TYPE_PLAYER:
self.can_jump = True
self.reset_collider()
def match_left(self, other):
self.position.x = other.position.x - self.animation.current_frame().get_width()
self.reset_collider()
def match_right(self, other):
self.position.x = other.position.x + other.animation.current_frame().get_width()
self.reset_collider()
def colliders(self):
return [self.collider]
def reset_collider(self):
self.collider = pygame.Rect(self.position.x, self.position.y,
self.animation.current_frame().get_width(),
self.animation.current_frame().get_height())