refactor: scale and anchor

This commit is contained in:
cha0s 2024-07-03 21:56:55 -05:00
parent 64cb88ae2f
commit 4a7006a48d
4 changed files with 20 additions and 7 deletions

View File

@ -45,7 +45,8 @@ export default async function createHomestead(Ecs) {
Ecs: {}, Ecs: {},
Position: {x: 100, y: 100}, Position: {x: 100, y: 100},
Sprite: { Sprite: {
anchor: {x: 0.5, y: 0.8}, anchorX: 0.5,
anchorY: 0.8,
source: '/assets/shit-shack/shit-shack.json', source: '/assets/shit-shack/shit-shack.json',
}, },
VisibleAabb: {}, VisibleAabb: {},

View File

@ -45,7 +45,8 @@ export default async function createPlayer(id) {
Speed: {speed: 100}, Speed: {speed: 100},
Sound: {}, Sound: {},
Sprite: { Sprite: {
anchor: {x: 0.5, y: 0.8}, anchorX: 0.5,
anchorY: 0.8,
animation: 'moving:down', animation: 'moving:down',
frame: 0, frame: 0,
frames: 8, frames: 8,

View File

@ -1,18 +1,28 @@
import Component from '@/ecs/component.js'; import Component from '@/ecs/component.js';
import vector2d from "./helpers/vector-2d";
export default class Sprite extends Component { export default class Sprite extends Component {
instanceFromSchema() {
return class SpriteInstance extends super.instanceFromSchema() {
get anchor() {
return {x: this.anchorX, y: this.anchorY};
}
get scale() {
return {x: this.scaleX, y: this.scaleY};
}
};
}
async load(instance) { async load(instance) {
instance.$$sourceJson = await this.ecs.readJson(instance.source); instance.$$sourceJson = await this.ecs.readJson(instance.source);
} }
static properties = { static properties = {
anchor: vector2d('float32', {x: 0.5, y: 0.5}), anchorX: {defaultValue: 0.5, type: 'float32'},
anchorY: {defaultValue: 0.5, type: 'float32'},
animation: {type: 'string'}, animation: {type: 'string'},
elapsed: {type: 'float32'}, elapsed: {type: 'float32'},
frame: {type: 'uint16'}, frame: {type: 'uint16'},
frames: {type: 'uint16'}, frames: {type: 'uint16'},
scale: vector2d('float32', {x: 1, y: 1}), scaleX: {defaultValue: 1, type: 'float32'},
scaleY: {defaultValue: 1, type: 'float32'},
source: {type: 'string'}, source: {type: 'string'},
speed: {type: 'float32'}, speed: {type: 'float32'},
}; };

View File

@ -30,7 +30,8 @@ if (projected?.length > 0) {
stages: Array(5).fill(5), stages: Array(5).fill(5),
}, },
Sprite: { Sprite: {
anchor: {x: 0.5, y: 0.75}, anchorX: 0.5,
anchorY: 0.75,
animation: 'stage/0', animation: 'stage/0',
frame: 0, frame: 0,
frames: 1, frames: 1,