-
Notifications
You must be signed in to change notification settings - Fork 1
/
actor.py
157 lines (122 loc) · 4.95 KB
/
actor.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
import pygame
from engine.Object import MovableObject
from saas import SaAS
class Actor(MovableObject, SaAS):
def __init__(self, portrait, animations, \
position, col_rect, actor_data=None):
MovableObject.__init__(self, animations['down'][3], position, col_rect)
SaAS.__init__(self)
self.is_player = False
self.portrait = portrait
if actor_data is not None:
self.name = actor_data['name']
self.dialogs = actor_data['dialogs']
self.id = actor_data
self.diff_x = self.crect.x - self.rect.x
self.diff_y = self.crect.y - self.rect.y
self.animations = animations
self.directions = { 'up' : [0, -1],
'down' : [0, 1],
'left' : [-1, 0],
'right' : [1, 0],
'none' : [0, 0]}
#temporarly
self.step = 5
self.a_step = 0
self.i_step = 0
self.prev_dir = 'none'
# def clamp(self, rect):
# self.rect.clamp_ip(rect)
# self.crect.topleft = \
# (self.rect.x+self.diff_x, self.rect.y+self.diff_y)
def action(self, target):
pass
def facing(self, target):
if target.crect.center[0] < self.crect.center[0]:
if target.crect.center[1] < self.crect.center[1]:
if (self.crect.center[0] - target.crect.center[0]) < \
(self.crect.center[1] - target.crect.center[1]):
self.image = self.animations['up'][3]
else:
self.image = self.animations['left'][3]
else:
if (self.crect.center[0] - target.crect.center[0]) < \
(target.crect.center[1] - self.crect.center[1]):
self.image = self.animations['down'][3]
else:
self.image = self.animations['left'][3]
else:
if target.crect.center[1] < self.crect.center[1]:
if (target.crect.center[0] - self.crect.center[0]) < \
(self.crect.center[1] - target.crect.center[1]):
self.image = self.animations['up'][3]
else:
self.image = self.animations['right'][3]
else:
if (target.crect.center[0] - self.crect.center[0]) < \
(target.crect.center[1] - self.crect.center[1]):
self.image = self.animations['down'][3]
else:
self.image = self.animations['right'][3]
def face(self, direction):
self.image = self.animations[direction][3]
def animate(self, direction):
"""
up, down, left, right, ...
"""
if direction == self.prev_dir:
if self.a_step == 1:
self.i_step += 1
self.a_step = 0
else:
self.a_step += 1
else:
self.a_step = 0
self.i_step = 0
self.prev_dir = direction
try:
self.image = self.animations[direction][self.i_step]
except IndexError:
self.i_step = 0
def set_center(self, coord):
self.rect.center = coord
self.crect.topleft = (self.rect.x+self.diff_x, self.rect.y+self.diff_y)
def move(self, coord):
crect = self.crect.move(coord)
# return old, new
return crect, self.crect
def loop(self, world):
_choice = self.prev_dir
direction = self.directions[_choice]
direction = [direction[0]*self.step, direction[1]*self.step]
# The same as in the cls.move, but its faster
# without an extra function call.
self.set_center(world.clamp(self.rect).center)
crect = self.crect.move(direction)
self.crect = world.check_collision(self, crect, self.crect)
self.rect.topleft = \
(self.crect.x-self.diff_x, self.crect.y-self.diff_y)
self
class Player(Actor):
def __init__(self, portrait, animations, position, col_rect):
Actor.__init__(self, portrait, animations, position, col_rect)
self.world = None
self.name = "Yves"
self.is_player = True
self.events = []
self.quest_events = {}
# Will work on a better solution
self.directions = { '[0, -1]' : 'up',
'[0, 1]' : 'down',
'[-1, 0]' : 'left',
'[1, 0]' : 'right',
'[1, 1]' : 'downright',
'[-1, 1]' : 'downleft',
'[1, -1]' : 'upright',
'[-1, -1]' : 'upleft'}
def animate(self, direction):
Actor.animate(self, self.directions[direction])
def loop(self):
self.rect.topleft = \
(self.crect.x-self.diff_x, self.crect.y-self.diff_y)
self.set_center(self.world.clamp(self.rect).center)