Skip to content

Commit

Permalink
collision detection
Browse files Browse the repository at this point in the history
  • Loading branch information
mzelder committed Jul 25, 2024
1 parent f8cac9a commit df4039e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</head>
<body>
<div id="fan"></div>
<img id="cursor" src="cursor.png">
<img id="cursor" src="trash.png">
<img class="lamp" src="lamp.png">
<img class="plant" src="plant.png">
<div class="lamp-hitbox"></div>
Expand Down
27 changes: 26 additions & 1 deletion script.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ document.addEventListener("DOMContentLoaded", () => {
const windForce = 0.5;
let velocityY = 0;
let velocityX = 0;
let angle = 0;

let mouseX = 0;
let mouseY = 0;
Expand All @@ -34,7 +35,12 @@ document.addEventListener("DOMContentLoaded", () => {
// Update cursor position and apply gravity every 10 milliseconds
setInterval(() => {
updateCol();

if (checkColission()) {
alert("Game over!");
}

angle += 10;
cursor.style.transform = `rotate(${angle}deg)`

// Apply gravity
velocityY += gravity;
Expand Down Expand Up @@ -122,4 +128,23 @@ document.addEventListener("DOMContentLoaded", () => {
plantHitBox.style.left = plantNewPosition + 60 + 'px';
}
}

function checkColission() {
let cursorRect = cursor.getBoundingClientRect();
let lampRect = lampHitBox.getBoundingClientRect();
let plantRect = plantHitBox.getBoundingClientRect();
// Check for collision with the lamp
let lampCollision = !(cursorRect.right < lampRect.left ||
cursorRect.left > lampRect.right ||
cursorRect.bottom < lampRect.top ||
cursorRect.top > lampRect.bottom);

// Check for collision with the plant
let plantCollision = !(cursorRect.right < plantRect.left ||
cursorRect.left > plantRect.right ||
cursorRect.bottom < plantRect.top ||
cursorRect.top > plantRect.bottom);

return lampCollision || plantCollision;
}
});
3 changes: 1 addition & 2 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ body {
}

#cursor {
width: 16px;
height: 16px;
width: 50px;
position: absolute;
top: 100px;
left: 100px;
Expand Down
Binary file added trash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit df4039e

Please sign in to comment.