silphius/resources/tomato-plant/interact.js
2024-09-28 10:26:20 -05:00

126 lines
2.4 KiB
JavaScript

const {Interactive, Position, Plant, Sprite} = subject;
Interactive.interacting = false;
const promises = [];
for (let i = 0; i < 10; ++i) {
promises.push(ecs.create({
Collider: {
bodies: [
{
points: [
{x: -4, y: -4},
{x: 3, y: -4},
{x: 3, y: 3},
{x: -4, y: 3},
],
},
],
collisionStartScript: [
'if (other.Inventory) {',
' other.Inventory.give({',
' qty: 1,',
" source: '/resources/tomato/tomato.json',",
' })',
' ecs.destroy(entity.id)',
' return undefined;',
'}',
].join('\n'),
},
Forces: {},
Magnetic: {},
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 ids = await Promise.all(promises);
for (const id of ids) {
const tomato = ecs.get(id);
const {x, y} = Math.normalizeVector({
x: (Math.random() * 2) - 1,
y: (Math.random() * 2) - 1,
});
const d = delta(
tomato.Position,
{
y: {
duration: 0.5,
delta: 0,
},
},
)
tomato.Ticking.add(
Promise.Ticker.all([
d.promise,
lfo(
d.deltas.y,
{
delta: {
count: 1,
frequency: 0.5,
magnitude: 64,
median: 0,
offset: -0.5,
},
},
).promise,
lfo(
tomato.Sprite,
{
scaleX: {
count: 1,
frequency: 0.5,
magnitude: 0.333,
median: 0.333,
elapsed: 0.25,
offset: -0.5,
},
scaleY: {
count: 1,
frequency: 0.5,
magnitude: 0.333,
median: 0.333,
elapsed: 0.25,
offset: -0.5,
},
},
).promise,
delta(
tomato.Position,
{
x: {
duration: 0.5,
delta: (12 * x) + Math.random() * 8,
},
},
).promise,
delta(
tomato.Position,
{
y: {
duration: 0.5,
delta: (12 * y) + Math.random() * 8,
},
},
).promise,
]),
);
}
Plant.stage = 4;
Sprite.animation = ['stage', Plant.stage].join('/')