-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathWall.as
executable file
·139 lines (123 loc) · 4.54 KB
/
Wall.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
package
{
import flash.geom.Point;
import org.flixel.FlxSprite;
import org.flixel.FlxG;
import org.flixel.FlxObject;
import org.flixel.FlxPoint;
public class Wall extends FlxSprite implements Workable, Buildable{
[Embed(source='/assets/gfx/wall.png')] private var WallImg:Class;
[Embed(source="/assets/sound/hitwall.mp3")] private var HitwallSound:Class;
public const HEIGHT:Array = [11,38,46,54,59]; // Effective Height: [26, 34, 42, 47]
public const HEALTH:Array = [2,38,50,60,75];
public const HURT_COOLDOWN:Number = 1;
public const WORK_BUILD_HEIGHT:int = 10;
public const WORK_HEAL_AMOUNT:int = 4;
private var playstate:PlayState;
public var scaffold:Scaffold = null;
public var building:Boolean = false;
public var heightToBuild:int = 0;
public var baseY:Number;
public var stage:int = 0;
private var t:Number = 0;
public function Wall(X:Number, Y:Number){
baseY = Y;
super(X,Y);
immovable = true;
moves = false;
solid = false;
loadGraphic(WallImg, true, true, 32, 64);
addAnimation("grow",[0,1,2,3,4,5,6,7,8,9],1);
if (X > 1920){
facing = LEFT;
}
offset.x = 4;
width = 24;
health = HEALTH[stage];
updateAppearance();
playstate = FlxG.state as PlayState;
}
public function build():void{
buildTo(stage + 1);
}
public function buildTo(s:int, instant:Boolean=false):void{
if (!instant && s < stage) return;
building = true;
stage = s;
heightToBuild = HEIGHT[stage];
health = HEALTH[stage];
updateAppearance();
if (scaffold != null){
scaffold.kill();
}
scaffold = (playstate.indicators.recycle(Scaffold) as Scaffold).build(this);
scaffold.y = y - HEIGHT[stage];
solid = false;
// Kind of hacky this.
if (instant){
heightToBuild = 1;
work(null);
}
// flicker();
}
public function work(citizen:Citizen=null):void{
if (heightToBuild > 0) {
heightToBuild -= WORK_BUILD_HEIGHT;
if (heightToBuild <= 0){
heightToBuild = 0;
building = false;
solid = true;
Utils.explode(scaffold, playstate.gibs, 1);
scaffold.kill();
scaffold = null;
}
} else {
if (health < HEALTH[stage]){
health += Math.min(WORK_HEAL_AMOUNT, HEALTH[stage] - health);
}
}
updateAppearance();
}
override public function hurt(Damage:Number):void{
if (t > HURT_COOLDOWN){
health -= Damage;
FlxG.play(HitwallSound).proximity(x, y, playstate.player, FlxG.width)
Utils.explode(this, playstate.gibs, 0.1);
t = 0;
}
if (health <= 0 && stage > 0){
Utils.explode(this, playstate.gibs, 1.0);
stage = 0;
health = HEALTH[stage];
}
health = Math.max(health, HEALTH[0]);
updateAppearance();
}
public function needsWork():Boolean{
return ((building || health < HEALTH[stage]) && t < HURT_COOLDOWN);
}
public function canBuild():Boolean{
return (!building && stage < 4)
}
private function updateAppearance():void{
height = HEIGHT[stage] - heightToBuild;
y = baseY + 64 - height;
offset.y = 64 - HEIGHT[stage];
if (health < HEALTH[stage] / 2){
frame = stage + 5;
} else {
frame = stage;
}
}
override public function update():void{
t += FlxG.elapsed;
if (this.stage > 0 && !building){
if (this.x > PlayState.GAME_WIDTH/2){
playstate.kingdomRight = Math.max(playstate.kingdomRight, x)
} else {
playstate.kingdomLeft = Math.min(playstate.kingdomLeft, x+width)
}
}
}
}
}