/
Player.as
executable file
·163 lines (145 loc) · 4.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
package game
{
import org.flixel.FlxG;
import org.flixel.FlxPoint;
import org.flixel.FlxSprite;
import states.PlayState;
/**
* Ludum Dare 21 - Escape
* @author Paul S Burgess - 20/8/2011
*/
public class Player extends FlxSprite
{
[Embed(source = '../../data/textures/player.png')] private var imgPlayer:Class;
private const HORIZONTAL_RUN_SPEED:int = 60;
private const VERTICAL_RUN_SPEED:int = 40;
private const NUM_ANIM_FRAMES_PER_DIRECTION:int = 2;
// Pseudo-enum
private const E_DIRECTION_LEFT:int = 0;
private const E_DIRECTION_RIGHT:int = 1;
private const E_DIRECTION_UP:int = 2;
private const E_DIRECTION_DOWN:int = 3;
public var m_spriteWidth:int;
public var m_spriteHeight:int;
public var m_hasLoot:Boolean = false;
public function Player(xPos:int, yPos:int)
{
super(xPos, yPos);
loadGraphic(imgPlayer, true, false, 12, 16);
m_spriteWidth = width;
m_spriteHeight = height;
addAnimation("Idle_L", [E_DIRECTION_LEFT * NUM_ANIM_FRAMES_PER_DIRECTION + 0]);
addAnimation("Idle_R", [E_DIRECTION_RIGHT * NUM_ANIM_FRAMES_PER_DIRECTION + 0]);
addAnimation("Idle_U", [E_DIRECTION_UP * NUM_ANIM_FRAMES_PER_DIRECTION + 0]);
addAnimation("Idle_D", [E_DIRECTION_DOWN * NUM_ANIM_FRAMES_PER_DIRECTION + 0]);
addAnimation("Dead_L", [E_DIRECTION_LEFT * NUM_ANIM_FRAMES_PER_DIRECTION + 1]);
addAnimation("Dead_R", [E_DIRECTION_RIGHT * NUM_ANIM_FRAMES_PER_DIRECTION + 1]);
addAnimation("Dead_U", [E_DIRECTION_UP * NUM_ANIM_FRAMES_PER_DIRECTION + 1]);
addAnimation("Dead_D", [E_DIRECTION_DOWN * NUM_ANIM_FRAMES_PER_DIRECTION + 1]);
// Adjust collision rect
offset.x = 3;
offset.y = (height - height / 4);
width -= offset.x * 2;
height -= offset.y;
}
override public function update():void
{
var centreX:Number = getCentreStandingPos().x;
var centreY:Number = getCentreStandingPos().y;
if (alive)
{
if (FlxG.keys.pressed("RIGHT") && PlayState.m_currentLevel.IsInLevelBounds(centreX+1, centreY))
{
velocity.x = HORIZONTAL_RUN_SPEED;
}
else if (FlxG.keys.pressed("LEFT") && PlayState.m_currentLevel.IsInLevelBounds(centreX-1, centreY))
{
velocity.x = -HORIZONTAL_RUN_SPEED;
}
else
{
velocity.x = 0;
}
if (FlxG.keys.pressed("DOWN") && PlayState.m_currentLevel.IsInLevelBounds(centreX, centreY+1))
{
velocity.y = VERTICAL_RUN_SPEED;
}
else if (FlxG.keys.pressed("UP") && PlayState.m_currentLevel.IsInLevelBounds(centreX, centreY-1))
{
velocity.y = -VERTICAL_RUN_SPEED;
}
else
{
velocity.y = 0;
}
}
// Clamp position
var maxX:int = PlayState.m_currentLevel.m_roomCentreX + PlayState.m_currentLevel.getMaxXForY(centreY);
var minX:int = PlayState.m_currentLevel.m_roomCentreX - PlayState.m_currentLevel.getMaxXForY(centreY);
var maxY:int = PlayState.m_currentLevel.m_roomCentreY + PlayState.m_currentLevel.getMaxYForX(centreX);
var minY:int = PlayState.m_currentLevel.m_roomCentreY - PlayState.m_currentLevel.getMaxYForX(centreX);
if (centreX > maxX)
x = maxX - (width / 2.0);
else if (centreX < minX)
x = minX - (width / 2.0);
if (centreY > maxY)
y = maxY - (height - width / 4.0);
else if (centreY < minY)
y = minY - (height - width / 4.0);
if (alive)
{
// Animate
if (velocity.x > 0)
{
// Right
play("Idle_R");
facing = RIGHT;
}
else if (velocity.x < 0)
{
// Left
play("Idle_L");
facing = LEFT;
}
else if (velocity.y > 0)
{
// Down
play("Idle_D");
facing = DOWN;
}
else if (velocity.y < 0)
{
// Down
play("Idle_U");
facing = UP;
}
}
super.update();
}
override public function kill():void
{
alive = false;
switch (facing)
{
case LEFT:
play("Dead_L");
break;
case RIGHT:
play("Dead_R");
break;
case UP:
play("Dead_U");
break;
case DOWN:
default:
play("Dead_D");
break;
}
}
public function getCentreStandingPos():FlxPoint
{
var ret:FlxPoint = new FlxPoint(x + width / 2.0, y + height - width / 4.0);
return ret;
}
}
}