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

View File

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

View File

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

View File

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

View File

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