fix: cat jump bug @ restart

This commit is contained in:
nadiarvi 2025-05-10 18:21:26 +09:00
parent 874f24e47f
commit b797f7be41

View File

@ -28,7 +28,7 @@ export class Cat {
this.size = img.height / 5; this.size = img.height / 5;
this.sprite = new Sprite(this.x, this.y, this.size, this.size, 'dynamic'); this.sprite = new Sprite(this.x, this.y, this.size, this.size, 'dynamic');
console.log(`sprite made at ${this.x}, ${this.y}`); //console.log(`sprite made at ${this.x}, ${this.y}`);
this.sprite.rotationLock = true; this.sprite.rotationLock = true;
this.sprite.bounciness = 0.1; this.sprite.bounciness = 0.1;
this.sprite.spriteSheet = img; this.sprite.spriteSheet = img;
@ -52,12 +52,14 @@ export class Cat {
// this.sprite.anis.offset.x = this.targetSize / 4; // this.sprite.anis.offset.x = this.targetSize / 4;
this.loaded = true; this.loaded = true;
this._debug();
}); });
} }
update() { update() {
if (!this.sprite || this.steps.length === 0) return; if (!this.sprite || this.steps.length === 0) return;
// console.log(`updating sprite...`); // //console.log(`updating sprite...`);
if (this.isMoving) { if (this.isMoving) {
this._continueMovement(); this._continueMovement();
@ -75,6 +77,8 @@ export class Cat {
} }
_startMovement(direction) { _startMovement(direction) {
//console.log('start');
//console.log(`[startMovement] called with direction: ${direction}`);
this.moveDirection = direction; this.moveDirection = direction;
this.stepTimer = 0; this.stepTimer = 0;
this.isMoving = true; this.isMoving = true;
@ -123,6 +127,8 @@ export class Cat {
} }
_continueMovement() { _continueMovement() {
//console.log('continue');
//console.log(`[startMovement] called with direction: ${this.moveDirection}`);
this.stepTimer++; this.stepTimer++;
let isOnPlatform = this._checkPlatform(); let isOnPlatform = this._checkPlatform();
@ -142,6 +148,7 @@ export class Cat {
} }
if (this.moveDirection === 'up' && isOnPlatform && !this.hasJumped) { if (this.moveDirection === 'up' && isOnPlatform && !this.hasJumped) {
//console.log(`up called with direction: ${this.moveDirection}. inside the handler`);
this.changeAni('j'); this.changeAni('j');
this.sprite.vel.y = -20; this.sprite.vel.y = -20;
@ -152,9 +159,11 @@ export class Cat {
} else { } else {
this.sprite.vel.x = 0; this.sprite.vel.x = 0;
} }
this.hasJumped = true;
} }
this.hasJumped = true;
if (this.stepTimer >= this.stepDuration) { if (this.stepTimer >= this.stepDuration) {
this.isMoving = false; this.isMoving = false;
@ -182,23 +191,16 @@ export class Cat {
} }
restart() { restart() {
console.log('resetting initial position.....'); //console.log('resetting initial position.....');
if (!this.sprite) return null; if (!this.sprite) return null;
this.sprite.x = this.x; this.sprite.x = this.x;
this.sprite.y = this.y; this.sprite.y = this.y;
console.log(`sprite re-made at ${this.x}, ${this.y}`); //console.log(`sprite re-made at ${this.x}, ${this.y}`);
this.sprite.vel.x = 0; this.sprite.vel.x = 0;
this.sprite.vel.y = 0; this.sprite.vel.y = 0;
// this.sprite.scale = this.targetSize / this.size;
// this.sprite.mirror.x = false;
// this.sprite.rotation = 0;
// this.sprite.rotationSpeed = 0;
// this.sprite.anis.offset.x = this.shiftOffset + this.targetSize / 4;
this.steps = []; this.steps = [];
this.currentStepIndex = 0; this.currentStepIndex = 0;
this.isMoving = false; this.isMoving = false;
@ -207,12 +209,18 @@ export class Cat {
// this.targetX = null; // this.targetX = null;
// this.targetY = null; // this.targetY = null;
// TRIAL PLS WORK
this.lastDirection = null;
this.hasJumped = false;
this.changeAni('i'); this.changeAni('i');
this._debug();
} }
keyPressed(key) { keyPressed(key) {
console.log(`receiving keyboard input: ${key}`); //console.log(`receiving keyboard input: ${key}`);
} }
draw() { draw() {
@ -271,22 +279,22 @@ export class Cat {
// helper // helper
_getSpritePosition(){ _getSpritePosition(){
console.log(`sprite's actual position: ${this.sprite.x}, ${this.sprite.y}`); //console.log(`sprite's actual position: ${this.sprite.x}, ${this.sprite.y}`);
return (this.sprite.x, this.sprite.y); return (this.sprite.x, this.sprite.y);
} }
_translate(step){ _translate(step){
switch (step) { switch (step) {
case 'up': case 'up':
console.log('move: up'); //console.log('move: up');
return 'jump' return 'jump'
break; break;
case 'right': case 'right':
console.log('move: down'); //console.log('move: down');
return 'walk' return 'walk'
break; break;
case 'down': case 'down':
console.log('move: up'); //console.log('move: up');
return 'jump' return 'jump'
break; break;
default: default:
@ -295,7 +303,41 @@ export class Cat {
} }
_handleInput(){ _handleInput(){
// console.log('assume i have something....'); // //console.log('assume i have something....');
} }
_debug() {
//console.log("===== CAT DEBUG INFO =====");
//console.log(`Position: (${this.x}, ${this.y})`);
//console.log(`Target Size: ${this.targetSize}`);
//console.log(`Current Size: ${this.size}`);
//console.log(`Velocity: ${this.velocity}`);
//console.log(`Block Size: ${this.blockSize}`);
//console.log(`Step Duration: ${this.stepDuration}`);
//console.log(`Current Step Index: ${this.currentStepIndex}`);
//console.log(`Is Moving: ${this.isMoving}`);
//console.log(`Move Direction: ${this.moveDirection}`);
//console.log(`Last Direction: ${this.lastDirection}`);
//console.log(`Has Jumped: ${this.hasJumped}`);
//console.log(`Loaded: ${this.loaded}`);
//console.log(`Shift Offset: ${this.shiftOffset}`);
//console.log(`Ground Ref:`, this.ground);
//console.log(`Obstacles Ref:`, this.obstacles);
if (this.sprite) {
//console.log("Sprite Info:");
//console.log(` Sprite Position: (${this.sprite.x}, ${this.sprite.y})`);
//console.log(` Sprite Size: ${this.sprite.width} x ${this.sprite.height}`);
//console.log(` Sprite Scale: ${this.sprite.scale}`);
//console.log(` Sprite Rotation Locked: ${this.sprite.rotationLock}`);
//console.log(` Sprite Bounciness: ${this.sprite.bounciness}`);
//console.log(` Sprite Friction: ${this.sprite.friction}`);
//console.log(` Sprite Ani Offset: x=${this.sprite.anis.offset.x}, y=${this.sprite.anis.offset.y}`);
//console.log(` Current Animation: ${this.sprite.animation.name}`);
} else {
//console.log("Sprite not yet loaded.");
}
//console.log("===========================");
};
} }