-
Notifications
You must be signed in to change notification settings - Fork 289
/
Copy pathPlayState.hx
231 lines (182 loc) · 5.34 KB
/
PlayState.hx
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
package;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.effects.particles.FlxEmitter;
import flixel.text.FlxText;
import flixel.util.FlxSave;
class PlayState extends FlxState
{
var _player:Player;
var _bounceLeft:FlxSprite;
var _bounceRight:FlxSprite;
var _paddleLeft:Paddle;
var _paddleRight:Paddle;
var _spikeBottom:FlxSprite;
var _spikeTop:FlxSprite;
var _scoreDisplay:FlxText;
var _feathers:FlxEmitter;
var _highScore:FlxText;
override public function create():Void
{
super.create();
// Keep a reference to this state in Reg for global access.
Reg.PS = this;
// Set background color identical to the bottom of the "city", so on tall screens there's not a big black bar at the bottom.
FlxG.camera.bgColor = 0xff646A7D;
// Hide the mouse.
#if FLX_MOUSE
FlxG.mouse.visible = false;
#end
// The background city.
add(new FlxSprite(0, 0, "assets/bg.png"));
// Current score.
_scoreDisplay = new FlxText(0, 180, FlxG.width);
_scoreDisplay.alignment = CENTER;
_scoreDisplay.color = 0xff868696;
add(_scoreDisplay);
#if mobile
_scoreDisplay.text = "Tap to start";
#else
_scoreDisplay.text = "Press Space to start";
#end
// Update all-time high score.
Reg.highScore = loadScore();
// Display high score.
_highScore = new FlxText(0, 40, FlxG.width, "");
_highScore.alignment = CENTER;
_highScore.color = 0xff868696;
add(_highScore);
if (Reg.highScore > 0)
_highScore.text = Std.string(Reg.highScore);
// The left bounce panel. Drawn via code in Reg to fit screen height.
_bounceLeft = new FlxSprite(1, 17);
_bounceLeft.loadGraphic(Reg.getBounceImage(FlxG.height - 34), true, 4, FlxG.height - 34);
_bounceLeft.animation.add("flash", [1, 0], 8, false);
add(_bounceLeft);
// The right bounce panel.
_bounceRight = new FlxSprite(FlxG.width - 5, 17);
_bounceRight.loadGraphic(Reg.getBounceImage(FlxG.height - 34), true, 4, FlxG.height - 34);
_bounceRight.animation.add("flash", [1, 0], 8, false);
add(_bounceRight);
// The left spiky paddle
_paddleLeft = new Paddle(6, RIGHT);
add(_paddleLeft);
// The right spiky paddle
_paddleRight = new Paddle(FlxG.width - 15, LEFT);
add(_paddleRight);
// Spikes at the bottom of the screen
_spikeBottom = new FlxSprite(0, 0, "assets/spike.png");
_spikeBottom.y = FlxG.height - _spikeBottom.height;
add(_spikeBottom);
// Spikes at the top of the screen. Rotated to reduce number of assets.
_spikeTop = new FlxSprite(0, 0);
_spikeTop.loadRotatedGraphic("assets/spike.png", 4);
_spikeTop.angle = 180;
_spikeTop.y = -72;
add(_spikeTop);
// The bird.
_player = new Player();
add(_player);
// A simple emitter to make some feathers when the bird gets spiked.
_feathers = new FlxEmitter();
_feathers.loadParticles("assets/feather.png", 50, 32);
_feathers.velocity.set(-10, -10, 10, 10);
_feathers.acceleration.set(0, 10);
add(_feathers);
}
override public function update(elapsed:Float):Void
{
if (FlxG.pixelPerfectOverlap(_player, _spikeBottom)
|| FlxG.pixelPerfectOverlap(_player, _spikeTop)
|| FlxG.pixelPerfectOverlap(_player, _paddleLeft)
|| FlxG.pixelPerfectOverlap(_player, _paddleRight))
{
_player.kill();
}
else if (_player.x < 5)
{
_player.x = 5;
_player.velocity.x = -_player.velocity.x;
_player.flipX = false;
increaseScore();
_bounceLeft.animation.play("flash");
_paddleRight.randomize();
}
else if (_player.x + _player.width > FlxG.width - 5)
{
_player.x = FlxG.width - _player.width - 5;
_player.velocity.x = -_player.velocity.x;
_player.flipX = true;
increaseScore();
_bounceRight.animation.play("flash");
_paddleLeft.randomize();
}
#if FLX_KEYBOARD
if (FlxG.keys.justPressed.E && (FlxG.keys.pressed.CONTROL || FlxG.keys.pressed.SHIFT || FlxG.keys.pressed.ALT))
{
clearSave();
FlxG.resetState();
}
#end
super.update(elapsed);
}
public function launchFeathers(X:Float, Y:Float, Amount:Int):Void
{
_feathers.x = X;
_feathers.y = Y;
_feathers.start(true, 0, Amount);
}
public function randomPaddleY():Int
{
return FlxG.random.int(Std.int(_bounceLeft.y), Std.int(_bounceLeft.y + _bounceLeft.height - _paddleLeft.height));
}
function increaseScore():Void
{
Reg.score++;
_scoreDisplay.text = Std.string(Reg.score);
_scoreDisplay.size = 24;
}
/**
* Resets the state to its initial position without having to call FlxG.resetState().
*/
public function reset()
{
_paddleLeft.y = FlxG.height;
_paddleRight.y = FlxG.height;
_player.flipX = false;
Reg.score = 0;
_scoreDisplay.text = "";
Reg.highScore = loadScore();
if (Reg.highScore > 0)
_highScore.text = Std.string(Reg.highScore);
}
/**
* Safely store a new high score into the saved session, if possible.
*/
static public function saveScore():Void
{
if ((FlxG.save.data.score == null) || (FlxG.save.data.score < Reg.score))
FlxG.save.data.score = Reg.score;
// Have to do this in order for saves to work on native targets!
FlxG.save.flush();
}
/**
* Load data from the saved session.
*
* @return The total points of the saved high score.
*/
static public function loadScore():Int
{
if ((FlxG.save.data != null) && (FlxG.save.data.score != null))
return FlxG.save.data.score;
return 0;
}
/**
* Wipe save data.
*/
static public function clearSave():Void
{
FlxG.save.erase();
}
}