silphius/resources/tomato-seeds/start.js

94 lines
2.2 KiB
JavaScript
Raw Normal View History

2024-06-28 14:36:49 -05:00
const {Direction, Position, Wielder} = wielder
2024-07-24 09:28:35 -05:00
const projected = Wielder.activeItem()?.project(Position.tile, Direction.quantize(4))
2024-06-28 14:36:49 -05:00
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 = {
2024-07-02 16:16:39 -05:00
Collider: {
bodies: [
2024-07-03 16:13:14 -05:00
{
points: [
{x: -8, y: -8},
{x: 7, y: -8},
{x: -8, y: 7},
{x: 7, y: 7},
],
},
2024-07-02 16:16:39 -05:00
],
},
2024-07-01 18:12:53 -05:00
Interactive: {
2024-09-17 01:25:39 -05:00
interactScript: '/resources/tomato-plant/interact.js',
2024-07-01 18:12:53 -05:00
},
2024-06-28 14:36:49 -05:00
Plant: {
2024-09-17 01:25:39 -05:00
growScript: '/resources/tomato-plant/grow.js',
mayGrowScript: '/resources/tomato-plant/may-grow.js',
2024-07-20 07:20:32 -05:00
stages: [0.5, 0.5, 0.5, 0.5, 0.5],
2024-06-28 14:36:49 -05:00
},
Sprite: {
2024-07-03 21:56:55 -05:00
anchorY: 0.75,
2024-06-28 14:36:49 -05:00
animation: 'stage/0',
2024-09-17 01:25:39 -05:00
source: '/resources/tomato-plant/tomato-plant.json',
2024-06-28 14:36:49 -05:00
},
Ticking: {},
VisibleAabb: {},
};
2024-07-30 09:56:53 -05:00
Emitter.emit({
count: 25,
2024-06-28 14:36:49 -05:00
frequency: 0.01,
2024-07-30 09:56:53 -05:00
shape: {
type: 'filledRect',
payload: {width: 16, height: 16},
2024-06-28 14:36:49 -05:00
},
2024-07-30 09:56:53 -05:00
entity: {
Forces: {forceY: -100},
Position: {
x: Position.x,
y: Position.y,
},
Sprite: {
scaleX: 0.125,
scaleY: 0.125,
tint: 0x221100,
},
Ttl: {ttl: 0.25},
}
});
2024-06-28 14:36:49 -05:00
2024-09-17 01:25:39 -05:00
Sound.play('/resources/sow.wav');
2024-06-28 14:36:49 -05:00
Sprite.animation = ['moving', direction].join(':');
2024-07-24 09:28:35 -05:00
const directionMap = {0: 'right', 1: 'down', 2: 'left', 3: 'up'};
2024-06-28 14:36:49 -05:00
for (let i = 0; i < 6; ++i) {
2024-07-24 09:28:35 -05:00
Direction.direction = Math.HALF_PI * Math.floor(Math.random() * 4);
Sprite.animation = ['moving', directionMap[Direction.quantize(4)]].join(':');
2024-06-28 14:36:49 -05:00
await wait(0.125);
}
Sprite.animation = ['idle', direction].join(':');
const promises = [];
2024-07-12 17:41:55 -05:00
for (const {x, y} of projected) {
2024-06-28 14:36:49 -05:00
promises.push(ecs.create({
...plant,
Plant: {
...plant.Plant,
growthFactor: Math.floor(Math.random() * 256),
},
Position: {
2024-07-12 17:41:55 -05:00
x: x * layer.tileSize.x + (0.5 * layer.tileSize.x),
y: y * layer.tileSize.y + (0.5 * layer.tileSize.y),
2024-06-28 14:36:49 -05:00
},
}))
}
await Promise.all(promises)
Controlled.locked = 0;
}