-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_entity.py
159 lines (131 loc) · 4.46 KB
/
base_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
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
#-*- coding: utf-8 -*-
from pygame.locals import K_UP as UP
from pygame.sprite import Sprite, RenderUpdates as Group
from pygame.rect import Rect
from base_animation import BaseAnimation
class BaseEntity(Sprite):
"""Base Class Entity to use for games entity.
Extends pygame.Sprite class
Use to manage entities, for space collapsing
and draw animation space.
Instance variable:
- rect : pygame.Rect class
Use for collapse beetween entities
- direction : direction state
UP = 273
DOWN = 274
RIGHT = 275
LEFT = 276
- speed : list of speed mvt : [abs, ord]
"""
def __init__(self, name, rect_data, speed, max_frame, max_frame_delay, img):
""" Init.
- rect_data : list contains =>
- x : position x
- y : position y
- w : width of rect_collapse
- h : height of collase
- direction
"""
super(BaseEntity, self).__init__()
self.name = name
self.rect = None
self.image = None
self.childs = Group()
self.rect_collapse = Rect(rect_data)
self.speed = speed
self.direction = UP
# Create animation for the entity
self.animation = self.init_animation(max_frame, max_frame_delay, img)
def add_child(self, child):
"""Add a child entity."""
self.childs.add(child)
def remove_child(self, child):
"""Remove a child entity."""
self.childs.remove(child)
def direction_get(self):
return self.direction
def direction_set(self, direction):
self.direction = direction
def get_rect(self, value=0):
"""Return rect
0 = rect(actual rect to use)
1 = rect_animation
2 = rect_collapse
"""
if value == 1:
return self.image.get_rect()
elif value == 2:
return self.rect_collapse
else:
return self.rect
def init_animation(self, max_frame, max_frame_delay, img):
"""Function for animation initialisation.
Need to be defined.
"""
pass
def __str__(self):
"""Custom __str__."""
string = (
u"<Entity : %s -- Pos (%s,%s)>\n" % (
str(self.name),
str(self.rect_collapse[0]),
str(self.rect_collapse[1]),
)
)
return string
def move(self, move_direction):
"""Basic mouvement.
Basic calcul tomove the entity, defined by direction parameter
Reimplements if you need to change move's pattern
"""
x, y = self.rect_collapse.topleft
direction_num = move_direction - UP
if direction_num == 0:
move = (0, -1)
elif direction_num == 1:
move = (0, 1)
elif direction_num == 2:
move = (1, 0)
elif direction_num == 3:
move = (-1, 0)
x = x + (self.speed[0] * move[0])
y = y + (self.speed[1] * move[1])
self.rect_collapse.left = x
self.rect_collapse.top = y
def stop(self):
"""Basic stop.
Stop the mouvement of the entity
Reimplements if you need to change move's pattern
"""
pass
def update(self, movement = None):
"""Update function.
Basic update position of the entity (move or stop)
Redefine it for your own purpose
Action use by pygame.sprite.Group.update() function.
"""
if movement is None:
self.stop()
self.animation.stop()
else:
self.direction = movement
self.move(movement)
self.animation.update()
self.setup_animation(self.direction)
self.childs.update()
def setup_collapse(self):
"""Setup variable.
Set up rect attribute for collapse eval"""
self.rect = self.rect_collapse
def setup_animation(self, direction):
"""Setup variable.
Set up rect attribute for animation draw
Be careful :: is function move anim_sprite to center
with rect_collapse
Catch image.get_rect directly will give you the wrong coordinate
"""
self.image = self.animation.get_sprite(direction).convert_alpha()
rect_anim_position = self.image.get_rect()
rect_anim_position.center = self.rect_collapse.center
self.rect = rect_anim_position