-
Notifications
You must be signed in to change notification settings - Fork 1
/
jump.ts
171 lines (165 loc) · 5.03 KB
/
jump.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
import ThreeD, {
ThreeDParms
} from "./threeD";
enum Level {
EASY = "简单",
MEDIUM = "中等",
DIFFICULT = "困难",
NONE = "NONE"
}
interface JumpConfig extends ThreeDParms {
}
class Jump {
constructor() {
this.threeD = new ThreeD(this.config);
this.appDom = document.getElementById("jump_app") as HTMLDivElement;
this.startDom = document.getElementById("jump_startGame") as HTMLButtonElement;
this.restartDom = document.getElementById("jump_restartGame") as HTMLButtonElement;
this.scoreDom = document.getElementById("jump_score") as HTMLDivElement;
this.scoreTotalDom = document.getElementById("jump_scoreTotal") as HTMLSpanElement;
}
private readonly config: JumpConfig = {
renderClearColor: "#fbde9f",
cubeColor: "#ca746e",
cubeWidth: 4,
cubeHeight: 2,
cubeDeep: 4,
maxDistance: 5,
playerColor: "red",
playerWidth: 1,
playerheight: 2,
playerDeep: 1,
playerMinScale: 0.1,
playerSpeedD: 0.004,
playerSpeedY: 0.008,
listener: this.getListener(),
onPlayerFail: this.gameOver.bind(this),
onPlayerSuccess: this.addScore.bind(this)
}
private readonly threeD: ThreeD;
private readonly startDom: HTMLButtonElement;
private readonly restartDom: HTMLButtonElement;
private readonly appDom: HTMLDivElement;
private readonly scoreDom: HTMLDivElement;
private readonly scoreTotalDom: HTMLSpanElement;
private readonly mobileSubDom: HTMLButtonElement;
private updateConfigByLevel(): void {
const level = this.getLevel();
switch (level) {
case Level.EASY:
this.config.cubeDeep = 4;
this.config.cubeWidth = 4;
break;
case Level.MEDIUM:
this.config.cubeDeep = 4;
this.config.cubeWidth = 2;
break;
case Level.DIFFICULT:
this.config.cubeDeep = 2;
this.config.cubeWidth = 2;
break;
}
}
private addScore(): void {
if (this.scoreTotalDom) {
const currentScoreTotal = Number(this.scoreTotalDom.innerHTML);
this.scoreTotalDom.innerHTML = String(100 + currentScoreTotal);
}
}
private initScore(): void {
if (this.scoreTotalDom) {
this.scoreTotalDom.innerHTML = "0";
}
}
private getListener(): HTMLElement {
return document.querySelector("canvas") || document.body;
}
private preventDefault (): void {
document.oncontextmenu = (e) => {
e.preventDefault();
}
}
private addRestartListener(): boolean {
const _self = this;
if ( this.restartDom ) {
this.restartDom.addEventListener("click", _self.gameRestart.bind(_self));
return true;
}
return false;
}
private addStartListener(): boolean {
const _self = this;
if ( this.startDom ) {
this.startDom.addEventListener("click", _self.gameStart.bind(_self));
return true;
}
return false;
}
private hiddenAppDom(): void {
if (this.appDom) {
this.appDom.style.display = "none";
}
}
private showAppDom(): void {
if (this.appDom) {
this.appDom.style.display = "block";
}
}
private hiddenStartDom(): void {
if (this.startDom) {
this.startDom.style.display = "none";
}
}
private showRestartDom(): void {
if (this.restartDom) {
this.restartDom.style.display = "inline-block";
}
}
private showScoreDom(): void {
if (this.scoreDom) {
this.scoreDom.style.display = "block";
}
}
private getLevel(): Level {
const select: HTMLSelectElement = document.getElementById("jump_select") as HTMLSelectElement;
if (select) {
return select.value as Level;
}
return Level.NONE;
}
public gameRender(): void {
this.addStartListener();
this.preventDefault();
}
public gameStart(): void {
this.hiddenAppDom();
this.showScoreDom();
this.hiddenStartDom();
this.showRestartDom();
this.updateConfigByLevel();
this.threeD.initRender();
this.threeD.initCamera();
this.threeD.initLight();
this.threeD.addCube();
this.threeD.addCube();
this.threeD.addPlayer();
this.addRestartListener();
this.threeD.addMouseListener();
}
public gameRestart(): void {
this.hiddenAppDom();
this.showScoreDom();
this.initScore();
this.updateConfigByLevel();
this.threeD.initProperty();
this.threeD.initCamera();
this.threeD.addCube();
this.threeD.addCube();
this.threeD.addPlayer();
this.threeD.addMouseListener();
}
public gameOver(): void {
this.showAppDom();
}
}
export default Jump;