-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.ts
58 lines (46 loc) · 2.01 KB
/
renderer.ts
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
// the rendering system
class Renderer {
static readonly COLOR_SKY: string = "#66ddff";
static readonly COLOR_GROUND: string = "#88ddbb";
private canvas: HTMLCanvasElement;
private ctx: CanvasRenderingContext2D;
constructor(canvas: HTMLCanvasElement) {
this.canvas = canvas
this.canvas.width = CONST.SCREEN_WIDTH
this.canvas.height = CONST.SCREEN_HEIGHT
this.ctx = canvas.getContext("2d")
}
update(deltaTime: number, state: State) {
// background
this.ctx.fillStyle = Renderer.COLOR_SKY
this.ctx.fillRect(0, 0, CONST.SCREEN_WIDTH, CONST.SCREEN_HEIGHT)
// ground
let offset: Point = { x: CONST.SCREEN_WIDTH / 2 - state.camera.pos.x, y: CONST.SCREEN_HEIGHT / 2 - state.camera.pos.y }
this.ctx.fillStyle = Renderer.COLOR_GROUND
this.ctx.fillRect(0, offset.y, CONST.SCREEN_WIDTH, CONST.SCREEN_HEIGHT)
// objects
state.doWithAllEntities(r => {
if (r.sprite && r.sprite.isLoaded) {
this.ctx.save()
this.ctx.translate( Math.round(r.pos.x + offset.x),
Math.round(r.pos.y + offset.y) )
if (r.sprite.rotation !== 0) {
this.ctx.rotate(r.sprite.rotation)
}
if (r.sprite.flipped){
this.ctx.scale(-1, 1)
}
this.ctx.translate( -r.sprite.renderPivot.x, -r.sprite.renderPivot.y)
this.ctx.globalAlpha = r.sprite.alpha
r.sprite.draw(this.ctx)
this.ctx.restore()
if (CONST.DEBUG_SHOW_COLLIDERS) {
this.ctx.strokeStyle = "#ff0000"
this.ctx.beginPath();
this.ctx.arc(r.pos.x + offset.x, r.pos.y + offset.y, Math.sqrt(r.collisionRadiusSqare), 0, Math.PI * 2);
this.ctx.stroke();
}
}
})
}
}