Skip to content

Commit

Permalink
Scale and vertical position as the parameters of sprites
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Jul 5, 2024
1 parent a835e0d commit d5ede56
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
6 changes: 3 additions & 3 deletions game.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,8 @@ function displaySwapBackImageData(display: Display) {
export interface Sprite {
imageData: ImageData;
position: Vector2;
z: number;
scale: number;
}

function renderSprites(display: Display, player: Player, sprites: Array<Sprite>) {
Expand All @@ -560,15 +562,13 @@ function renderSprites(display: Display, player: Player, sprites: Array<Sprite>)
const cy = display.backImageData.height*0.5;
const pdist = sprite.position.clone().sub(player.position).dot(dir);
if (pdist < NEAR_CLIPPING_PLANE) continue; // TODO: I'm not sure if this check is necessary considering the `spl <= NEAR_CLIPPING_PLANE` above
// TODO: add an ability to positiion the sprites vertically
// TODO: make the scale of the sprite a parameter configurable per sprite
const spriteScale = 0.5;
const spriteSize = display.backImageData.height/pdist*spriteScale;
const maxSpriteSize = display.backImageData.height/pdist;
const spriteSize = maxSpriteSize*sprite.scale;
const x1 = Math.floor(cx - spriteSize*0.5);
const x2 = Math.floor(x1 + spriteSize - 1);
const bx1 = Math.max(0, x1);
const bx2 = Math.min(display.backImageData.width-1, x2);
const y1 = Math.floor(cy - spriteSize*0.5);
const y1 = Math.floor(cy + maxSpriteSize*0.5 - maxSpriteSize*sprite.z);
const y2 = Math.floor(y1 + spriteSize - 1);
const by1 = Math.max(0, y1);
const by2 = Math.min(display.backImageData.height-1, y2);
Expand Down
10 changes: 7 additions & 3 deletions index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,24 @@ async function loadImageData(url: string): Promise<ImageData> {

let game = await import("./game.js");
const scene = game.createScene([
[ null, null, wall, wall, null, null, null],
[ null, null, wall, wall, wall, null, null],
[ null, null, null, null, null, null, null],
[ wall, null, null, null, null, null, null],
[ wall, null, null, null, null, null, null],
[ null],
[ wall],
[ null, null, null, null, null, null, null],
[ null, null, null, null, null, null, null],
]);

const KEY_SCALE = 0.4;
const KEY_Z = KEY_SCALE;
const sprites = [
{
imageData: key,
position: new game.Vector2(1.5, 1.5),
}
z: KEY_Z,
scale: KEY_SCALE
},
];

const player = game.createPlayer(
Expand Down
Binary file modified screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d5ede56

Please sign in to comment.