refactor: item pickup

This commit is contained in:
cha0s 2024-10-21 02:37:27 -05:00
parent 739a3e4f95
commit 9b9b06f1dc
8 changed files with 92 additions and 49 deletions

View File

@ -0,0 +1,3 @@
import Component from '@/ecs/component.js';
export default class Grabber extends Component {}

View File

@ -0,0 +1,21 @@
import Component from '@/ecs/component.js';
export default class ItemStack extends Component {
instanceFromSchema() {
const {ecs} = this;
return class ItemStackInstance extends super.instanceFromSchema() {
pickup(other) {
const {Collider} = ecs.get(this.entity);
if (Collider) {
Collider.isColliding = 0;
}
other.Inventory.give(this.toJSON());
ecs.destroy(this.entity);
}
};
}
static properties = {
qty: {type: 'uint32'},
source: {type: 'string'},
};
}

View File

@ -0,0 +1,22 @@
import {System} from '@/ecs/index.js';
export default class InventoryCloser extends System {
static queries() {
return {
default: ['Collider', 'ItemStack'],
};
}
tick() {
for (const {Collider, ItemStack} of this.select('default')) {
for (const [other] of Collider.$$intersections) {
const otherEntity = this.ecs.get(other.entity);
if (otherEntity.Grabber && otherEntity.Inventory) {
ItemStack.pickup(otherEntity);
}
}
}
}
}

View File

@ -18,6 +18,7 @@ export default function createEcs(Ecs) {
'MaintainColliderHash',
'Colliders',
'ControlDirection',
'PickupItems',
'SpriteDirection',
'RunAnimations',
'RunTickingPromises',

View File

@ -148,35 +148,11 @@ function createChest() {
function createTomato() {
return {
Collider: {
bodies: [
{
group: -2,
points: [
{x: -4, y: -4},
{x: 3, y: -4},
{x: 3, y: 3},
{x: -4, y: 3},
],
},
],
collisionStartScript: '/resources/tomato/collision-start.js',
},
Forces: {},
Magnetic: {},
$$extends: '/resources/tomato/tomato.entity.json',
Position: {
x: 168 + Math.random() * 30,
y: 448 + Math.random() * 30,
},
Sprite: {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.333,
scaleY: 0.333,
source: '/resources/tomato/tomato.sprite.json',
},
Ticking: {},
VisibleAabb: {},
};
}

View File

@ -53,6 +53,7 @@ export default function createPlayer(id) {
},
},
},
Grabber: {},
Health: {health: 100},
Light: {brightness: 0},
// Magnet: {strength: 24},

View File

@ -10,31 +10,8 @@ export default function*({ecs, subject}) {
const specs = [];
for (let i = 0; i < 10; ++i) {
specs.push({
Collider: {
bodies: [
{
points: [
{x: -4, y: -4},
{x: 3, y: -4},
{x: 3, y: 3},
{x: -4, y: 3},
],
},
],
collisionStartScript: '/resources/tomato/collision-start.js',
},
Forces: {},
Magnetic: {},
...ecs.readJson('/resources/tomato/tomato.entity.json'),
Position: {x: Position.x, y: Position.y},
Sprite: {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.333,
scaleY: 0.333,
source: '/resources/tomato/tomato.sprite.json',
},
Ticking: {},
VisibleAabb: {},
});
}
const tomatoes = Array.from(ecs.createMany(specs)).map((entityId) => ecs.get(entityId));

View File

@ -0,0 +1,42 @@
{
"Collider": {
"bodies": [
{
"group": -2,
"points": [
{
"x": -4,
"y": -4
},
{
"x": 3,
"y": -4
},
{
"x": 3,
"y": 3
},
{
"x": -4,
"y": 3
}
]
}
]
},
"Forces": {},
"ItemStack": {
"qty": 1,
"source": "/resources/tomato/tomato.item.json"
},
"Magnetic": {},
"Sprite": {
"anchorX": 0.5,
"anchorY": 0.5,
"scaleX": 0.333,
"scaleY": 0.333,
"source": "/resources/tomato/tomato.sprite.json"
},
"Ticking": {},
"VisibleAabb": {}
}