-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac93079
commit 7b208a8
Showing
2 changed files
with
52 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,51 @@ | ||
document.addEventListener('DOMContentLoaded', () => { | ||
const bot1 = document.getElementById('bot1'); | ||
const mover = new MoverTS(bot1); | ||
const bot1 = document.getElementById('bot1'); | ||
const mover = new MoverTS(bot1); | ||
|
||
//Calcola la distanza tra i 2 elementi in argomento | ||
function getDistance(rect1, rect2) { | ||
const dx = rect1.x - rect2.x; | ||
const dy = rect1.y - rect2.y; | ||
return Math.sqrt(dx * dx + dy * dy); | ||
} | ||
|
||
function RadarMode() { //Cerca in un raggio di 100px gli id: player || PwUP | ||
const radius = 100; // Raggio di ricerca in pixel | ||
const botRect = bot1.getBoundingClientRect(); | ||
|
||
const player = document.getElementById('player'); | ||
const PwUP = document.getElementById('PwUP'); | ||
|
||
let foundElement = null; | ||
|
||
mover.glideAtIdElement('', 5) | ||
// Verifica se l'elemento player è entro il raggio | ||
if (player) { | ||
const playerRect = player.getBoundingClientRect(); | ||
const distanceToPlayer = getDistance(botRect, playerRect); | ||
if (distanceToPlayer <= radius) { | ||
foundElement = player; | ||
} | ||
} | ||
|
||
// Verifica se l'elemento PwUP è entro il raggio | ||
if (PwUP && !foundElement) { // Se non ha già trovato il player | ||
const pwupRect = PwUP.getBoundingClientRect(); | ||
const distanceToPwUP = getDistance(botRect, pwupRect); | ||
if (distanceToPwUP <= radius) { | ||
foundElement = PwUP; | ||
} | ||
} | ||
|
||
if (foundElement) { | ||
console.info("Elemento più vicino trovato nel raggio: "+radius+"): ", foundElement.id); | ||
} else { | ||
console.warn("RadarMode(): Nessun elemento nel raggio di: "+radius+"px"); | ||
mover.glideAt(mover.getRandomX, mover.getRandomY, 3); // Va in una posizione a caso | ||
RadarMode(); | ||
} | ||
} | ||
|
||
document.addEventListener('DOMContentLoaded', () => { | ||
|
||
// Si sposta verso la cassa più vicina | ||
mover.glideAtIdElement('PwUP', 3) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters