From 35515f53459deb70e1a4de1569529ad89fe1d446 Mon Sep 17 00:00:00 2001 From: cha0s Date: Tue, 25 Jun 2024 06:19:57 -0500 Subject: [PATCH] feat: Position.tile --- app/ecs-components/position.js | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/app/ecs-components/position.js b/app/ecs-components/position.js index a0e637a..c629f5f 100644 --- a/app/ecs-components/position.js +++ b/app/ecs-components/position.js @@ -1,4 +1,29 @@ -export default { - x: {type: 'float32'}, - y: {type: 'float32'}, -}; +import Schema from '@/ecs/schema.js'; + +export default function(Component) { + return class Wielder extends Component { + instanceFromSchema() { + const Instance = super.instanceFromSchema(); + const Component = this; + Object.defineProperty(Instance.prototype, 'tile', { + get: function () { + const {TileLayers} = Component.ecs.get(1); + const {Position: {x, y}} = Component.ecs.get(this.entity); + const {tileSize} = TileLayers.layers[0]; + return { + x: (x - (x % tileSize.x)) / tileSize.x, + y: (y - (y % tileSize.y)) / tileSize.y, + } + }, + }); + return Instance; + } + static schema = new Schema({ + type: 'object', + properties: { + x: {type: 'float32'}, + y: {type: 'float32'}, + }, + }); + } +}