silphius/public/assets/tomato-seeds/start.js
2024-07-30 09:56:53 -05:00

94 lines
2.2 KiB
JavaScript

const {Direction, Position, Wielder} = wielder
const projected = Wielder.activeItem()?.project(Position.tile, Direction.quantize(4))
if (projected?.length > 0) {
const {Controlled, Emitter, Sound, Sprite} = wielder
const {TileLayers} = ecs.get(1)
const layer = TileLayers.layer(0)
Controlled.locked = 1;
const [, direction] = Sprite.animation.split(':')
const plant = {
Collider: {
bodies: [
{
points: [
{x: -8, y: -8},
{x: 7, y: -8},
{x: -8, y: 7},
{x: 7, y: 7},
],
},
],
},
Interactive: {
interactScript: '/assets/tomato-plant/interact.js',
},
Plant: {
growScript: '/assets/tomato-plant/grow.js',
mayGrowScript: '/assets/tomato-plant/may-grow.js',
stages: [0.5, 0.5, 0.5, 0.5, 0.5],
},
Sprite: {
anchorY: 0.75,
animation: 'stage/0',
source: '/assets/tomato-plant/tomato-plant.json',
},
Ticking: {},
VisibleAabb: {},
};
Emitter.emit({
count: 25,
frequency: 0.01,
shape: {
type: 'filledRect',
payload: {width: 16, height: 16},
},
entity: {
Forces: {forceY: -100},
Position: {
x: Position.x,
y: Position.y,
},
Sprite: {
scaleX: 0.125,
scaleY: 0.125,
tint: 0x221100,
},
Ttl: {ttl: 0.25},
}
});
Sound.play('/assets/sow.wav');
Sprite.animation = ['moving', direction].join(':');
const directionMap = {0: 'right', 1: 'down', 2: 'left', 3: 'up'};
for (let i = 0; i < 6; ++i) {
Direction.direction = Math.HALF_PI * Math.floor(Math.random() * 4);
Sprite.animation = ['moving', directionMap[Direction.quantize(4)]].join(':');
await wait(0.125);
}
Sprite.animation = ['idle', direction].join(':');
const promises = [];
for (const {x, y} of projected) {
promises.push(ecs.create({
...plant,
Plant: {
...plant.Plant,
growthFactor: Math.floor(Math.random() * 256),
},
Position: {
x: x * layer.tileSize.x + (0.5 * layer.tileSize.x),
y: y * layer.tileSize.y + (0.5 * layer.tileSize.y),
},
}))
}
await Promise.all(promises)
Controlled.locked = 0;
}