added example updates

master
Christian Dreier 2017-05-30 22:43:58 +02:00
parent 38ee7a6b65
commit 77122e6908
3 changed files with 29 additions and 7 deletions

View File

@ -2,9 +2,8 @@ import Game from './src/Game'
import Entity from './src/Entities/Entity' import Entity from './src/Entities/Entity'
const canvas = document.getElementById("canvas") const canvas = document.getElementById("canvas")
const ctx = canvas.getContext("2d")
const flappyGame = new Game(ctx) const flappyGame = new Game(canvas)
const e1 = new Entity() const e1 = new Entity()
flappyGame.addEntity(e1) flappyGame.addEntity(e1)

View File

@ -1,8 +1,23 @@
class Entity { class Entity {
constructor(){
this.size = {
x: 50,
y: 50,
}
this.pos = {
x: 10,
y: 10,
}
}
update(){
this.pos.x += 1;
}
render(ctx: CanvasRenderingContext2D){ render(ctx: CanvasRenderingContext2D){
ctx.fillStyle = 'rgb(200,0,0)'; ctx.fillStyle = 'rgb(200,0,0)';
ctx.fillRect(10, 10, 55, 50); ctx.fillRect(this.pos.x, this.pos.y, this.size.x, this.size.y);
} }
} }

View File

@ -8,9 +8,13 @@ class Game {
entities: Array<Entity> entities: Array<Entity>
ctx: CanvasRenderingContext2D ctx: CanvasRenderingContext2D
constructor(_ctx: CanvasRenderingContext2D){ constructor(canvas: HTMLElement){
this.entities = [] this.entities = []
this.ctx = _ctx this.ctx = canvas.getContext("2d")
this.canvasSize = {
x: canvas.offsetWidth,
y: canvas.offsetHeight,
}
} }
start(){ start(){
@ -20,7 +24,11 @@ class Game {
_draw(){ _draw(){
setTimeout(() => { setTimeout(() => {
requestAnimationFrame(() => this._draw()) requestAnimationFrame(() => this._draw())
this.entities.forEach(e => e.render(this.ctx)) this.ctx.clearRect(0, 0, this.canvasSize.x, this.canvasSize.y)
this.entities.forEach(e => {
e.update()
e.render(this.ctx)
})
}, 1000 / fps) }, 1000 / fps)
} }