refactor: item pickup
This commit is contained in:
parent
739a3e4f95
commit
9b9b06f1dc
3
app/ecs/components/grabber.js
Normal file
3
app/ecs/components/grabber.js
Normal file
|
@ -0,0 +1,3 @@
|
|||
import Component from '@/ecs/component.js';
|
||||
|
||||
export default class Grabber extends Component {}
|
21
app/ecs/components/item-stack.js
Normal file
21
app/ecs/components/item-stack.js
Normal 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'},
|
||||
};
|
||||
}
|
22
app/ecs/systems/pickup-items.js
Normal file
22
app/ecs/systems/pickup-items.js
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -18,6 +18,7 @@ export default function createEcs(Ecs) {
|
|||
'MaintainColliderHash',
|
||||
'Colliders',
|
||||
'ControlDirection',
|
||||
'PickupItems',
|
||||
'SpriteDirection',
|
||||
'RunAnimations',
|
||||
'RunTickingPromises',
|
||||
|
|
|
@ -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: {},
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -53,6 +53,7 @@ export default function createPlayer(id) {
|
|||
},
|
||||
},
|
||||
},
|
||||
Grabber: {},
|
||||
Health: {health: 100},
|
||||
Light: {brightness: 0},
|
||||
// Magnet: {strength: 24},
|
||||
|
|
|
@ -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));
|
||||
|
|
42
resources/tomato/tomato.entity.json
Normal file
42
resources/tomato/tomato.entity.json
Normal 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": {}
|
||||
}
|
Loading…
Reference in New Issue
Block a user