gpt-4-0314· openai
Suzhen/CodeChat
async attackLoop() { let mob; const expMaps = this.storage.expMapy.split(','); while…
- User
async attackLoop() { let mob; const expMaps = this.storage.expMapy.split(',');
while (this.storage.ExpButton) { mob = this.closestMob({ min: this.storage.minlvl, max: this.storage.maxlvl }); if (!mob) { const currentMapIndex = expMaps.indexOf(map.id.toString()), nextMap = currentMapIndex !== -1 ? expMaps[currentMapIndex + 1] : (await this.getRoute(expMaps[0])).split(',')[1]; await this.goToMap(Number(nextMap)); } else { if (this.getDistanceToNpc(mob.x, mob.y) === 1) { window._g(`fight&a=attack&ff=1&id=-${mob.id}`); await new Promise(resolve => setTimeout(resolve, 500)); } else { await this.goTo(mob.x, mob.y); } } } }Jak sprawić by w przypadku warunku !mob i przypadku gdy currentMapIndex !== -1 postać ciągle chodziła po mapach ale w taki sposób 1,2,3,4,5 a nie tak żebym musiał tworzyć pętle typu 1,2,3,4,5,4,3,2,1
- Assistant
Aby osiągnąć to, co opisałeś, możemy dodać zmienną, która będzie określać kierunek poruszania się po mapach. Kiedy postać dojdzie do końca listy map, zmieniamy kierunek na przeciwny. Oto przykład, jak to zrobić:
async attackLoop() { let mob; const expMaps = this.storage.expMapy.split(','); let mapIndexIncrement = 1; // dodajemy zmienną, która będzie określać kierunek poruszania się po mapach while (this.storage.ExpButton) { mob = this.closestMob({ min: this.storage.minlvl, max: this.storage.maxlvl }); if (!mob) { const currentMapIndex = expMaps.indexOf(map.id.toString()), if (currentMapIndex + mapIndexIncrement >= expMaps.length || currentMapIndex + mapIndexIncrement < 0) { mapIndexIncrement = -mapIndexIncrement; // zmieniamy kierunek na przeciwny, gdy osiągniemy koniec listy map } const nextMap = expMaps[currentMapIndex + mapIndexIncrement]; await this.goToMap(Number(nextMap)); } else { if (this.getDistanceToNpc(mob.x, mob.y) === 1) { window._g(`fight&a=attack&ff=1&id=-${mob.id}`); await new Promise(resolve => setTimeout(resolve, 500)); } else { await this.goTo(mob.x, mob.y); } } } }