-
Notifications
You must be signed in to change notification settings - Fork 0
/
Preloader.hx
260 lines (226 loc) · 7.11 KB
/
Preloader.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
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
//SHOUTOUTS TO GAMEPOPPER FOR THE BALLIN TUTORIAL
//https://gamepopper.co.uk/2014/08/26/haxeflixel-making-a-custom-preloader/
package;
import openfl.Lib;
import openfl.display.Bitmap;
import openfl.display.BitmapData;
import openfl.display.Shape;
import openfl.display.Sprite;
import openfl.events.Event;
import openfl.events.MouseEvent;
import openfl.geom.Rectangle;
import openfl.text.TextField;
import openfl.text.TextFormat;
import openfl.text.TextFormatAlign;
import flixel.tweens.FlxEase;
@:bitmap("assets/preloader/cane.png" ) class Cane extends BitmapData { }
@:bitmap("assets/preloader/caneMask.png") class CaneMask extends BitmapData { }
@:bitmap("assets/preloader/stripes.png" ) class Stripes extends BitmapData { }
@:bitmap("assets/preloader/loading.png" ) class Loading extends BitmapData { }
@:bitmap("assets/preloader/start.png" ) class Start extends BitmapData { }
@:bitmap("assets/preloader/caneAnim.png") class CaneAnim extends BitmapData { }
@:bitmap("assets/preloader/xmasTank.png") class XmasTank extends BitmapData { }
@:bitmap("assets/preloader/bg.png" ) class BG extends BitmapData { }
class Preloader extends flixel.system.FlxBasePreloader
{
inline static var STRIPE_LOOP = 149;
inline static var CANE_THICKNESS = 60;
inline static var STRIPE_MAX = 326;
inline static var LOOP_TIME = 1.0;
inline static var TEXT_CHANGE_TIME = 1.0;
inline static var TEXT_WIPE_TIME = TEXT_CHANGE_TIME / 3;
override public function new(MinDisplayTime:Float = 1, ?AllowedURLs:Array<String>)
{
super(MinDisplayTime, AllowedURLs);
}
var cane:Bitmap;
var caneAnim:SpriteSheet;
var stripes:Bitmap;
var maskShape:Shape;
var outroStarted = false;
var outroStartTime:Float = 0;
var outroCompleted = false;
var loadingText:Bitmap;
var startText:Bitmap;
var clicked = false;
override private function create():Void
{
this._width = Lib.current.stage.stageWidth;
this._height = Lib.current.stage.stageHeight;
//bg
var BG = new Bitmap(new BG(960, 540));
BG.smoothing = false;
//bg.scaleX /= 2;
//bg.scaleY /= 2;
addChild(BG);
//tank image
var tank = new Bitmap(new XmasTank(318, 318));
tank.smoothing = false;
tank.scaleX /= 2;
tank.scaleY /= 2;
tank.x = (this._width - 318 * (tank.scaleX*2)) / 2;
tank.y = 25;
addChild(tank);
//cane loading base
cane = new Bitmap(new Cane(400, 150));
var caneMask = new Bitmap(new CaneMask(0, 0));
addChild(cane);
addChild(stripes = new Bitmap(new Stripes(0, 0)));
addChild(caneMask);
cane.smoothing = false;
cane.x = (this._width - 400) / 2;
cane.y = (this._height - 150) / 2 + 175;
caneMask.smoothing = false;
// caneMask.transform.colorTransform.color = Lib.current.stage.color;
caneMask.x = cane.x;
caneMask.y = cane.y + 150 - CANE_THICKNESS + 2;
stripes.smoothing = false;
stripes.x = caneMask.x;
stripes.y = caneMask.y;
maskShape = new Shape();
maskShape.graphics.beginFill(0xFFFFFF);
maskShape.graphics.drawRect(0, 0, STRIPE_MAX, CANE_THICKNESS);
maskShape.graphics.endFill();
maskShape.x = caneMask.x;
maskShape.y = caneMask.y;
stripes.mask = maskShape;
//text
loadingText = new Bitmap(new Loading(0, 0));
loadingText.smoothing = false;
loadingText.x = cane.x + 30;
loadingText.y = cane.y + 18;
loadingText.scrollRect = new Rectangle(0, 0, 194, 48);
addChild(loadingText);
startText = new Bitmap(new Start(0, 0));
startText.smoothing = false;
startText.x = loadingText.x;
startText.y = loadingText.y - 24;
startText.scrollRect = new Rectangle(0, 0, 0, 63);
addChild(startText);
//pull version from project.xml
var versionText = new TextField();
var format = new TextFormat("_sans", null, 0x999999);
versionText.defaultTextFormat = format;
versionText.selectable = false;
versionText.text = 'Version: ' + lime.app.Application.current.meta.get('version');
versionText.y = _height - versionText.textHeight - 2;
addChild(versionText);
stage.addEventListener(MouseEvent.CLICK, onClick);
super.create();
}
function onClick(e:MouseEvent)
{
if (outroCompleted)
{
stage.removeEventListener(MouseEvent.CLICK, onClick);
clicked = true;
}
}
override private function destroy():Void
{
stripes = null;
mask = null;
loadingText = null;
startText = null;
super.destroy();
}
override function onEnterFrame(e:Event)
{
var time = Date.now().getTime() - _startTime;
var min = minDisplayTime * 1000;
var textOutroComplete = false;
if (outroStarted)
{
var outroTime = (time - outroStartTime) / 1000;
//trace(outroTime);
if (outroTime < TEXT_WIPE_TIME)
{
final rect = loadingText.scrollRect;
final t = outroTime / TEXT_WIPE_TIME;
rect.width = FlxEase.circIn(1 - t) * loadingText.width;
loadingText.scrollRect = rect;
}
else if (outroTime > TEXT_CHANGE_TIME)
{
startText.scrollRect = null;
}
else if (outroTime > TEXT_CHANGE_TIME - TEXT_WIPE_TIME)
{
final rect = startText.scrollRect;
final t = (TEXT_CHANGE_TIME - TEXT_WIPE_TIME - outroTime) / TEXT_WIPE_TIME;
rect.width = FlxEase.circIn(t) * startText.width;
startText.scrollRect = rect;
}
if (outroTime > TEXT_WIPE_TIME)
{
loadingText.visible = false;
loadingText.scrollRect = null;
}
textOutroComplete = outroTime > TEXT_CHANGE_TIME;
}
if (caneAnim != null)
{
outroCompleted = caneAnim.finished && textOutroComplete;
caneAnim.update();
}
if (_loaded && (min <= 0 || time / min >= 1) && !outroCompleted)
{
_loaded = false;
outroStarted = true;
outroStartTime = time;
}
else if (outroCompleted && clicked)
_loaded = true;
super.onEnterFrame(e);
if (stripes != null)
{
var oldX = stripes.x;
stripes.x = cane.x
+ Math.round(-STRIPE_LOOP * (time / 1000.0 / LOOP_TIME)) % STRIPE_LOOP;
stripes.x = Math.floor(stripes.x / 4) * 4;
if (oldX < stripes.x && outroStarted)
{
stripes.x = cane.x;// - STRIPE_LOOP;
addChild(caneAnim = new SpriteSheet(new CaneAnim(0, 0), 168, 150, [0,0,0,0,1,2,3,4,5,6,7,7,7,7,7,7]));
caneAnim.x = cane.x + cane.width - caneAnim.width;
caneAnim.y = cane.y;
stripes = null;
}
}
}
override public function update(percent:Float):Void
{
super.update(percent);
maskShape.width = STRIPE_MAX * percent;
if (caneAnim != null)
caneAnim.update();
}
}
class SpriteSheet extends Sprite
{
public var finished(get, never):Bool;
inline function get_finished() return time > frames.length * frameTime;
var frames:Array<Int>;
var frameTime = 0.0;
var time = 0.0;
override function get_width():Float return scrollRect.width;
override function get_height():Float return scrollRect.height;
public function new(bitmapData:BitmapData, width:Int, height:Int, frames:Array<Int>, frameRate = 15)
{
this.frameTime = 1 / frameRate;
this.frames = frames;
super();
addChild(new Bitmap(bitmapData));
scrollRect = new Rectangle(0, 0, width, height);
}
inline public function update():Void
{
time += 1 / Lib.current.stage.frameRate;
var frame = Math.floor(time / frameTime);
if (frame >= frames.length)
frame = frames.length -1;
var rect = scrollRect;
rect.x = rect.width * frames[frame];
scrollRect = rect;
}
}