Skip to content

Commit 68dfc05

Browse files
committed
cast() and hook implementation
1 parent da471d1 commit 68dfc05

1 file changed

Lines changed: 64 additions & 20 deletions

File tree

src/scenes/mainGame.js

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default class MainGame extends Phaser.Scene{
1010
super('MainGame');
1111

1212
this.speed = 100;
13+
this.length = 100;
1314

1415
}
1516

@@ -26,6 +27,9 @@ export default class MainGame extends Phaser.Scene{
2627
this.player = this.physics.add.sprite(400, 110, 'player').setDisplaySize(180,120);
2728
this.player.setCollideWorldBounds(true);
2829

30+
// Variables
31+
this.moveFreely = true;
32+
this.rightFacing = true;
2933

3034
// C4C default text
3135
C4C.Editor.setText(`// Enter your code here!\n`);
@@ -34,40 +38,80 @@ export default class MainGame extends Phaser.Scene{
3438
// Keyboard Input
3539
this.cursor = this.input.keyboard.createCursorKeys();
3640

37-
// Define functions used in the written code----------------------------
38-
39-
// THIS DOES NOTHING ATM
40-
C4C.Interpreter.define('cast', (speed) => {
41-
if (speed === undefined) {
42-
speed = 100;
41+
// Define functions used in the written coding area---------------------
42+
43+
// addBait(bait type)
44+
45+
// cast(length)
46+
C4C.Interpreter.define('cast', (length) => {
47+
if (length === undefined) {
48+
length = 100;
49+
}
50+
// Create hook
51+
if (this.rightFacing){
52+
this.hook = this.physics.add.sprite(this.player.x + 90, this.player.y - 60, 'hook').setDisplaySize(30,30);
53+
} else {
54+
this.hook = this.physics.add.sprite(this.player.x - 90, this.player.y - 60, 'hook').setDisplaySize(30,30);
4355
}
44-
this.boat.flipY;
56+
// Stop/freeze player movements
57+
this.moveFreely = false;
58+
59+
// Move hook down according to length arg
60+
this.tweens.add({
61+
targets: this.hook, // The sprite you want to move
62+
y: length + 100, // The target Y coordinate
63+
duration: 1000, // Duration of the tween in milliseconds
64+
ease: 'Power2', // Easing function for smoother animation (optional)
65+
onComplete: () => {
66+
// Code to execute when the sprite reaches the target position
67+
//Detect Collisions:
68+
//ADD CODE
69+
// Waiting Period, then resume game
70+
this.timedEvent = this.time.delayedCall(3000, resumeGame, null, this);
71+
}
72+
});
73+
4574
// Stop after 1 game loop
4675
setTimeout(() => {
4776
try {
48-
this.boat.flipY;
77+
// ?
78+
4979
} catch (e){};
5080
}, gameLoopSpeed);
5181
})
52-
// ----------------------------------------------------------------
82+
83+
// ----------------------------------------------------------------
84+
// Other Functions:
85+
86+
function resumeGame() {
87+
this.hook.destroy();
88+
this.moveFreely = true;
89+
}
90+
5391
}
5492

93+
5594
update(){
5695

5796
// Conditional logic for keyboard controls
5897
const speed = this.cursor.shift.isDown ? 300 : 160;
5998

99+
if(this.moveFreely) {
60100
// Boat & Raccoon movement
61-
if (this.cursor.left.isDown) {
62-
this.player.setVelocityX(-speed);
63-
this.player.setFlipX(true);
64-
} else if (this.cursor.right.isDown) {
65-
this.player.setVelocityX(speed);
66-
this.player.setFlipX(false);
67-
} else {
68-
this.player.setVelocityX(0);
69-
}
70-
101+
if (this.cursor.left.isDown) {
102+
this.player.setVelocityX(-speed);
103+
this.player.setFlipX(true);
104+
this.rightFacing = false;
105+
} else if (this.cursor.right.isDown) {
106+
this.player.setVelocityX(speed);
107+
this.player.setFlipX(false);
108+
this.rightFacing = true;
109+
} else {
110+
this.player.setVelocityX(0);
111+
}
112+
}
113+
71114
// Functions
72115
}
73-
}
116+
}
117+

0 commit comments

Comments
 (0)