silphius/resources/tomato-plant/interact.js

96 lines
2.2 KiB
JavaScript
Raw Normal View History

2024-10-17 23:35:34 -05:00
import delta from '@/util/delta.js';
import lfo from '@/util/lfo.js';
import * as Math from '@/util/math.js';
import Ticker from '@/util/ticker.js';
2024-09-28 10:26:20 -05:00
2024-10-17 23:35:34 -05:00
export default function*({ecs, subject}) {
const {Interactive, Position, Plant, Sprite} = subject;
Interactive.interacting = false;
2024-07-02 10:28:48 -05:00
2024-10-17 23:35:34 -05:00
const specs = [];
2024-10-21 02:41:37 -05:00
const json = ecs.readJson('/resources/tomato/tomato.entity.json');
2024-10-17 23:35:34 -05:00
for (let i = 0; i < 10; ++i) {
specs.push({
2024-10-21 02:41:37 -05:00
...json,
2024-10-17 23:35:34 -05:00
Position: {x: Position.x, y: Position.y},
});
}
const tomatoes = Array.from(ecs.createMany(specs)).map((entityId) => ecs.get(entityId));
2024-07-04 09:10:49 -05:00
2024-10-17 23:35:34 -05:00
// const tickers = [];
for (const tomato of tomatoes) {
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,
2024-07-04 09:10:49 -05:00
},
2024-10-17 23:35:34 -05:00
},
)
tomato.Ticking.add(
Ticker.all([
d.ticker,
lfo(
d.deltas.y,
{
delta: {
count: 1,
frequency: 0.5,
magnitude: 64,
median: 0,
offset: -0.5,
},
2024-07-22 01:31:52 -05:00
},
2024-10-17 23:35:34 -05:00
).ticker,
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,
},
2024-07-22 01:31:52 -05:00
},
2024-10-17 23:35:34 -05:00
).ticker,
delta(
tomato.Position,
{
x: {
duration: 0.5,
delta: (12 * x) + Math.random() * 8,
},
2024-07-22 01:31:52 -05:00
},
2024-10-17 23:35:34 -05:00
).ticker,
delta(
tomato.Position,
{
y: {
duration: 0.5,
delta: (12 * y) + Math.random() * 8,
},
2024-07-22 01:31:52 -05:00
},
2024-10-17 23:35:34 -05:00
).ticker,
]),
);
}
Plant.stage = 4;
Sprite.animation = ['stage', Plant.stage].join('/')
2024-07-04 09:10:49 -05:00
2024-10-17 23:35:34 -05:00
}