Skip to content

Commit 92a46ae

Browse files
committed
Collision Detection
1 parent 2113ded commit 92a46ae

1 file changed

Lines changed: 88 additions & 45 deletions

File tree

src/scenes/mainGame.js

Lines changed: 88 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ export default class MainGame extends Phaser.Scene{
6464
let velocityX = Phaser.Math.RND.pick([species.speedRange[0], species.speedRange[1]]);
6565
if (velocityX === 0) velocityX = 100; // ensure nonzero
6666

67-
6867
// Create fish
69-
const f = this.physics.add.image(startX, startY, 'fish').setDisplaySize(100, 50);
68+
const f = this.physics.add.sprite(startX, startY, 'fish').setDisplaySize(100, 50);
69+
f.fishName = species.type;
7070
f.body.allowGravity = false;
7171
f.setBounce(1, 1);
7272
f.setCollideWorldBounds(true);
@@ -91,61 +91,104 @@ export default class MainGame extends Phaser.Scene{
9191
// Keyboard Input
9292
this.cursor = this.input.keyboard.createCursorKeys();
9393

94-
// Define functions used in the written coding area---------------------
94+
// Define functions used in the C4C written coding area---------------------
9595

9696
// addBait(bait type)
9797

9898
// cast(length)
9999
C4C.Interpreter.define('cast', (length) => {
100-
if(this.canCast){
100+
if (this.canCast) {
101101
if (length === undefined) {
102102
length = 100;
103-
} else if (length > 500){
103+
} else if (length > 500) {
104104
length = 500;
105105
}
106-
// Create hook
107-
if (this.rightFacing){
108-
this.hook = this.physics.add.sprite(this.player.x + 90, this.player.y - 60, 'hook').setDisplaySize(30,30);
106+
107+
// Create hook sprite
108+
if (this.rightFacing) {
109+
this.hook = this.physics.add.sprite(this.player.x + 90, this.player.y - 60, 'hook').setDisplaySize(30, 30);
109110
} else {
110-
this.hook = this.physics.add.sprite(this.player.x - 90, this.player.y - 60, 'hook').setDisplaySize(30,30);
111+
this.hook = this.physics.add.sprite(this.player.x - 90, this.player.y - 60, 'hook').setDisplaySize(30, 30);
111112
}
112-
// Stop/freeze player movements and removes ability to spam cast
113-
this.moveFreely = false;
114-
this.canCast = false;
115-
116-
// Move hook down according to length arg
117-
this.tweens.add({
118-
targets: this.hook, // The sprite you want to move
119-
y: length + 100, // The target Y coordinate
120-
duration: 1000, // Duration of the tween in milliseconds
121-
ease: 'Power2', // Easing function for smoother animation (optional)
122-
onComplete: () => {
123-
// Code to execute when the sprite reaches the target position
124-
//Detect Collisions:
125-
//ADD CODE
126-
// Waiting Period, then resume game
127-
this.timedEvent = this.time.delayedCall(3000, resumeGame, null, this);
128-
}
129-
});
130-
131-
// Stop after 1 game loop
132-
setTimeout(() => {
133-
try {
134-
// ?
135-
136-
} catch (e){};
137-
}, gameLoopSpeed);
113+
114+
this.moveFreely = false;
115+
this.canCast = false;
116+
117+
// Hook animation downward
118+
this.tweens.add({
119+
targets: this.hook,
120+
y: length + 100,
121+
duration: 1000,
122+
ease: 'Power2',
123+
onComplete: () => {
124+
// Detect collisions with any fish
125+
this.physics.add.overlap(this.hook, this.fishGroup, handleOverlap, null, this);
126+
127+
// If no collision after 3 seconds, reset
128+
this.time.delayedCall(3000, resumeGame, null, this);
129+
}
130+
});
138131
}
139-
})
140-
141-
// ----------------------------------------------------------------
142-
// Other Functions:
143-
144-
function resumeGame() {
145-
this.hook.destroy();
146-
this.moveFreely = true;
147-
this.canCast = true;
148-
}
132+
});
133+
134+
135+
// Additional Functions ----------------------------------------------------------------
136+
function handleOverlap(hook, fish) {
137+
// Prevent multiple overlaps
138+
this.physics.world.removeCollider(this.hookCollider);
139+
140+
// Stop fish and attach to hook
141+
fish.body.setVelocity(0);
142+
fish.setY(hook.y + 10);
143+
144+
// Display a popup with fish type
145+
showCatchPopup.call(this, fish);
146+
147+
// Make them rise together
148+
this.tweens.add({
149+
targets: [hook, fish],
150+
y: 110,
151+
duration: 1500,
152+
ease: 'Power1',
153+
onComplete: () => {
154+
// Example: destroy them after rising
155+
fish.destroy();
156+
hook.destroy();
157+
this.moveFreely = true;
158+
this.canCast = true;
159+
}
160+
});
161+
}
162+
163+
function showCatchPopup(fish) {
164+
const fishName = fish.fishName || 'a Fish?';
165+
166+
// Create popup text near top center
167+
const popup = this.add.text(400, 100, `🎣 Caught ${fishName}!`, {
168+
fontSize: '20px',
169+
fontFamily: 'Arial',
170+
color: '#ffffffff',
171+
backgroundColor: '#467862ff',
172+
padding: { x: 10, y: 5 },
173+
}).setOrigin(0.5);
174+
175+
// Animate popup fading and moving up
176+
this.tweens.add({
177+
targets: popup,
178+
y: 60,
179+
alpha: 0,
180+
duration: 1500,
181+
ease: 'Power1',
182+
onComplete: () => popup.destroy(),
183+
});
184+
}
185+
186+
function resumeGame() {
187+
if (this.hook) this.hook.destroy();
188+
this.moveFreely = true;
189+
this.canCast = true;
190+
}
191+
//--------------------------------------------------------------------------------------
149192

150193
}
151194

0 commit comments

Comments
 (0)