Initial commit: Naddie Jump — Monad Edition
This commit is contained in:
117
src/entities/Player.js
Normal file
117
src/entities/Player.js
Normal file
@@ -0,0 +1,117 @@
|
||||
import { Physics } from 'phaser';
|
||||
import {
|
||||
GAME_WIDTH,
|
||||
JUMP_VELOCITY,
|
||||
SUPER_JUMP_VELOCITY,
|
||||
ROCKET_VELOCITY,
|
||||
PROPELLER_VELOCITY,
|
||||
PLAYER_SPEED,
|
||||
} from '../config/game.config.js';
|
||||
|
||||
export class Player extends Physics.Arcade.Sprite {
|
||||
constructor(scene, x, y) {
|
||||
super(scene, x, y, 'player_idle');
|
||||
scene.add.existing(this);
|
||||
scene.physics.add.existing(this);
|
||||
|
||||
this.setCollideWorldBounds(false);
|
||||
this.setScale(0.45);
|
||||
this.body.setSize(70, 90);
|
||||
|
||||
this.state = 'normal'; // normal, propeller, rocket, dead
|
||||
this.propellerTimer = 0;
|
||||
this.rocketTimer = 0;
|
||||
}
|
||||
|
||||
update(cursors, wasd, touchLeft, touchRight, time, delta) {
|
||||
if (this.state === 'dead') return;
|
||||
|
||||
let velocityX = 0;
|
||||
if (cursors.left.isDown || wasd.left.isDown || touchLeft) velocityX = -PLAYER_SPEED;
|
||||
if (cursors.right.isDown || wasd.right.isDown || touchRight) velocityX = PLAYER_SPEED;
|
||||
this.setVelocityX(velocityX);
|
||||
|
||||
// Screen wrap
|
||||
if (this.x < -this.width / 2) this.x = GAME_WIDTH + this.width / 2;
|
||||
if (this.x > GAME_WIDTH + this.width / 2) this.x = -this.width / 2;
|
||||
|
||||
// Tilt sprite based on movement
|
||||
if (velocityX < 0) {
|
||||
this.setFlipX(true);
|
||||
this.setAngle(-5);
|
||||
} else if (velocityX > 0) {
|
||||
this.setFlipX(false);
|
||||
this.setAngle(5);
|
||||
} else {
|
||||
this.setAngle(0);
|
||||
}
|
||||
|
||||
// Power-up timers
|
||||
if (this.state === 'propeller') {
|
||||
this.propellerTimer -= delta;
|
||||
this.setVelocityY(PROPELLER_VELOCITY);
|
||||
if (this.propellerTimer <= 0) this.endPowerUp();
|
||||
} else if (this.state === 'rocket') {
|
||||
this.rocketTimer -= delta;
|
||||
this.setVelocityY(ROCKET_VELOCITY);
|
||||
if (this.rocketTimer <= 0) this.endPowerUp();
|
||||
}
|
||||
}
|
||||
|
||||
jump(force = JUMP_VELOCITY) {
|
||||
if (this.state === 'dead' || this.state === 'rocket' || this.state === 'propeller') return;
|
||||
this.setVelocityY(force);
|
||||
// Squash animation
|
||||
this.scene.tweens.add({
|
||||
targets: this,
|
||||
scaleX: 0.55,
|
||||
scaleY: 0.4,
|
||||
duration: 80,
|
||||
yoyo: true,
|
||||
ease: 'Quad.easeOut',
|
||||
});
|
||||
}
|
||||
|
||||
startPropeller() {
|
||||
if (this.state === 'dead') return;
|
||||
this.state = 'propeller';
|
||||
this.propellerTimer = 3500;
|
||||
const oldHeight = this.displayHeight;
|
||||
this.setTexture('player_propeller');
|
||||
this.setScale(0.45);
|
||||
this.body.setSize(70, 105);
|
||||
this.y -= (this.displayHeight - oldHeight) / 2;
|
||||
}
|
||||
|
||||
startRocket() {
|
||||
if (this.state === 'dead') return;
|
||||
this.state = 'rocket';
|
||||
this.rocketTimer = 4000;
|
||||
const oldHeight = this.displayHeight;
|
||||
this.setTexture('player_rocket');
|
||||
this.setScale(0.52);
|
||||
this.body.setSize(55, 180);
|
||||
this.y -= (this.displayHeight - oldHeight) / 2;
|
||||
}
|
||||
|
||||
endPowerUp() {
|
||||
const oldHeight = this.displayHeight;
|
||||
this.state = 'normal';
|
||||
this.setTexture('player_idle');
|
||||
this.setScale(0.45);
|
||||
this.body.setSize(70, 90);
|
||||
this.setAngle(0);
|
||||
this.y -= (this.displayHeight - oldHeight) / 2;
|
||||
}
|
||||
|
||||
die() {
|
||||
if (this.state === 'dead') return;
|
||||
this.state = 'dead';
|
||||
this.setTexture('player_dead');
|
||||
this.setScale(0.4);
|
||||
this.setAngle(0);
|
||||
this.body.setSize(70, 90);
|
||||
this.setVelocity(0, -250);
|
||||
this.body.allowGravity = true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user