-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathindex.ts
136 lines (108 loc) · 3.74 KB
/
index.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
import { MarioGame } from "./mariogame";
import { InputController } from "./input_controller";
import { FPSController } from "./fps_controller";
declare var rivets;
class MyApp{
ctx:CanvasRenderingContext2D;
canvas:HTMLCanvasElement;
marioGame:MarioGame;
inputController:InputController;
mobileMode = false;
isSixtyFPS = true;
fpsController:FPSController;
useragent:string = '';
constructor(){
rivets.bind(document.getElementsByTagName('body')[0], { data: this });
this.canvas = document.getElementById('my-canvas') as HTMLCanvasElement;
this.useragent = navigator.userAgent.toLocaleLowerCase();
//mobile device
if ((window.innerWidth<600 || this.useragent.includes('iphone') ||
this.useragent.includes('ipad') || this.useragent.includes('android'))
&& window.innerWidth<window.innerHeight)
{
this.mobileMode = true;
}
if (this.mobileMode)
{
document.getElementById('header').style.display = "none";
$("#mobileDiv").show();
$('#my-canvas').width(window.innerWidth);
$('#my-canvas').appendTo("#mobileCanvas");
$("body").css({"overflow":"hidden"});
this.inputController = new InputController('divTouchSurface');
document.getElementById('my-canvas').addEventListener( 'touchstart', function (e) { e.preventDefault(); }, false );
document.getElementById('my-canvas').addEventListener( 'touchend', function (e) { e.preventDefault(); }, false );
document.getElementById('my-canvas').addEventListener( 'touchmove', function (e) { e.preventDefault(); }, false );
}
else
{
this.inputController = new InputController('divMain');
//MS Edge doesn't work well with wasd keys, misses some key up events
//also spacebar key made ux scroll sometimes so using a and s instead
this.inputController.KeyMappings = {
Mapping_Left:'Left',
Mapping_Right:'Right',
Mapping_Up:'Up',
Mapping_Down:'Down',
// Mapping_Action_1:'Shift',
// Mapping_Action_2:' ',
Mapping_Action_1:'a',
Mapping_Action_2:'s',
Mapping_Start:'p',
Mapping_Select:'n',
Mapping_Joy_L: ',',
Mapping_Joy_R: '.'
};
this.inputController.setupGamePad();
// this.inputController.Gamepad_Process_Axis = true;
$('#divMain').show();
}
this.ctx = this.canvas.getContext('2d');
this.createGame();
this.fpsController = new FPSController(60);
$('#divLoading').hide();
window.requestAnimationFrame(this.draw.bind(this));
}
createGame(){
this.marioGame = new MarioGame(this.canvas.width, this.canvas.height,this.ctx,
this.inputController, this.mobileMode, this.isSixtyFPS);
}
//30 FPS mode to experience the game as originally designed
thirty(){
this.isSixtyFPS = false;
this.fpsController.UpdateTargetFPS(30);
this.fpsController.enabled = true;
this.createGame();
}
//switch back to 60 FPS mode which is the default
sixty(){
this.isSixtyFPS = true;
this.fpsController.UpdateTargetFPS(60);
this.fpsController.enabled = false;
this.createGame();
}
prevLevel(){
if (this.marioGame.currentLevel>1)
this.marioGame.loadLevel(this.marioGame.currentLevel-1);
}
nextLevel(){
if (this.marioGame.currentLevel<8)
this.marioGame.loadLevel(this.marioGame.currentLevel+1);
}
btnClick(){
this.marioGame.startGame();
}
drawGame(){
this.marioGame.draw();
}
draw(){
if (this.fpsController.enabled==false) //not using frame skipping - draw as fast as screen allows
this.drawGame();
else if (this.fpsController.IsFrameReady()) //use frame skipping for either high refresh or for 30FPS mode
{
this.drawGame();
}
window.requestAnimationFrame(this.draw.bind(this));
}
}
window["myApp"] = new MyApp();