-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
390 lines (318 loc) · 12 KB
/
main.ts
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import {Sound} from "./Sound";
import {PauseTimer} from "./PauseTimer";
import {LM} from "./lm";
import {Vec2} from "./Math";
import {ResourceManager} from "./ResourceManager";
import {LevelNames} from "./ObjectsList";
import {CAMERA_DEBUG, DEBUG_EFFECTS, FRAME_DELAY, MIN_SCR_HEIGHT, MIN_SCR_WIDTH, SKIP_MENU} from "./ClientSettings";
import {SM} from "./Stages/SM";
import {Menu} from "./Stages/Menu";
import {Game} from "./Stages/Game";
//asdasdasd
declare let window: any;
declare let Stats: any;
export type PIXIContainer = any;
export type PIXIRectangle = any;
declare let Playtomic: any;
export let $: any = window.$;
const GLOBAL_MUSIC_ASSETS = [];
const GLOBAL_SOUND_ASSETS = [
//////////////////////////////////////////
// Music
//////////////////////////////////////////
];//
const GLOBAL_ASSETS = [
///////////////////////////////////////////
// Spine
///////////////////////////////////////////
///////////////////////////////////////////
// Atlases
///////////////////////////////////////////
'atlas/tiles1.json',
///////////////////////////////////////////
// Fonts
///////////////////////////////////////////
'fonts/main-export.xml',
'fonts/font2-export.xml',
///////////////////////////////////////////
// Shaders
///////////////////////////////////////////
];
export let Bounce = window.Bounce;
export let Linear = window.Linear;
export let Quad = window.Quad;
export let Power3 = window.Power3;
export let Power2 = window.Power2;
export let Sine = window.Sine;
export let Elastic = window.Elastic;
export let FMath = window.FMath;
export let SCR_WIDTH = 720;//SCR_WIDTH;
export let SCR_HEIGHT = 1280;//SCR_HEIGHT;
export let SCR_WIDTH_HALF; //recalc later
export let SCR_HEIGHT_HALF; //recalc later
export let TweenMax = window.TweenMax;
export class Main {
engine: any;
public activeTab: boolean = true;
public timer: PauseTimer = new PauseTimer();
private lastLoop: number = 0;
private totalFrames: number = 0;
private totalDelta: number = 0;
public time: number;
//analytics
private country: string;
private avgFPS: number;
private avgPing: number;
private loadTime: number;
public fMath = new FMath(null);
private preloadBar: PIXI.Graphics;
menu: Menu = new Menu();
game: Game = new Game();
public onContext: Function;
public __DIR: string;
public TweenMax: any = window.TweenMax;
public TimelineMax: any = window.TimelineMax;
public PIXI: any;
public rm: ResourceManager;
public sm: SM;
public lm: LM;
public renderer: any;
public camera: any;
public app: PIXI.Application;
public worldSpeed: number = 1;
public debug: boolean = true;
public sound: Sound;
public cursor: PIXI.Sprite;
isInitialLoading: boolean = true;
assets: Array<string>;
assetsLoaded: number = 0;
delta: number = 0;
public random: number;
public stats: any;
private loadingCounter: number = 0;
screenCenterOffset: Vec2;
public cursorPos: PIXI.Point;
public globalMouseDown: Function;
private bindedAnimate: FrameRequestCallback;
public appScale = 1;
public lastNetworkPing: number;
public timeScale: number = 1;
private statsPIXIHook: any;
lostFocus: number;
public deltaSec: number = 0.01;
constructor() {
this.__DIR = location.pathname.substring(0, location.pathname.lastIndexOf('/') + 1);
}
cm(s: string, layer: PIXIContainer = null, autoplay: boolean = false, times: number[] = null): PIXI.extras.AnimatedSprite { //create sprite from frame and add to default layer
let textures = [];
let keys: Array<string> = [];
for (let key in PIXI.utils.TextureCache) {
if (key.indexOf(s) == 0) {
keys.push(key);
}
}
let inx = 0;
for (let key of keys) {
if (times) {
textures.push({texture: PIXI.utils.TextureCache[key], time: times[inx] ? times[inx] : 1});
} else {
textures.push(PIXI.utils.TextureCache[key]);
}
inx++;
}
let gfx = new PIXI.extras.AnimatedSprite(textures);
gfx.anchor.x = 0.5;
gfx.anchor.y = 0.5;
if (layer)
layer.addChild(gfx);
if (autoplay) {
gfx.gotoAndPlay(0)
}
return gfx;
}
csproj(s: string, layer: PIXIContainer = null): any {
let texture = PIXI.Texture.fromFrame(s);
let gfx = new PIXI.Sprite(texture);
gfx.anchor.x = .5;
gfx.anchor.y = .5;
if (layer)
layer.addChild(gfx); else {
}
return gfx
}
cs(s: string, layer: PIXIContainer = null): PIXI.Sprite { //create sprite from frame and add to default layer
if (!PIXI.utils.TextureCache[s]) {
console.log("@@@@Can't find ", s);
return null;
}
let texture = PIXI.Texture.fromFrame(s);
texture = texture ? texture : PIXI.Texture.fromFrame(s + '.png');
if (texture) {
let gfx = new PIXI.Sprite(texture);
gfx.anchor.x = .5;
gfx.anchor.y = .5;
if (layer)
layer.addChild(gfx); else {
}
return gfx
} else {
console.log("@@@@Can't find ", s);
return null;
}
}
_(s: string) {
return this.sm.findStringId(s)
}
static GET(url: string, cb: Function) {
let xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
cb(xmlHttp.responseText);
}
};
xmlHttp.open("GET", url, true); // true for asynchronous
xmlHttp.send(null);
}
setScreenRes(baseW: number, baseH: number) {
//this.appScale = baseH / MIN_SCR_HEIGHT;
//if (this.appScale > 1.28) this.appScale = 1.28;
//SCR_WIDTH = baseW / this.appScale;
//SCR_HEIGHT = baseH / this.appScale;
//SCR_WIDTH_HALF = SCR_WIDTH * .5;
//SCR_HEIGHT_HALF = SCR_HEIGHT * .5;
//this.screenCenterOffset = [(SCR_WIDTH - MIN_SCR_WIDTH) * .5, (SCR_HEIGHT - MIN_SCR_HEIGHT) * .5];
this.screenCenterOffset = [0, 0];
}
start() {
document.addEventListener('contextmenu', (event) => {
if (this.onContext) this.onContext();
event.preventDefault()
});
this.setScreenRes(SCR_WIDTH, SCR_HEIGHT);
//TweenMax.lagSmoothing(0);
this.appScale = window.PIXIRatio ? window.PIXIRatio : 1;
console.log( window.devicePixelRatio);
this.app = new PIXI.Application(SCR_WIDTH, SCR_HEIGHT, {
autoStart: true,
clearBeforeRender: true,
resolution: window.devicePixelRatio, antialias: false,
preserveDrawingBuffer: false, forceFXAA: true, backgroundColor: 0xffffff,
});
document.body.appendChild(this.app.view);
this.camera = new PIXI.Container();
this.camera.x = 0;
this.camera.y = 0;
// TODO: camera
this.app.stage = new PIXI.Container();
this.sm = new SM();
this.sm.init();
// this.app.stage.scrollRect = new PIXI.Rectangle(10, 10, 100, 100);
//this.app.stage.position.set(this.app.renderer.width/2, this.app.renderer.height/2);
this.app.stage.scale.set(this.appScale, this.appScale);
// this.app.renderer.plugins.interaction = new PIXI.interaction.InteractionManager(this.app.renderer, {autoPreventDefault: false});
this.lm = new LM();
this.sm.createCamera();
this.lastLoop = (new Date()).getTime();
this.lastNetworkPing = this.lastLoop;
this.bindedAnimate = this.animate.bind(this);
this.app.ticker.add(this.bindedAnimate);
};
loadComplete(): void {
this.isInitialLoading = false;
this.loadTime = (new Date()).getTime() - window.startTime.getTime();
this.clearPreloader();
const interaction = this.app.renderer.plugins.interaction;
document.addEventListener('mousedown', (e) => {
if (this.globalMouseDown) this.globalMouseDown(e), false
});
// this.app.stage.pivot.set(this.app.renderer.width, this.app.renderer.height);
/*interaction.cursorStyles["init"] = "url(/game/cursors/cursor.png), default";
interaction.cursorStyles["default"] = "url(/game/cursors/cursor.png), default";
interaction.cursorStyles["pointer"] = "url(/game/cursors/hand.png), pointer";
interaction.cursorStyles["skill"] = "url(/game/cursors/cursor_skill.png), pointer";
interaction.currentCursorMode = "init";
this.app.stage.interactive = true;
this.app.stage.cursor = "init";*/
this.sm.openStage(this.menu);
}
initPreloader() {
this.preloadBar = new PIXI.Graphics();
this.app.stage.addChild(this.preloadBar);
const borderWidth = 3;
this.preloadBar.beginFill(0x000000);
this.preloadBar.moveTo(SCR_WIDTH * 0.1 - borderWidth, SCR_HEIGHT * 0.495 - borderWidth);
this.preloadBar.lineTo(SCR_WIDTH * 0.9 + borderWidth, SCR_HEIGHT * 0.495 - borderWidth);
this.preloadBar.lineTo(SCR_WIDTH * 0.9 + borderWidth, SCR_HEIGHT * 0.505 + borderWidth);
this.preloadBar.lineTo(SCR_WIDTH * 0.1 - borderWidth, SCR_HEIGHT * 0.505 + borderWidth);
this.preloadBar.endFill();
}
drawPreloaderProgress(progressPercent: number): void {
this.preloadBar.beginFill(0x000000);
const progress = progressPercent / 100;
this.preloadBar.moveTo(SCR_WIDTH * 0.1, SCR_HEIGHT * 0.495);
this.preloadBar.lineTo(SCR_WIDTH * 0.1 + SCR_WIDTH * 0.8 * progress, SCR_HEIGHT * 0.495);
this.preloadBar.lineTo(SCR_WIDTH * 0.1 + SCR_WIDTH * 0.8 * progress, SCR_HEIGHT * 0.505);
this.preloadBar.lineTo(SCR_WIDTH * 0.1, SCR_HEIGHT * 0.505);
this.preloadBar.endFill();
}
clearPreloader() {
this.app.stage.removeChild(this.preloadBar);
}
load(): void {
this.loadingCounter = 0;
this.initPreloader();
let onAssetsLoaded = () => {
this.loadingCounter++;
if (this.loadingCounter == 2) this.loadComplete()
};
this.rm = new ResourceManager();
this.rm.loadAssets(GLOBAL_ASSETS.concat(LevelNames), (loader: any, evt: any) => {
this.drawPreloaderProgress(loader.progress);
this.assetsLoaded++;
}, onAssetsLoaded);
this.sound = new Sound();
this.sound.load(GLOBAL_MUSIC_ASSETS, GLOBAL_SOUND_ASSETS, onAssetsLoaded);
}
animate(): void {
// this.stats.begin();
this.timer.process();
this.random = Math.random();
this.time = (new Date()).getTime();
this.cursorPos = this.app.renderer.plugins.interaction.mouse.global;
if (!this.isInitialLoading) {
let timeD = (this.time - this.lastLoop);
this.lastLoop = this.time;
this.deltaSec = timeD / 1000.;
this.delta = timeD / FRAME_DELAY;
this.totalDelta += this.delta;
this.totalFrames++;
this.sm.process();
}
}
killTween(tween: any): null {
if (tween && tween.totalProgress() != 1)
tween.totalProgress(1).kill();
return null
}
addFilter(m: PIXI.Container, x: PIXI.Filter<any>) {
let mm: any = <any>(m);
if (!mm._filters) mm._filters = [];
mm._filters.push(x);
}
removeFilterByType(main: PIXI.Container, ftype: any) {
let m = <any>main;
if (!m._filters) return;
for (let x = m._filters.length - 1; x>=0; x--) {
if (m._filters[x] instanceof ftype) {
m._filters.splice(x, 1);
}
}
}
removeFilter(main: PIXI.Container, f: PIXI.Filter<any>) {
let m = <any>main;
m._filters.splice(_.sm.main.filters.indexOf(f), 1);
}
}
export var _: Main = new Main();
_.start();
_.load();