-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.as
314 lines (258 loc) · 10.3 KB
/
Player.as
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package
{
import flash.display.MovieClip;
import flash.geom.Point;
public class Player extends MovieClip
{
public var directionFacing:String = "RIGHT";
public var animationState:String;
public var lastAnimationState:String;
public var attackFinished:Boolean = true;
public var animationBeforeAttack;
public var currentWeapon;
public var xVelocity:Number = 0;
public var yVelocity:Number = 0;
private var maxVelocity:Number = 16;
private var xConst:Number = 0;
private var yConst:Number = 0;
private var gravity:Number = 0.2;
public var grounded:Boolean = true;
private var parentState;
//Has a movement key been pressed/released?
private var isMovingUp:Boolean = false;
private var isMovingDown:Boolean = false;
private var isMovingLeft:Boolean = false;
private var isMovingRight:Boolean = false;
public function Player(_x, _y, state)
{
x = _x;
y = _y;
parentState = state;
currentWeapon = new Weapon("KNIFE", true, this, parentState);
addChild(currentWeapon);
scaleX = 0.8;
scaleY = 0.8;
}
public function update()
{
updateMovement();
updateCollisions()
currentWeapon.update();
//Apply the appropriate animation to the player
if (attackFinished) stateCheck();
}
private function stateCheck()
{
//Keep a track of what the last animation state was to stop the same animation constantly restarting
lastAnimationState = animationState;
if (yVelocity > 0 && directionFacing == "LEFT" && parentState.hitler.isCarried == false) animationState = "L_FALL";
else if (yVelocity > 0 && directionFacing == "RIGHT" && parentState.hitler.isCarried == false) animationState = "R_FALL";
if (yVelocity > 0 && directionFacing == "LEFT" && parentState.hitler.isCarried == true) animationState = "L_FALL_CARRY";
else if (yVelocity > 0 && directionFacing == "RIGHT" && parentState.hitler.isCarried == true) animationState = "R_FALL_CARRY";
if (isMovingLeft && grounded && parentState.hitler.isCarried == false) animationState = "L_WALKING";
//else if (directionFacing == "LEFT" && grounded && parentState.hitler.isCarried == false) animationState = "L_IDLE";
if (isMovingRight && grounded && parentState.hitler.isCarried == false) animationState = "R_WALKING";
//else if (directionFacing == "RIGHT" && grounded && parentState.hitler.isCarried == false) animationState = "R_IDLE";
if (isMovingLeft && grounded && parentState.hitler.isCarried == true) animationState = "L_WALKING_CARRY";
else if (directionFacing == "LEFT" && grounded && parentState.hitler.isCarried == true) animationState = "L_IDLE_CARRY";
if (isMovingRight && grounded && parentState.hitler.isCarried == true) animationState = "R_WALKING_CARRY";
else if (directionFacing == "RIGHT" && grounded && parentState.hitler.isCarried == true) animationState = "R_IDLE_CARRY";
if ((isMovingUp || yVelocity < 0) && directionFacing == "LEFT" && parentState.hitler.isCarried == false) animationState = "L_JUMP";
else if ((isMovingUp || yVelocity < 0) && directionFacing == "RIGHT" && parentState.hitler.isCarried == false) animationState = "R_JUMP";
if ((isMovingUp || yVelocity < 0) && directionFacing == "LEFT" && parentState.hitler.isCarried == true) animationState = "L_JUMP_CARRY";
else if ((isMovingUp || yVelocity < 0) && directionFacing == "RIGHT" && parentState.hitler.isCarried == true) animationState = "R_JUMP_CARRY";
//Weapon specific states
if (currentWeapon.type == "KNIFE")
{
if (isMovingLeft && grounded && parentState.hitler.isCarried == false) animationState = "L_WALKING_KNIFE";
else if (directionFacing == "LEFT" && grounded && parentState.hitler.isCarried == false) animationState = "L_IDLE_KNIFE";
if (isMovingRight && grounded && parentState.hitler.isCarried == false) animationState = "R_WALKING_KNIFE";
else if (directionFacing == "RIGHT" && grounded && parentState.hitler.isCarried == false) animationState = "R_IDLE_KNIFE";
}
else if (currentWeapon.type == "GUN")
{
if (isMovingLeft && grounded && parentState.hitler.isCarried == false) animationState = "L_WALKING_RIFLE";
else if (directionFacing == "LEFT" && grounded && parentState.hitler.isCarried == false) animationState = "L_IDLE_RIFLE";
if (isMovingRight && grounded && parentState.hitler.isCarried == false) animationState = "R_WALKING_RIFLE";
else if (directionFacing == "RIGHT" && grounded && parentState.hitler.isCarried == false) animationState = "R_IDLE_RIFLE";
}
else if (currentWeapon.type == "GRENADE")
{
if (isMovingLeft && grounded && parentState.hitler.isCarried == false) animationState = "L_WALKING_GRENADE";
else if (directionFacing == "LEFT" && grounded && parentState.hitler.isCarried == false) animationState = "L_IDLE_GRENADE";
if (isMovingRight && grounded && parentState.hitler.isCarried == false) animationState = "R_WALKING_GRENADE";
else if (directionFacing == "RIGHT" && grounded && parentState.hitler.isCarried == false) animationState = "R_IDLE_GRENADE";
}
//Only set the new animation if there was a change in the state
if (lastAnimationState != animationState)
{
gotoAndPlay(animationState);
}
}
//Attack animations interrupt any other animations and will not allow a state change until the animation has finished
private function startAttackAnimation()
{
animationBeforeAttack = animationState;
if (currentWeapon.type == "KNIFE")
{
if (isMovingRight && parentState.hitler.isCarried == false) gotoAndPlay("R_ATTACK_KNIFE");
if (isMovingLeft && parentState.hitler.isCarried == false) gotoAndPlay("L_ATTACK_KNIFE");
if (!isMovingRight && directionFacing == "RIGHT" && parentState.hitler.isCarried == false) gotoAndPlay("R_ATTACK_KNIFE");
if (!isMovingLeft && directionFacing == "LEFT" && parentState.hitler.isCarried == false) gotoAndPlay("L_ATTACK_KNIFE");
}
else if (currentWeapon.type == "GUN")
{
if (isMovingRight && parentState.hitler.isCarried == false) gotoAndPlay("R_ATTACK_RIFLE");
if (isMovingLeft && parentState.hitler.isCarried == false) gotoAndPlay("L_ATTACK_RIFLE");
if (!isMovingRight && directionFacing == "RIGHT" && parentState.hitler.isCarried == false) gotoAndPlay("R_IDLE_ATTACK_RIFLE");
if (!isMovingLeft && directionFacing == "LEFT" && parentState.hitler.isCarried == false) gotoAndPlay("L_IDLE_ATTACK_RIFLE");
}
else if (currentWeapon.type == "GRENADE")
{
if (isMovingRight && parentState.hitler.isCarried == false) gotoAndPlay("R_ATTACK_GRENADE");
if (isMovingLeft && parentState.hitler.isCarried == false) gotoAndPlay("L_ATTACK_GRENADE");
if (!isMovingRight && directionFacing == "RIGHT" && parentState.hitler.isCarried == false) gotoAndPlay("R_ATTACK_GRENADE");
if (!isMovingLeft && directionFacing == "LEFT" && parentState.hitler.isCarried == false) gotoAndPlay("L_ATTACK_GRENADE");
}
}
public function attack()
{
if (parentState.hitler.isCarried) return;
currentWeapon.fire();
//Override the current animation checking with an immediate attack animation and record what the animation was before the attack started
//So it can be returned to after the attack finishes
attackFinished = false;
if (currentWeapon.ammo < 0)
{
attackFinished = true;
stateCheck();
return;
}
if (animationState != animationBeforeAttack && (currentWeapon.currentTime > currentWeapon.cooldownOverTime))
{
animationBeforeAttack = animationState;
}
startAttackAnimation();
}
//When the current weapon runs out of ammo, give the player a knife
public function weaponDepleted()
{
removeChild(currentWeapon);
currentWeapon = new Weapon("KNIFE", true, this, parentState);
addChild(currentWeapon);
}
//Picking up weapons off the ground
public function pickupWeapon()
{
for (var i:int = 0; i < parentState.droppedWeapons.length; i++)
{
if (parentState.droppedWeapons[i].alive)
{
if (this.hitTestObject(parentState.droppedWeapons[i]))
{
parentState.droppedWeapons[i].alive = false;
removeChild(currentWeapon);
currentWeapon = new Weapon(parentState.droppedWeapons[i].type, true, this, parentState);
addChild(currentWeapon);
return;
}
}
}
}
//Basically the same as the mario code
private function updateMovement()
{
var tempVelocity:Number = 0;
if(Math.abs(xVelocity) > maxVelocity)
{
tempVelocity = xVelocity;
xVelocity = maxVelocity;
if(tempVelocity < 0)
{
xVelocity = -xVelocity;
}
}
if(Math.abs(yVelocity) > maxVelocity)
{
tempVelocity = yVelocity;
yVelocity = maxVelocity;
if(tempVelocity < 0)
{
yVelocity = -yVelocity;
}
}
x += xVelocity + xConst;
y += yVelocity + yConst;
applyForce(0, gravity);
//If the player starts to fall, set grounded to false
if (yVelocity > 3) grounded = false;
}
private function updateCollisions()
{
y -= 50;
for (var i = 0; i < parentState.barrierArray.length; i++)
{
parentState.barrierArray[i].resolveCollisions(this);
}
y += 50;
}
public function resetYVelocity()
{
yVelocity = 0;
}
public function applyForce(aX:Number, aY:Number)
{
xVelocity += aX;
yVelocity += aY;
}
public function setConstantForce(aX:Number, aY:Number)
{
xConst = aX;
yConst = aY;
}
public function applyConstantForce(aX:Number, aY:Number)
{
xConst += aX;
yConst += aY;
}
public function startMovingUp()
{
if (!isMovingUp && grounded)
{
Audio.play("jump", 3);
grounded = false;
isMovingUp = true;
applyForce(0, -8.5);
}
}
public function startMovingDown() {isMovingDown = true;}
public function startMovingLeft()
{
if (!isMovingLeft)
{
directionFacing = "LEFT";
isMovingLeft = true;
applyConstantForce(-6,0);
}
}
public function startMovingRight()
{
if (!isMovingRight)
{
directionFacing = "RIGHT";
isMovingRight = true;
applyConstantForce(6,0);
}
}
public function stopMovingUp() {isMovingUp = false;}
public function stopMovingDown() {isMovingDown = false;}
public function stopMovingLeft()
{
isMovingLeft = false; xConst = 0;
}
public function stopMovingRight()
{
isMovingRight = false; xConst = 0;
}
}
}