-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
171 lines (152 loc) · 6.29 KB
/
player.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
160
161
162
163
164
165
166
167
168
169
170
171
import random
import app
from physics import Component as PhysicsComponent
from animation import Animation, AnimationFactory
from config import Player as config
# Player controls
class Controls:
END = 0
JUMP = 1
LEFT = 2
RIGHT = 3
GROUND = 4
ATTACK = 5
class State(object):
def __init__(self, model):
self.model = model
def update(self): pass
def on_start(self): pass
def on_end(self): pass
class AnimatedState(State):
def __init__(self, model, animation):
super(AnimatedState, self).__init__(model)
self.animation = animation
def on_start(self):
self.model.animation = self.animation
self.animation.reset()
def on_finished(self):
self.model.control(Controls.END)
def update(self):
super(AnimatedState, self).update()
if self.animation.finished:
self.on_finished()
class MoveState(AnimatedState):
def __init__(self, model, animation, speed):
super(MoveState, self).__init__(model, animation)
self.speed = speed
def update(self):
super(MoveState, self).update()
self.model.physics.position.x += self.speed
class JumpState(AnimatedState):
def __init__(self, model, animation, force):
super(JumpState, self).__init__(model, animation)
self.force = force
def update(self):
super(JumpState, self).update()
if self.model.physics.ground:
self.on_ground()
def on_start(self):
super(JumpState, self).on_start()
if self.model.physics.ground:
self.model.physics.velocity.y -= self.force
def on_ground(self):
self.model.control(Controls.GROUND)
class JumpMoveState(JumpState):
def __init__(self, model, animation, speed, force):
super(JumpMoveState, self).__init__(model, animation, force)
self.speed = speed
def update(self):
super(JumpMoveState, self).update()
self.model.physics.position.x += self.speed
class Model(app.Drawable):
def __init__(self):
self.physics = PhysicsComponent(config['size'])
fact = AnimationFactory()
self.states = {
'idle_left': AnimatedState(self, fact.get('idle_left')),
'idle_right': AnimatedState(self, fact.get('idle_right')),
'walk_left': MoveState(self, fact.get('walk_left'), -config['speeds']['walk']),
'walk_right': MoveState(self, fact.get('walk_right'), config['speeds']['walk']),
'idle_jump_left': JumpState(self, fact.get('jump_left', loop=False), config['accelerations']['jump']),
'idle_jump_right': JumpState(self, fact.get('jump_right', loop=False), config['accelerations']['jump']),
'jump_left': JumpMoveState(self, fact.get('jump_left', loop=False), -config['speeds']['walk'], config['accelerations']['jump']),
'jump_right': JumpMoveState(self, fact.get('jump_right', loop=False), config['speeds']['walk'], config['accelerations']['jump']),
'attack_left': AnimatedState(self, fact.get('vomit_left', loop=False)),
'attack_right': AnimatedState(self, fact.get('vomit_right', loop=False)),
}
self.transitions = {
'idle_left': {
(Controls.LEFT, True): 'walk_left',
(Controls.RIGHT, True): 'walk_right',
(Controls.JUMP, True): 'idle_jump_left',
(Controls.ATTACK, True): 'attack_left'
},
'idle_right': {
(Controls.LEFT, True): 'walk_left',
(Controls.RIGHT, True): 'walk_right',
(Controls.JUMP, True): 'idle_jump_right',
(Controls.ATTACK, True): 'attack_right'
},
'walk_left': {
(Controls.LEFT, False): 'idle_left',
(Controls.RIGHT, True): 'walk_right',
(Controls.JUMP, True): 'jump_left',
(Controls.ATTACK, True): 'attack_left'
},
'walk_right': {
(Controls.LEFT, True): 'walk_left',
(Controls.RIGHT, False): 'idle_right',
(Controls.JUMP, True): 'jump_right',
(Controls.ATTACK, True): 'attack_right'
},
'idle_jump_left': {
(Controls.GROUND, True): 'idle_left',
(Controls.LEFT, True): 'jump_left',
(Controls.RIGHT, True): 'jump_right'
},
'idle_jump_right': {
(Controls.GROUND, True): 'idle_right',
(Controls.RIGHT, True): 'jump_right',
(Controls.LEFT, True): 'jump_left'
},
'jump_left': {
(Controls.GROUND, True): 'walk_left',
(Controls.LEFT, False): 'idle_jump_left'
},
'jump_right': {
(Controls.GROUND, True): 'walk_right',
(Controls.RIGHT, False): 'idle_jump_right'
},
'attack_left': {
(Controls.END, True): 'idle_left'
},
'attack_right': {
(Controls.END, True): 'idle_right'
}
}
self.animation = None
self.saved_state_key = self.state_key = 'idle_' \
+ random.choice(['left', 'right'])
self.state = self.states[self.state_key]
self.state.on_start()
def update(self):
self.physics.update()
if self.state:
self.state.update()
if self.state_key != self.saved_state_key:
self.saved_state_key = self.state_key
self.state.on_end()
self.state = self.states[self.state_key]
self.state.on_start()
self.animation.advance()
def control(self, control, pred=True):
try:
self.state_key = self.transitions[self.state_key][(control, pred)]
except KeyError:
pass
def render(self, graphics):
graphics.push_state()
x, y = self.physics.position
graphics.translate(x, y)
graphics.draw(self.animation)
graphics.pop_state()