-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframework.js
76 lines (64 loc) · 1.8 KB
/
framework.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
class Framework {
constructor() {
this.chars = []
this.width = 0
this.height = 0
this.FPS = 0
this.nextFrameTimeout
}
gameLoop() {
let time = Date.now()
if (update)
update()
let calculatedText = this.chars.map((row) => row.join(""))
calculatedText = calculatedText.join("\n")
if (document.getElementById("frameworkPre"))
document.getElementById("frameworkPre").innerText = calculatedText
let now = Date.now()
let timeTaken = now - time
this.nextFrameTimeout = setTimeout(this.gameLoop.bind(this), (1000 / FPS - timeTaken))
}
}
let framework = new Framework()
function fillScreen(char) {
for (let y = 0; y < framework.chars.length; y++) {
for (let x = 0; x < framework.chars[0].length; x++)
framework.chars[y][x] = char
}
}
function print(text, x, y) {
const origX = x
let cursorX = x
let cursorY = y
for (let c of text) {
if (c == "\n") {
cursorY += 1
cursorX = origX
continue
}
if (framework.chars[cursorY] && framework.chars[cursorY][cursorX])
framework.chars[cursorY][cursorX] = c
cursorX += 1
}
}
function setFPS(fps) {
this.FPS = fps
framework.gameLoop()
}
function setSize(width, height) {
framework.chars = []
for (let y = 0; y < height; y++) {
let cur = []
for (let x = 0; x < width; x++)
cur.push(" ")
framework.chars.push(cur)
}
if (document.getElementById("frameworkPre") == null) {
let textContainer = document.createElement("pre")
textContainer.id = "frameworkPre"
document.body.append(textContainer)
}
}
function get(x, y) {
return framework.chars[y][x]
}