silphius/public/assets/tomato-seeds/start.js
2024-06-28 14:40:30 -05:00

138 lines
3.0 KiB
JavaScript

const {Direction, Position, Wielder} = wielder
const projected = Wielder.activeItem()?.project(Position.tile, Direction.direction)
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 = {
Plant: {
growScript: '/assets/tomato-plant/grow.js',
mayGrowScript: '/assets/tomato-plant/may-grow.js',
stages: Array(5).fill(60),
},
Sprite: {
anchor: {x: 0.5, y: 0.75},
animation: 'stage/0',
frame: 0,
frames: 1,
source: '/assets/tomato-plant/tomato-plant.json',
speed: 0,
},
Ticking: {},
VisibleAabb: {},
};
const seedParticles = {
behaviors: [
{
type: 'moveAcceleration',
config: {
accel: {
x: 0,
y: 400,
},
minStart: 0,
maxStart: 0,
rotate: false,
}
},
{
type: 'moveSpeed',
config: {
speed: {
list: [
{
time: 0,
value: 100
},
{
time: 1,
value: 10
}
]
}
}
},
{
type: 'rotation',
config: {
accel: 0,
minSpeed: 0,
maxSpeed: 0,
minStart: 225,
maxStart: 320
}
},
{
type: 'scale',
config: {
scale: {
list: [
{
value: 0.75,
time: 0,
},
{
value: 0.5,
time: 1,
},
]
}
}
},
{
type: 'textureSingle',
config: {
texture: 'tomato-plant/tomato-plant/0',
}
},
],
lifetime: {
min: 0.25,
max: 0.25,
},
frequency: 0.01,
emitterLifetime: 0.75,
pos: {
x: Position.x,
y: Position.y,
},
};
Emitter.emit(seedParticles)
Sound.play('/assets/sow.wav');
Sprite.animation = ['moving', direction].join(':');
for (let i = 0; i < 6; ++i) {
Direction.direction = Math.floor(Math.random() * 4);
Sprite.animation = ['moving', 0 === Direction.direction ? 'up' : (1 === Direction.direction ? 'right' : (2 === Direction.direction ? 'down' : 'left'))].join(':');
await wait(0.125);
}
Sprite.animation = ['idle', direction].join(':');
const promises = [];
for (let i = 0; i < projected.length; ++i) {
promises.push(ecs.create({
...plant,
Plant: {
...plant.Plant,
growthFactor: Math.floor(Math.random() * 256),
},
Position: {
x: projected[i].x * layer.tileSize.x + (0.5 * layer.tileSize.x),
y: projected[i].y * layer.tileSize.y + (0.5 * layer.tileSize.y),
},
}))
}
await Promise.all(promises)
Controlled.locked = 0;
}