-
Notifications
You must be signed in to change notification settings - Fork 58
/
Player.as
executable file
·267 lines (232 loc) · 9.75 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
package
{
import flash.geom.Point;
import org.flixel.FlxSprite;
import org.flixel.FlxG;
import org.flixel.FlxCamera;
import org.flixel.FlxGroup;
import org.flixel.FlxObject;
import org.flixel.FlxPoint;
import org.flixel.FlxSound;
public class Player extends FlxSprite{
[Embed(source='/assets/gfx/king.png')] private var PlayerImg:Class;
[Embed(source="/assets/sound/pickup.mp3")] public static const PickupSound:Class;
[Embed(source="/assets/sound/build.mp3")] private var BuildSound:Class;
[Embed(source="/assets/sound/throw.mp3")] private var ThrowSound:Class;
[Embed(source="/assets/sound/stolen.mp3")] private var StolenSound:Class;
public static var pickupSound:FlxSound = FlxG.loadSound(PickupSound);
public static const BASE_SKIN:uint = 0xFFedbebf;
public static const BASE_DARK:uint = 0xFFbd9898;
public static const BASE_EYES:uint = 0xFFa18383;
public static const MAX_SPEED:Number = 80;
public static const MIN_SPEED:Number = 25;
public static const MAX_FOOD_BONUS:Number = 50;
public static const MAX_FOOD:Number = 100;
public static const HIT_RATE:Number = 0.2;
public static const SELECT_DISTANCE:Number = 10;
private var playstate:PlayState;
private var selectedBuilding:FlxSprite = null;
private var floatCoin:CoinFloat = null;
private var lastMoved:Number = 0;
public var food:Number = 100;
public var hasCrown:Boolean = true;
public var lastTrollHit:Number = 0;
public var coins:int = 7;
public function Player(X:int,Y:int){
super(X,Y);
y = 100 // 132 (land height) - 32 (player height)
loadGraphic(PlayerImg,true,true,64,64);
width = 20;
height = 32;
offset.x = 22;
offset.y = 32;
maxVelocity.x = MAX_SPEED;
drag.x = maxVelocity.x*5;
playstate = (FlxG.state as PlayState);
addAnimation('walk_slow',[0,1,2,3,4,5,6,7],10,true);
addAnimation('walk_fast',[0,1,2,3,4,5,6,7],15,true);
addAnimation('stand',[8],10,true);
addAnimation('eat',[9,10],5,true);
addAnimation('nocrown',[11],10,true);
play('stand');
playstate.player = this;
var d:Number = Math.random() * 20;
var skin:uint = Utils.HSVtoRGB(d, 0.19 + (d / 100), 0.97 - (d / 33));
Utils.replaceColor(pixels, BASE_SKIN, skin);
Utils.replaceColor(pixels, BASE_DARK, Utils.interpolateColor(skin,0xFF000000,0.2));
Utils.replaceColor(pixels, BASE_EYES, Utils.interpolateColor(skin,0xFF000000,0.5));
}
public function changeCoins(amt:int):void{
if (amt > 0) {
pickupSound.play(false);
pickupSound.proximity(x, y, this, FlxG.width);
}
coins += amt;
playstate.showCoins();
}
public function hitByTroll(troll:Troll):void{
if (troll.hasCoin) return;
if (lastTrollHit < HIT_RATE) return;
lastTrollHit = 0;
// If the player has coins, lose one and return.
if (coins > 0){
var c:Coin = (playstate.coins.recycle(Coin) as Coin);
c.drop(this, troll, true);
c.justThrown = true;
FlxG.play(StolenSound).proximity(x, y, this, FlxG.width);
changeCoins(-1);
FlxG.shake();
return;
}
if (hasCrown){
FlxG.flash(0xFFFFFFFF, 0.1);
troll.stealCrown();
lostCrown(troll);
playstate.crownStolen();
}
}
public function lostCrown(troll:FlxObject):void{
hasCrown = false;
facing = troll.x > x ? RIGHT : LEFT;
play('nocrown');
FlxG.play(StolenSound).proximity(x, y, this, FlxG.width);
Utils.explode(this, playstate.gibs);
}
public function pickup(coin:FlxObject):void{
if (!coin.alive) return;
var c:Coin = coin as Coin;
// Return if the coin doesn't belong to me.
if (c.justThrown){
return;
}
c.kill();
changeCoins(1);
}
override public function update():void {
lastTrollHit += FlxG.elapsed;
// Check for movement input
acceleration.x = 0;
if (!hasCrown){
return;
}
if(FlxG.keys.LEFT || FlxG.keys.RIGHT){
lastMoved = 0;
if (food > 0){
food -= FlxG.elapsed;
}
maxVelocity.x = MIN_SPEED + Math.min(1,food/MAX_FOOD_BONUS) * (MAX_SPEED-MIN_SPEED);
if (!playstate.horseAdvice && food < 10){
playstate.horseAdvice = true;
playstate.showText("Horse is tired. Let him rest on the grass.")
}
}
if(FlxG.keys.LEFT){
acceleration.x = -maxVelocity.x*4;
facing = LEFT;
if (maxVelocity.x > MIN_SPEED + 15){
play('walk_fast');
} else {
play('walk_slow');
}
} else if(FlxG.keys.RIGHT){
acceleration.x = maxVelocity.x*4;
facing = RIGHT;
if (maxVelocity.x > MIN_SPEED + 15){
play('walk_fast');
} else {
play('walk_slow');
}
} else {
lastMoved += FlxG.elapsed;
if (lastMoved > 1 && food < MAX_FOOD){
// Check if on grass
var headPos:Number = (x+width/2) + (facing == RIGHT ? 25: -25)
var onTile:int = playstate.floor.getTile(headPos/32,4)
if ((onTile >= 7 && onTile <= 11) || (onTile >= 17 && onTile <= 18)){
play('eat',false);
food += FlxG.elapsed*10;
} else {
play('stand');
}
} else {
play('stand');
}
}
if (FlxG.keys.SHIFT && PlayState.CHEATS) {
velocity.x *= 10
}
if (FlxG.keys.justPressed("DOWN")){
if (coins <= 0){
playstate.showCoins();
} else {
if (selectedBuilding != null){
changeCoins(-1);
giveCoin(selectedBuilding);
} else {
var cit:Citizen;
var closestCitizen:Citizen = null;
var closest:Number = 1000000;
for (var i:int = 0; i < playstate.beggars.length; i++){
cit = (playstate.beggars.members[i] as Citizen)
if (Math.abs((cit.x + cit.width/2) - (x + width/2)) < closest){
closestCitizen = cit;
closest = Math.abs((cit.x + cit.width/2) - (x + width/2));
}
}
if (playstate.recruitedCitizen || closest < 64){
var c:Coin = (playstate.coins.recycle(Coin) as Coin);
c.drop(this, closestCitizen);
c.justThrown = true;
FlxG.play(ThrowSound).proximity(x, y, this, FlxG.width);
changeCoins(-1);
}
}
}
}
super.update();
//Find selected shop/wall
if (selectedBuilding != null){
if (Math.abs((selectedBuilding.x + selectedBuilding.width/2) - (x + width/2)) > SELECT_DISTANCE * 2){
deselect(selectedBuilding);
}
} else if (playstate.recruitedCitizen) {
checkSelectable(playstate.objects)
checkSelectable(playstate.shops);
checkSelectable(playstate.walls);
}
// CAP WALKING AT LEVEL ENDS
if (x < 0){
velocity.x = Math.max(velocity.x, 0);
} else if (x + width > PlayState.GAME_WIDTH) {
velocity.x = Math.min(velocity.x, 0);
}
}
private function checkSelectable(group:FlxGroup):void{
for (var i:int = 0; i < group.length; i ++){
var b:FlxSprite = group.members[i];
if (b != null && Math.abs((b.x + b.width/2) - (x + width/2)) <= SELECT_DISTANCE){
if ((b as Buildable).canBuild())
select(b);
}
}
}
private function select(building:FlxSprite):void{
selectedBuilding = building;
if (floatCoin == null){
playstate.add(floatCoin = new CoinFloat());
}
floatCoin.visible = true;
floatCoin.float(selectedBuilding);
}
private function deselect(building:FlxSprite):void{
selectedBuilding.color = 0xFFFFFFFF;
selectedBuilding = null;
floatCoin.visible = false;
}
private function giveCoin(building:FlxSprite):void{
Buildable(building).build();
FlxG.play(BuildSound).proximity(x, y, this, FlxG.width);
deselect(building);
}
}
}