-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.ts
135 lines (112 loc) · 4.32 KB
/
ui.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
interface RecordEntry {
pos: Point
kills: number
}
class UI {
private airborneSpan: HTMLSpanElement
private altitudeSpan: HTMLSpanElement
private recordDiv: HTMLElement
private recordTypeSpan: HTMLSpanElement
private recordValueSpan: HTMLElement
private recordHeight: RecordCategory
private recordFall: RecordCategory
private recordKills: RecordCategory
private recordDistance: RecordCategory
private recordAirtime: RecordCategory
private recordTutorial: RecordCategory
private lastPlayerAirtime = 0
private recordShowTimeLeft = 0
private totalTime = 0
private timeRecorded = 0
private record: RecordEntry[] = Array()
private tutorialUnderstood = false
constructor() {
this.airborneSpan = document.getElementById("ui_airborne")
this.altitudeSpan = document.getElementById("ui_altitude")
this.recordDiv = document.getElementById("ui_records")
this.recordTypeSpan = document.getElementById("ui_records_type")
this.recordValueSpan = document.getElementById("ui_records_value")
this.recordHeight = new RecordCategory(
"Altitude Record",
(value) => Math.floor(value / CONST.METER).toString() + " meters",
CONST.PLAYER_START_ALTITUDE )
this.recordFall = new RecordCategory(
"Highest Fall",
(value) => Math.floor(value / CONST.METER).toString() + " meters",
CONST.PLAYER_START_ALTITUDE )
this.recordAirtime = new RecordCategory(
"Airtime record",
(value) => value.toFixed(1) + " seconds",
4)
this.recordDistance = new RecordCategory(
"Horizontal Distance per Minute record",
(value) => Math.floor(value / CONST.METER).toString() + " meters",
200)
this.recordKills = new RecordCategory(
"Kill per Minute record",
(value) => value.toString(),
0)
}
update(deltaTime: number, player: Player) {
// basic display
this.altitudeSpan.textContent = Math.floor(player.pos.y / -CONST.METER).toString()
this.airborneSpan.textContent = player.airtime.toFixed(1)
if(this.recordShowTimeLeft < 0) {
this.recordDiv.style.display = "none"
} else {
this.recordDiv.style.display = "block"
}
// update every-frame records
if (player.isGrounded) {
this.recordFall.checkRecord(player.falldist, this)
this.recordAirtime.checkRecord(this.lastPlayerAirtime, this)
}
this.lastPlayerAirtime = player.airtime
this.recordHeight.checkRecord(-player.pos.y, this)
// update time-based records
while(this.timeRecorded < this.totalTime) {
this.record.push({
pos: { x: player.pos.x, y: player.pos.y },
kills: player.kills
})
if (this.record.length > 60) {
let begin = this.record.shift()
let end = this.record[59]
this.recordKills.checkRecord(end.kills - begin.kills, this)
this.recordDistance.checkRecord(Math.abs(end.pos.x - begin.pos.x), this)
}
this.timeRecorded += 1
}
/*
if (player.pos.y < -200 && player.velocity.y < 0) {
this.tutorialUnderstood = true
}
if(this.tutorialUnderstood === false ) {
this.postRecord("Aim&Shoot with mouse", "A&D or left&right for moving while airborne")
}
*/
this.totalTime += deltaTime
this.recordShowTimeLeft -= deltaTime
}
postRecord(type: string, value: string) {
this.recordShowTimeLeft = CONST.RECORD_SHOW_DURATION
this.recordTypeSpan.textContent = type
this.recordValueSpan.textContent = value
}
}
class RecordCategory{
headline: string
toStringFunc: (value:number) => string
private best: number
constructor(headline:string, toString: (value:number) => string, best: number) {
this.headline = headline
this.toStringFunc = toString
this.best = best
}
checkRecord(current: number, ui: UI) {
if (current > this.best) {
this.best = current
ui.postRecord(this.headline, this.toStringFunc(this.best))
}
}
}