flappyDojo/src/Game.js

41 lines
710 B
JavaScript

// @flow
import type Entity from './Entities/Entity'
const fps = 30
class Game {
entities: Array<Entity>
ctx: CanvasRenderingContext2D
constructor(canvas: HTMLElement){
this.entities = []
this.ctx = canvas.getContext("2d")
this.canvasSize = {
x: canvas.offsetWidth,
y: canvas.offsetHeight,
}
}
start(){
this._draw()
}
_draw(){
setTimeout(() => {
requestAnimationFrame(() => this._draw())
this.ctx.clearRect(0, 0, this.canvasSize.x, this.canvasSize.y)
this.entities.forEach(e => {
e.update()
e.render(this.ctx)
})
}, 1000 / fps)
}
addEntity(e: Entity){
this.entities.push(e)
}
}
export default Game