2023-05-27 09:35:08 +00:00
|
|
|
const promiseUpdateList: (()=>boolean)[] = []
|
2023-05-20 19:34:27 +00:00
|
|
|
|
2023-05-27 09:35:08 +00:00
|
|
|
const dispatchPromises = () => {
|
|
|
|
for (var i = promiseUpdateList.length - 1; i >= 0; i--) {
|
|
|
|
const finished = promiseUpdateList[i]()
|
|
|
|
if (finished) {
|
|
|
|
promiseUpdateList.splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const makeUpdateablePromise = (updateFn: () => boolean) => {
|
|
|
|
let resFn: () => void
|
|
|
|
const promise = new Promise<void>((resolve, reject) => {
|
|
|
|
resFn = resolve
|
|
|
|
});
|
|
|
|
const update = () => {
|
|
|
|
const res = updateFn()
|
|
|
|
if(res) resFn()
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
promiseUpdateList.unshift(update)
|
|
|
|
return promise
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export abstract class Game {
|
|
|
|
public clearColor = BLACK
|
2023-05-20 19:34:27 +00:00
|
|
|
private quit = false
|
|
|
|
|
|
|
|
constructor(public readonly width: number,
|
|
|
|
public readonly height: number,
|
|
|
|
public readonly title: string){
|
|
|
|
}
|
|
|
|
|
|
|
|
public run(){
|
|
|
|
initWindow(this.width,this.height,this.title)
|
|
|
|
setTargetFPS(60)
|
2023-05-27 09:35:08 +00:00
|
|
|
this.load()
|
2023-05-20 19:34:27 +00:00
|
|
|
while(!(this.quit = windowShouldClose())){
|
2023-05-27 09:35:08 +00:00
|
|
|
dispatchPromises()
|
|
|
|
this.update()
|
2023-05-20 19:34:27 +00:00
|
|
|
beginDrawing()
|
|
|
|
clearBackground(this.clearColor)
|
2023-05-27 09:35:08 +00:00
|
|
|
this.draw()
|
2023-05-20 19:34:27 +00:00
|
|
|
endDrawing()
|
|
|
|
}
|
2023-05-27 09:35:08 +00:00
|
|
|
this.unload()
|
2023-05-20 19:34:27 +00:00
|
|
|
closeWindow()
|
|
|
|
}
|
|
|
|
|
2023-05-27 09:35:08 +00:00
|
|
|
abstract draw(): void;
|
|
|
|
abstract update(): void;
|
|
|
|
abstract load(): void;
|
|
|
|
abstract unload(): void;
|
2023-05-20 19:34:27 +00:00
|
|
|
}
|