Skip to content

Commit

Permalink
feat(client): entity masking
Browse files Browse the repository at this point in the history
  • Loading branch information
Veradictus committed Sep 17, 2023
1 parent 4dd3fcc commit 70e0633
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
1 change: 1 addition & 0 deletions packages/client/components/game.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<canvas id="background"></canvas>
<canvas id="entities"></canvas>
<canvas id="foreground"></canvas>
<canvas id="entities-mask"></canvas>
<canvas id="cursor"></canvas>
<canvas id="overlay"></canvas>
<canvas id="text-canvas"></canvas>
Expand Down
36 changes: 18 additions & 18 deletions packages/client/data/sprites.json
Original file line number Diff line number Diff line change
Expand Up @@ -6019,8 +6019,8 @@
"row": 0
}
},
"offsetX": 0,
"offsetY": 0
"offsetX": -12,
"offsetY": -12
},
{
"id": "projectiles/firearrow",
Expand All @@ -6031,8 +6031,8 @@
"row": 0
}
},
"offsetX": 0,
"offsetY": 0
"offsetX": -16,
"offsetY": -8
},
{
"id": "projectiles/poisonarrow",
Expand All @@ -6043,8 +6043,8 @@
"row": 0
}
},
"offsetX": 0,
"offsetY": 0
"offsetX": -12,
"offsetY": -12
},
{
"id": "projectiles/icearrow",
Expand All @@ -6055,8 +6055,8 @@
"row": 0
}
},
"offsetX": 0,
"offsetY": 0
"offsetX": -12,
"offsetY": -12
},
{
"id": "projectiles/lightningarrow",
Expand All @@ -6067,8 +6067,8 @@
"row": 0
}
},
"offsetX": 0,
"offsetY": 0
"offsetX": -12,
"offsetY": -12
},
{
"id": "projectiles/nisocarrow",
Expand All @@ -6079,8 +6079,8 @@
"row": 0
}
},
"offsetX": 0,
"offsetY": 0
"offsetX": -12,
"offsetY": -12
},
{
"id": "projectiles/cinnabararrow",
Expand All @@ -6091,8 +6091,8 @@
"row": 0
}
},
"offsetX": 0,
"offsetY": 0
"offsetX": -12,
"offsetY": -12
},
{
"id": "projectiles/pythararrow",
Expand All @@ -6103,8 +6103,8 @@
"row": 0
}
},
"offsetX": 0,
"offsetY": 0
"offsetX": -12,
"offsetY": -12
},
{
"id": "projectiles/iboarrow",
Expand All @@ -6115,8 +6115,8 @@
"row": 0
}
},
"offsetX": 0,
"offsetY": 0
"offsetX": -12,
"offsetY": -12
},
{
"id": "projectiles/none",
Expand Down
3 changes: 2 additions & 1 deletion packages/client/scss/game/impl/_container.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#foreground,
#cursor,
#text-canvas,
#overlay {
#overlay,
#entities-mask {
position: absolute;
transform: translateZ(0);
margin: 0;
Expand Down
18 changes: 15 additions & 3 deletions packages/client/src/renderer/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export default class Renderer {
protected textCanvas = document.querySelector<HTMLCanvasElement>('#text-canvas')!;
protected entities = document.querySelector<HTMLCanvasElement>('#entities')!;
protected cursor = document.querySelector<HTMLCanvasElement>('#cursor')!;
protected entitiesMask = document.querySelector<HTMLCanvasElement>('#entities-mask')!;

// Store all canvases for easy iteration
protected canvases: HTMLCanvasElement[] = [
Expand All @@ -65,24 +66,32 @@ export default class Renderer {
this.overlay,
this.textCanvas,
this.entities,
this.cursor
this.cursor,
this.entitiesMask
];

// Create the contexts based on the canvases.
protected entitiesContext: CanvasRenderingContext2D = this.entities.getContext('2d')!;
protected overlayContext: CanvasRenderingContext2D = this.overlay.getContext('2d')!;
protected textContext: CanvasRenderingContext2D = this.textCanvas.getContext('2d')!;
protected cursorContext: CanvasRenderingContext2D = this.cursor.getContext('2d')!;
protected entitiesMaskContext: CanvasRenderingContext2D = this.entitiesMask.getContext('2d')!;

protected allContexts = [
this.entitiesContext,
this.overlayContext,
this.textContext,
this.cursorContext
this.cursorContext,
this.entitiesMaskContext
];

// We split contexts into two arrays, one for tilemap rendering and one for the rest.
protected contexts = [this.entitiesContext, this.textContext, this.overlayContext];
protected contexts = [
this.entitiesContext,
this.textContext,
this.overlayContext,
this.entitiesMaskContext
];

// Zooming buttons
private zoomIn: HTMLElement = document.querySelector('#zoom-in')!;
Expand Down Expand Up @@ -383,6 +392,9 @@ export default class Renderer {

this.drawEntity(entity);
});

this.entitiesMaskContext.globalAlpha = 0.3;
this.entitiesMaskContext.drawImage(this.entities, 0, 0);
}

/**
Expand Down

0 comments on commit 70e0633

Please sign in to comment.