rayjs/examples/ts_game/src/game.ts

64 lines
1.7 KiB
TypeScript
Raw Normal View History

2023-05-27 13:03:29 +00:00
import { EntityOf } from "./entity";
import { resourceUnloadAll } from "./resource";
2023-05-27 09:35:08 +00:00
const promiseUpdateList: (()=>boolean)[] = []
2023-05-27 13:03:29 +00:00
const entitiyList: EntityOf<any>[] = []
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
}
2023-05-27 13:03:29 +00:00
export const entityAdd = (entity: EntityOf<any>) => {
if (entity.load) entity.load(entity)
entitiyList.push(entity)
}
2023-05-27 09:35:08 +00:00
2023-05-27 13:03:29 +00:00
export const entityRemove = (entity: EntityOf<any>) => {
// TODO: Do this cached
const i = entitiyList.findIndex(x => x.id === entity.id)
if (i !== -1) {
const e = entitiyList[i]
if (e.unload) e.unload(entity)
entitiyList.splice(i, 1)
}
2023-05-27 13:03:29 +00:00
}
2023-05-27 13:03:29 +00:00
export const runGame = (width: number, height: number, title: string, startupCallback: () => void) => {
initWindow(width, height, title)
setTargetFPS(60)
startupCallback()
while(!windowShouldClose()){
dispatchPromises()
for (const entity of entitiyList) {
if (entity.update) entity.update(entity)
}
beginDrawing()
clearBackground(BLACK)
for (const entity of entitiyList) {
if (entity.draw) entity.draw(entity)
}
2023-05-27 13:03:29 +00:00
endDrawing()
}
2023-05-27 13:03:29 +00:00
resourceUnloadAll()
closeWindow()
}