Well it works again
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
import {
|
import {
|
||||||
type Entity,
|
|
||||||
entityMap,
|
entityMap,
|
||||||
entityProcessMap,
|
entityProcessMap,
|
||||||
type Process as ProcessEntity,
|
type Process as ProcessEntity,
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
export type SpritesSheet = {
|
import { loadImage } from "./lib/loadImage";
|
||||||
|
|
||||||
|
export type SpriteSheet = {
|
||||||
image: HTMLImageElement;
|
image: HTMLImageElement;
|
||||||
xFrames: number;
|
xFrames: number;
|
||||||
yFrames: number;
|
yFrames: number;
|
||||||
@@ -7,18 +9,23 @@ export type SpritesSheet = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export type SpriteAnimation = {
|
export type SpriteAnimation = {
|
||||||
sheet: SpritesSheet;
|
src: string;
|
||||||
xIndex: number;
|
xIndex: number;
|
||||||
yIndex: number;
|
yIndex: number;
|
||||||
frameTime: number;
|
frameTime: number;
|
||||||
frameDelta: number;
|
frameDelta: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function createSpritesSheet(
|
// It's not possible to crate a structured clone of a HTMLImageElement and it would be a
|
||||||
|
// waste of memory anyways since we use the HTMLImageElement only as reference for draw calls.
|
||||||
|
// Therefore the SpriteSheet will stored by it's image source.
|
||||||
|
export const spriteSheetMap = new Map<string, SpriteSheet>();
|
||||||
|
|
||||||
|
export function createSpriteSheet(
|
||||||
image: HTMLImageElement,
|
image: HTMLImageElement,
|
||||||
xFrames: number,
|
xFrames: number,
|
||||||
yFrames: number,
|
yFrames: number,
|
||||||
): SpritesSheet {
|
): SpriteSheet {
|
||||||
return {
|
return {
|
||||||
image,
|
image,
|
||||||
xFrames,
|
xFrames,
|
||||||
@@ -28,13 +35,26 @@ export function createSpritesSheet(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function useSpriteSheetSrc(
|
||||||
|
src: string,
|
||||||
|
xFrames: number,
|
||||||
|
yFrames: number,
|
||||||
|
): Promise<string> {
|
||||||
|
spriteSheetMap.set(
|
||||||
|
src,
|
||||||
|
createSpriteSheet(await loadImage(src), xFrames, yFrames),
|
||||||
|
);
|
||||||
|
|
||||||
|
return src;
|
||||||
|
}
|
||||||
|
|
||||||
export function createSpriteAnimation(
|
export function createSpriteAnimation(
|
||||||
sheet: SpritesSheet,
|
src: string,
|
||||||
frameTime: number,
|
frameTime: number,
|
||||||
yIndex: number,
|
yIndex: number,
|
||||||
): SpriteAnimation {
|
): SpriteAnimation {
|
||||||
return {
|
return {
|
||||||
sheet,
|
src,
|
||||||
xIndex: 0,
|
xIndex: 0,
|
||||||
yIndex,
|
yIndex,
|
||||||
frameTime,
|
frameTime,
|
||||||
@@ -44,7 +64,7 @@ export function createSpriteAnimation(
|
|||||||
|
|
||||||
export function drawSprite(
|
export function drawSprite(
|
||||||
ctx: CanvasRenderingContext2D,
|
ctx: CanvasRenderingContext2D,
|
||||||
sheet: SpritesSheet,
|
sheet: SpriteSheet,
|
||||||
xIndex: number,
|
xIndex: number,
|
||||||
yIndex: number,
|
yIndex: number,
|
||||||
x: number,
|
x: number,
|
||||||
@@ -72,12 +92,14 @@ export function playAnimation(
|
|||||||
y: number,
|
y: number,
|
||||||
delta: number,
|
delta: number,
|
||||||
): void {
|
): void {
|
||||||
|
const sheet = spriteSheetMap.get(animation.src)!;
|
||||||
|
|
||||||
if ((animation.frameDelta += delta) > animation.frameTime) {
|
if ((animation.frameDelta += delta) > animation.frameTime) {
|
||||||
animation.frameDelta = 0;
|
animation.frameDelta = 0;
|
||||||
if ((animation.xIndex += 1) >= animation.sheet.xFrames) {
|
if ((animation.xIndex += 1) >= sheet.xFrames) {
|
||||||
animation.xIndex = 0;
|
animation.xIndex = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
drawSprite(ctx, animation.sheet, animation.xIndex, animation.yIndex, x, y);
|
drawSprite(ctx, sheet, animation.xIndex, animation.yIndex, x, y);
|
||||||
}
|
}
|
||||||
@@ -2,13 +2,12 @@ import { type Circle, createCircle } from "#/engine/Circle.ts";
|
|||||||
import { moveAndCollide } from "#/engine/Collision.ts";
|
import { moveAndCollide } from "#/engine/Collision.ts";
|
||||||
import { defineEntity } from "#/engine/Entity.ts";
|
import { defineEntity } from "#/engine/Entity.ts";
|
||||||
import { keyboardInput } from "#/engine/Keyboard.ts";
|
import { keyboardInput } from "#/engine/Keyboard.ts";
|
||||||
import { loadImage } from "#/engine/lib/loadImage";
|
|
||||||
import { pointer } from "#/engine/Pointer.ts";
|
import { pointer } from "#/engine/Pointer.ts";
|
||||||
import {
|
import {
|
||||||
createSpriteAnimation,
|
createSpriteAnimation,
|
||||||
createSpritesSheet,
|
|
||||||
type SpriteAnimation,
|
type SpriteAnimation,
|
||||||
} from "#/engine/sprites.ts";
|
useSpriteSheetSrc,
|
||||||
|
} from "#/engine/Sprite.ts";
|
||||||
import { createVector, type Vector } from "#/engine/Vector.ts";
|
import { createVector, type Vector } from "#/engine/Vector.ts";
|
||||||
import charIdle from "#/game/assets/char-idle.png";
|
import charIdle from "#/game/assets/char-idle.png";
|
||||||
|
|
||||||
@@ -20,7 +19,7 @@ export type Hero = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const speed = 0.05;
|
const speed = 0.05;
|
||||||
const idleSheet = createSpritesSheet(await loadImage(charIdle), 2, 4);
|
const idleSheet = await useSpriteSheetSrc(charIdle, 2, 4);
|
||||||
|
|
||||||
const state: Hero = {
|
const state: Hero = {
|
||||||
position: createVector(),
|
position: createVector(),
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
import { defineEntity } from "#/engine/Entity.ts";
|
import { defineEntity } from "#/engine/Entity.ts";
|
||||||
import { loadImage } from "#/engine/lib/loadImage.ts";
|
import { createSpriteAnimation, useSpriteSheetSrc } from "#/engine/Sprite.ts";
|
||||||
import { createSpriteAnimation, createSpritesSheet } from "#/engine/sprites.ts";
|
|
||||||
import portal from "#/game/assets/portal.png";
|
import portal from "#/game/assets/portal.png";
|
||||||
|
|
||||||
const portalSheet = createSpritesSheet(await loadImage(portal), 3, 1);
|
const portalSheet = await useSpriteSheetSrc(portal, 3, 1);
|
||||||
|
|
||||||
defineEntity("portal", {
|
defineEntity("portal", {
|
||||||
animation: createSpriteAnimation(portalSheet, 400, 0),
|
animation: createSpriteAnimation(portalSheet, 400, 0),
|
||||||
|
|||||||
@@ -1,18 +1,17 @@
|
|||||||
import { defineEntity } from "#/engine/Entity.ts";
|
import { defineEntity } from "#/engine/Entity.ts";
|
||||||
import { loadImage } from "#/engine/lib/loadImage.ts";
|
|
||||||
import {
|
import {
|
||||||
createSpriteAnimation,
|
createSpriteAnimation,
|
||||||
createSpritesSheet,
|
|
||||||
type SpriteAnimation,
|
type SpriteAnimation,
|
||||||
} from "#/engine/sprites.ts";
|
useSpriteSheetSrc,
|
||||||
|
} from "#/engine/Sprite.ts";
|
||||||
import { createVector, type Vector } from "#/engine/Vector.ts";
|
import { createVector, type Vector } from "#/engine/Vector.ts";
|
||||||
import { viewport } from "#/engine/Viewport.ts";
|
import { viewport } from "#/engine/Viewport.ts";
|
||||||
import charIdle from "#/game/assets/char-idle.png";
|
import charIdle from "#/game/assets/char-idle.png";
|
||||||
import charWalk from "#/game/assets/char-walk.png";
|
import charWalk from "#/game/assets/char-walk.png";
|
||||||
|
|
||||||
const sprites = {
|
const sprites = {
|
||||||
idle: createSpritesSheet(await loadImage(charIdle), 2, 4),
|
idle: await useSpriteSheetSrc(charIdle, 2, 4),
|
||||||
walk: createSpritesSheet(await loadImage(charWalk), 4, 4),
|
walk: await useSpriteSheetSrc(charWalk, 4, 4),
|
||||||
};
|
};
|
||||||
|
|
||||||
const animations = {
|
const animations = {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { strokeCircle } from "#/engine/Circle.ts";
|
import { strokeCircle } from "#/engine/Circle.ts";
|
||||||
import { pointer } from "#/engine/Pointer.ts";
|
import { pointer } from "#/engine/Pointer.ts";
|
||||||
import { defineScene } from "#/engine/Scene.ts";
|
import { defineScene } from "#/engine/Scene.ts";
|
||||||
|
import { spriteSheetMap } from "#/engine/Sprite.ts";
|
||||||
import type { Hero } from "#/game/entities/hero/entity.ts";
|
import type { Hero } from "#/game/entities/hero/entity.ts";
|
||||||
import data from "./data.json";
|
import data from "./data.json";
|
||||||
|
|
||||||
@@ -11,9 +12,10 @@ linkScenes(["fooScene"] as const);
|
|||||||
preProcess((entityInstanceMap) => {
|
preProcess((entityInstanceMap) => {
|
||||||
const entry = entityInstanceMap.get("hero")!;
|
const entry = entityInstanceMap.get("hero")!;
|
||||||
const instance = entry.instances[0] as Hero;
|
const instance = entry.instances[0] as Hero;
|
||||||
|
const sheet = spriteSheetMap.get(instance.animation.src)!;
|
||||||
|
|
||||||
instance.position.x = data.width - instance.animation.sheet.frameHeight;
|
instance.position.x = data.width - sheet.frameHeight;
|
||||||
instance.position.y = data.height - instance.animation.sheet.frameWidth;
|
instance.position.y = data.height - sheet.frameWidth;
|
||||||
});
|
});
|
||||||
|
|
||||||
process((ctx, _delta) => {
|
process((ctx, _delta) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user