silphius/app/ecs/components/sprite.js

91 lines
2.4 KiB
JavaScript
Raw Normal View History

2024-06-26 21:08:09 -05:00
import Component from '@/ecs/component.js';
export default class Sprite extends Component {
2024-07-03 21:56:55 -05:00
instanceFromSchema() {
return class SpriteInstance extends super.instanceFromSchema() {
2024-07-21 11:14:51 -05:00
$$sourceJson = {};
2024-07-03 21:56:55 -05:00
get anchor() {
return {x: this.anchorX, y: this.anchorY};
}
2024-07-21 11:14:51 -05:00
get animation() {
return super.animation;
}
set animation(animation) {
2024-07-25 10:48:22 -05:00
if (this.$$animation === animation) {
return;
}
2024-07-21 11:14:51 -05:00
super.animation = animation;
// eslint-disable-next-line no-self-assign
this.frame = this.frame;
}
get frame() {
return super.frame;
}
set frame(frame) {
super.frame = this.frames ? frame % this.frames : 0;
}
get frames() {
2024-07-22 01:25:05 -05:00
if (
!this.animation
|| !this.$$sourceJson.animations
|| !(this.animation in this.$$sourceJson.animations)
) {
return 0;
}
return this.$$sourceJson.animations[this.animation].length;
2024-07-21 11:14:51 -05:00
}
2024-07-25 10:48:22 -05:00
hasAnimation(animation) {
if (
!this.$$sourceJson.animations
|| !(animation in this.$$sourceJson.animations)
) {
return false;
}
return true;
}
get rotates() {
if (!this.$$sourceJson.meta) {
return false;
}
return 'rotation' in this.$$sourceJson.meta;
}
get rotation() {
if (!this.$$sourceJson.meta) {
return 0;
}
return this.$$sourceJson.meta.rotation;
}
2024-07-03 21:56:55 -05:00
get scale() {
return {x: this.scaleX, y: this.scaleY};
}
2024-07-21 07:22:48 -05:00
toNet() {
// eslint-disable-next-line no-unused-vars
const {elapsed, ...rest} = super.toNet();
return rest;
}
2024-07-03 21:56:55 -05:00
};
}
2024-07-02 12:00:12 -05:00
async load(instance) {
instance.$$sourceJson = await this.ecs.readJson(instance.source);
}
2024-07-21 07:22:48 -05:00
markChange(entityId, key, value) {
if ('elapsed' === key) {
return;
}
super.markChange(entityId, key, value);
}
2024-06-26 21:08:09 -05:00
static properties = {
2024-07-25 10:48:22 -05:00
alpha: {defaultValue: 1, type: 'float32'},
2024-07-03 21:56:55 -05:00
anchorX: {defaultValue: 0.5, type: 'float32'},
anchorY: {defaultValue: 0.5, type: 'float32'},
2024-06-26 21:08:09 -05:00
animation: {type: 'string'},
elapsed: {type: 'float32'},
frame: {type: 'uint16'},
2024-07-21 11:14:51 -05:00
isAnimating: {defaultValue: 1, type: 'uint8'},
2024-07-03 21:56:55 -05:00
scaleX: {defaultValue: 1, type: 'float32'},
scaleY: {defaultValue: 1, type: 'float32'},
2024-06-26 21:08:09 -05:00
source: {type: 'string'},
speed: {type: 'float32'},
};
}