-
Notifications
You must be signed in to change notification settings - Fork 4
/
urlHunter.js
149 lines (133 loc) · 3.58 KB
/
urlHunter.js
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
class Game {
constructor() {
this.levelSize = 60
this.playerLocation = this.levelSize / 2
}
start() {
this.lastFrame = Date.now()
this.points = 0
this.startTime = new Date()
this.timeLimit = 30 // seconds
this.animals = []
for (let i = 0; i < 4; i++) {
let animal = new Animal(Math.floor(Math.random() * this.levelSize))
this.animals.push(animal)
}
this.update()
}
gameOver() {
cancelAnimationFrame(this.cancelId)
location.replace(
`#__You_killed_${this.points}_animal${
this.points === 1 ? "" : "'s"
} in ${this.elapsedTime()}_seconds!_(Press_ESC_to_play_again)`
)
}
elapsedTime() {
let milliseconds = new Date().getTime() - this.startTime.getTime()
return Math.floor(milliseconds / 1000)
}
// Animal Methods
// --------------
removeAnimal(deadAnimal) {
this.animals = this.animals.filter((animal) => animal !== deadAnimal)
}
animalAt(position) {
return this.animals.find(
(animal) => Math.floor(animal.position) === position
)
}
// Gamestate Methods
// -----------------
update() {
const lastFrame = this.lastFrame
const delta = Date.now() - lastFrame
if (delta > 1000 / 10) {
this.lastFrame = Date.now()
for (let animal of this.animals) {
animal.update(this.levelSize)
}
this.draw()
}
this.cancelId = window.requestAnimationFrame(() => this.update())
}
draw() {
let url = ""
while (url.length < this.levelSize) {
let position = url.length
if (position === this.playerLocation) {
url += this.animalAt(this.playerLocation) ? "@" : "O"
} else if (this.animalAt(position)) {
url += "a"
} else {
url += "-"
}
}
let timeLeft = this.timeLimit - this.elapsedTime()
if (timeLeft <= 0) {
this.gameOver()
} else {
if (timeLeft < 10) {
timeLeft = "0" + timeLeft // Keep the same width
}
location.replace(
`#_seconds_left_${timeLeft}|` + url + `|${timeLeft}_animal$`
)
document.title = `Points ${this.points}`
}
}
onKeyDown(event) {
if (event.which === 37) {
// left
this.playerLocation -= 1
if (this.playerLocation < 0) {
this.playerLocation = this.levelSize - 1
}
} else if (event.which === 39) {
// right
this.playerLocation += 1
this.playerLocation %= this.levelSize
} else if (event.which === 38 || event.which === 32) {
// attack
let animal = this.animalAt(this.playerLocation)
if (animal) {
this.points += 1
this.removeAnimal(animal)
if (this.animals.length === 0) {
this.gameOver()
}
}
} else if (event.which === 27) {
// enter
this.start()
}
}
}
class Animal {
constructor(position) {
this.position = position
this.velocityChange = Math.random() * 0.5
this.velocityIndex = Math.random() * Math.PI
}
update(levelSize) {
let dampener = 0.4
this.velocityIndex += Math.random() * this.velocityChange
this.position += Math.sin(this.velocityIndex) * dampener
this.position %= levelSize
if (this.position < 0) {
this.position += levelSize
}
}
}
document.addEventListener("DOMContentLoaded", () => {
let isSafari =
navigator.userAgent.indexOf("Safari") != -1 &&
navigator.userAgent.indexOf("Chrome") == -1
if (isSafari) {
document.body.classList.toggle("safari")
} else {
let game = new Game()
document.addEventListener("keydown", (...args) => game.onKeyDown(...args))
game.start()
}
})