refactor: for...of

This commit is contained in:
cha0s 2024-07-12 17:41:55 -05:00
parent 554ae074c5
commit 53f4d35717
7 changed files with 39 additions and 44 deletions

View File

@ -3,12 +3,12 @@ const layer1 = ecs.get(1).TileLayers.layer(1)
const filtered = []
for (let i = 0; i < projected.length; ++i) {
for (const position of projected) {
if (
[1, 2, 3, 4, 6].includes(layer0.tile(projected[i]))
&& ![7].includes(layer1.tile(projected[i]))
[1, 2, 3, 4, 6].includes(layer0.tile(position))
&& ![7].includes(layer1.tile(position))
) {
filtered.push(projected[i])
filtered.push(position)
}
}

View File

@ -88,7 +88,7 @@ if (projected?.length > 0) {
for (let i = 0; i < 2; ++i) {
Sound.play('/assets/hoe/dig.wav');
for (let i = 0; i < projected.length; ++i) {
for (const {x, y} of projected) {
Emitter.emit({
...dirtParticles,
behaviors: [
@ -98,8 +98,8 @@ if (projected?.length > 0) {
config: {
type: 'rect',
data: {
x: projected[i].x * layer.tileSize.x,
y: projected[i].y * layer.tileSize.y,
x: x * layer.tileSize.x,
y: y * layer.tileSize.y,
w: layer.tileSize.x,
h: layer.tileSize.y,
}
@ -114,8 +114,8 @@ if (projected?.length > 0) {
await wait(0.1)
}
for (let i = 0; i < projected.length; ++i) {
TileLayers.layer(1).stamp(projected[i], [[7]])
for (const position of projected) {
TileLayers.layer(1).stamp(position, [[7]])
}
Controlled.locked = 0;

View File

@ -1,19 +1,16 @@
for (let i = 0; i < intersections.length; ++i) {
if (intersections[i][0].tags) {
if (intersections[i][0].tags.includes('door')) {
if (other.Player) {
ecs.switchEcs(
other,
entity.Ecs.path,
{
Position: {
x: 72,
y: 304,
},
for (const [{tags}] of intersections) {
if (tags && tags.includes('door')) {
if (other.Player) {
ecs.switchEcs(
other,
entity.Ecs.path,
{
Position: {
x: 72,
y: 304,
},
);
}
},
);
}
}
}

View File

@ -2,9 +2,9 @@ const layer = ecs.get(1).TileLayers.layer(1)
const filtered = []
for (let i = 0; i < projected.length; ++i) {
const x0 = projected[i].x * layer.tileSize.x;
const y0 = projected[i].y * layer.tileSize.y;
for (const position of projected) {
const x0 = position.x * layer.tileSize.x;
const y0 = position.y * layer.tileSize.y;
const entities = Array.from(ecs.system('Colliders').within({
x0,
x1: x0 + layer.tileSize.x - 1,
@ -12,15 +12,14 @@ for (let i = 0; i < projected.length; ++i) {
y1: y0 + layer.tileSize.y - 1,
}));
let hasPlant = false;
for (let j = 0; j < entities.length; ++j) {
if (entities[j].Plant) {
for (const {Plant} of entities) {
if (Plant) {
hasPlant = true
}
}
if (!hasPlant) {
const tile = layer.tile(projected[i])
if ([7].includes(tile)) {
filtered.push(projected[i])
if ([7].includes(layer.tile(position))) {
filtered.push(position)
}
}
}

View File

@ -129,7 +129,7 @@ if (projected?.length > 0) {
Sprite.animation = ['idle', direction].join(':');
const promises = [];
for (let i = 0; i < projected.length; ++i) {
for (const {x, y} of projected) {
promises.push(ecs.create({
...plant,
Plant: {
@ -137,8 +137,8 @@ if (projected?.length > 0) {
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),
x: x * layer.tileSize.x + (0.5 * layer.tileSize.x),
y: y * layer.tileSize.y + (0.5 * layer.tileSize.y),
},
}))
}

View File

@ -2,10 +2,9 @@ const layer = ecs.get(1).TileLayers.layer(1)
const filtered = []
for (let i = 0; i < projected.length; ++i) {
const tile = layer.tile(projected[i])
if ([7].includes(tile)) {
filtered.push(projected[i])
for (const position of projected) {
if ([7].includes(layer.tile(position))) {
filtered.push(position)
}
}

View File

@ -80,7 +80,7 @@ if (projected?.length > 0) {
Sound.play('/assets/watering-can/water.wav');
for (let i = 0; i < 2; ++i) {
for (let i = 0; i < projected.length; ++i) {
for (const {x, y} of projected) {
Emitter.emit({
...waterParticles,
behaviors: [
@ -90,8 +90,8 @@ if (projected?.length > 0) {
config: {
type: 'rect',
data: {
x: projected[i].x * layer.tileSize.x,
y: projected[i].y * layer.tileSize.y - (layer.tileSize.y * 0.5),
x: x * layer.tileSize.x,
y: y * layer.tileSize.y - (layer.tileSize.y * 0.5),
w: layer.tileSize.x,
h: layer.tileSize.y,
}
@ -103,8 +103,8 @@ if (projected?.length > 0) {
await wait(0.25);
}
for (let i = 0; i < projected.length; ++i) {
const tileIndex = layer.area.x * projected[i].y + projected[i].x
for (const {x, y} of projected) {
const tileIndex = layer.area.x * y + x
let w;
if (Water.water[tileIndex]) {
w = Water.water[tileIndex]