369 lines
13 KiB
JavaScript
369 lines
13 KiB
JavaScript
const config = {
|
|
type: Phaser.AUTO,
|
|
width: 800,
|
|
height: 600,
|
|
physics: {
|
|
default: 'arcade',
|
|
arcade: {
|
|
gravity: { y: 800 },
|
|
debug: false
|
|
}
|
|
},
|
|
scene: [] // Scenes will be added dynamically
|
|
};
|
|
|
|
const game = new Phaser.Game(config);
|
|
|
|
// --- Base Scene for common functionality ---
|
|
class BaseScene extends Phaser.Scene {
|
|
constructor(key) {
|
|
super(key);
|
|
}
|
|
|
|
createBackButton() {
|
|
const backButton = this.add.text(this.cameras.main.width - 20, 20, 'Back to Menu', {
|
|
fontSize: '24px',
|
|
fill: '#fff',
|
|
backgroundColor: '#333',
|
|
padding: { x: 10, y: 5 }
|
|
}).setOrigin(1, 0);
|
|
|
|
backButton.setInteractive();
|
|
backButton.on('pointerdown', () => {
|
|
this.scene.start('MainMenuScene');
|
|
});
|
|
backButton.on('pointerover', () => backButton.setStyle({ fill: '#ff0' }));
|
|
backButton.on('pointerout', () => backButton.setStyle({ fill: '#fff' }));
|
|
}
|
|
}
|
|
|
|
|
|
// --- Main Menu Scene ---
|
|
class MainMenuScene extends Phaser.Scene {
|
|
constructor() {
|
|
super('MainMenuScene');
|
|
}
|
|
|
|
create() {
|
|
this.add.text(400, 100, 'Cosmic Runner', { fontSize: '64px', fill: '#fff' }).setOrigin(0.5);
|
|
|
|
const startButton = this.add.text(400, 250, 'Start Game', { fontSize: '32px', fill: '#0f0', backgroundColor: '#333', padding: { x: 10, y: 5 } }).setOrigin(0.5);
|
|
const rulesButton = this.add.text(400, 320, 'Rules & Bonuses', { fontSize: '32px', fill: '#ff0', backgroundColor: '#333', padding: { x: 10, y: 5 } }).setOrigin(0.5);
|
|
const scoresButton = this.add.text(400, 390, 'High Scores', { fontSize: '32px', fill: '#f0f', backgroundColor: '#333', padding: { x: 10, y: 5 } }).setOrigin(0.5);
|
|
|
|
// Interactivity
|
|
[startButton, rulesButton, scoresButton].forEach(button => {
|
|
button.setInteractive();
|
|
button.on('pointerover', () => button.setAlpha(0.8));
|
|
button.on('pointerout', () => button.setAlpha(1));
|
|
});
|
|
|
|
startButton.on('pointerdown', () => this.scene.start('GameScene'));
|
|
rulesButton.on('pointerdown', () => this.scene.start('RulesScene'));
|
|
scoresButton.on('pointerdown', () => this.scene.start('HighScoresScene'));
|
|
}
|
|
}
|
|
|
|
// --- Rules Scene ---
|
|
class RulesScene extends BaseScene {
|
|
constructor() {
|
|
super('RulesScene');
|
|
}
|
|
|
|
create() {
|
|
this.add.text(400, 50, 'Rules & Bonuses', { fontSize: '48px', fill: '#fff' }).setOrigin(0.5);
|
|
|
|
const rulesText = [
|
|
'1. Click or press SPACE to jump.',
|
|
'2. Avoid the green and blue obstacles.',
|
|
'3. Collect yellow coins for extra points (+100).',
|
|
'',
|
|
'Bonuses:',
|
|
'- Blue Shield: Grants 10 seconds of invincibility.',
|
|
' (You will destroy obstacles on contact!)'
|
|
];
|
|
|
|
this.add.text(400, 300, rulesText, { fontSize: '24px', fill: '#fff', align: 'center' }).setOrigin(0.5);
|
|
|
|
this.createBackButton();
|
|
}
|
|
}
|
|
|
|
// --- High Scores Scene ---
|
|
class HighScoresScene extends BaseScene {
|
|
constructor() {
|
|
super('HighScoresScene');
|
|
}
|
|
|
|
create() {
|
|
this.add.text(400, 50, 'High Scores', { fontSize: '48px', fill: '#fff' }).setOrigin(0.5);
|
|
|
|
const highScore = localStorage.getItem('highScore') || 0;
|
|
this.add.text(400, 200, `Best Score: ${highScore}`, { fontSize: '38px', fill: '#fff' }).setOrigin(0.5);
|
|
|
|
const clearButton = this.add.text(400, 400, 'Clear Scores', { fontSize: '24px', fill: '#f00', backgroundColor: '#333', padding: { x: 10, y: 5 } }).setOrigin(0.5);
|
|
clearButton.setInteractive();
|
|
clearButton.on('pointerdown', () => {
|
|
localStorage.setItem('highScore', '0');
|
|
this.scene.restart();
|
|
});
|
|
clearButton.on('pointerover', () => clearButton.setAlpha(0.8));
|
|
clearButton.on('pointerout', () => clearButton.setAlpha(1));
|
|
|
|
|
|
this.createBackButton();
|
|
}
|
|
}
|
|
|
|
|
|
// --- Game Scene ---
|
|
class GameScene extends Phaser.Scene {
|
|
constructor() {
|
|
super('GameScene');
|
|
this.player = null;
|
|
this.ground = null;
|
|
this.obstacles = null;
|
|
this.coins = null;
|
|
this.shields = null;
|
|
this.score = 0;
|
|
this.scoreText = null;
|
|
this.highScore = 0;
|
|
this.highScoreText = null;
|
|
this.bg1 = null;
|
|
this.bg2 = null;
|
|
this.bg3 = null;
|
|
this.jumpParticles = null;
|
|
this.landParticles = null;
|
|
this.wasOnGround = false;
|
|
this.gameState = 'playing';
|
|
this.gameOverText = null;
|
|
this.restartText = null;
|
|
this.obstacleTimer = null;
|
|
this.scoreTimer = null;
|
|
this.coinTimer = null;
|
|
this.shieldTimer = null;
|
|
}
|
|
|
|
preload() {
|
|
// Preload assets if any - currently all generated dynamically
|
|
}
|
|
|
|
create() {
|
|
this.gameState = 'playing';
|
|
this.score = 0;
|
|
this.highScore = localStorage.getItem('highScore') || 0;
|
|
|
|
// --- Create Starfield ---
|
|
const starfieldGraphics = this.add.graphics();
|
|
starfieldGraphics.fillStyle(0xffffff);
|
|
for (let i = 0; i < 100; i++) {
|
|
starfieldGraphics.fillPoint(
|
|
Phaser.Math.Between(0, 800),
|
|
Phaser.Math.Between(0, 600),
|
|
Phaser.Math.Between(1, 2)
|
|
);
|
|
}
|
|
starfieldGraphics.generateTexture('starfield', 800, 600);
|
|
starfieldGraphics.destroy();
|
|
|
|
this.bg1 = this.add.tileSprite(400, 300, 800, 600, 'starfield').setAlpha(0.3);
|
|
this.bg2 = this.add.tileSprite(400, 300, 800, 600, 'starfield').setAlpha(0.6);
|
|
this.bg3 = this.add.tileSprite(400, 300, 800, 600, 'starfield');
|
|
|
|
this.ground = this.add.rectangle(400, 580, 800, 40, 0x333333).setOrigin(0.5);
|
|
this.physics.add.existing(this.ground, true);
|
|
|
|
this.player = this.add.container(100, 450);
|
|
const body = this.add.rectangle(0, 0, 40, 40, 0xff8800);
|
|
const cockpit = this.add.rectangle(10, -5, 20, 20, 0x88ccff);
|
|
const wing = this.add.triangle(0, 0, -20, -15, -20, 15, 0, 0, 0xffffff);
|
|
this.player.add([body, cockpit, wing]);
|
|
this.physics.add.existing(this.player);
|
|
|
|
this.player.body.setBounce(0);
|
|
this.player.body.setCollideWorldBounds(true);
|
|
this.player.body.setSize(40, 40);
|
|
this.player.isInvincible = false;
|
|
|
|
const jumpParticleGraphics = this.add.graphics();
|
|
jumpParticleGraphics.fillStyle(0xffffff, 1);
|
|
jumpParticleGraphics.fillGradientStyle(0xffff00, 0xffff00, 0xffffff, 0xffffff, 1);
|
|
jumpParticleGraphics.fillRect(0, 0, 16, 16);
|
|
jumpParticleGraphics.generateTexture('jump_particle', 16, 16);
|
|
jumpParticleGraphics.destroy();
|
|
|
|
const landParticleGraphics = this.add.graphics();
|
|
landParticleGraphics.fillStyle(0xffffff, 0.5);
|
|
landParticleGraphics.fillCircle(8, 8, 8);
|
|
landParticleGraphics.generateTexture('land_particle', 16, 16);
|
|
landParticleGraphics.destroy();
|
|
|
|
this.jumpParticles = this.add.particles('jump_particle');
|
|
this.jumpParticles.createEmitter({
|
|
speed: { min: -100, max: 100 },
|
|
angle: { min: 250, max: 290 },
|
|
scale: { start: 0.8, end: 0 },
|
|
blendMode: 'ADD',
|
|
lifespan: 400,
|
|
tint: { start: 0xffff00, end: 0xffffff },
|
|
quantity: 15,
|
|
on: false
|
|
});
|
|
|
|
this.landParticles = this.add.particles('land_particle');
|
|
this.landParticles.createEmitter({
|
|
speed: { min: 50, max: 100 },
|
|
angle: { min: 260, max: 280 },
|
|
scale: { start: 1, end: 0 },
|
|
alpha: { start: 0.6, end: 0 },
|
|
lifespan: 500,
|
|
quantity: 20,
|
|
on: false
|
|
});
|
|
|
|
this.physics.add.collider(this.player, this.ground);
|
|
|
|
const handleAction = () => {
|
|
if (this.gameState === 'playing' && this.player.body.touching.down) {
|
|
this.player.body.setVelocityY(-500);
|
|
this.jumpParticles.emitParticleAt(this.player.x, this.player.y + 25);
|
|
} else if (this.gameState === 'gameOver') {
|
|
this.scene.start('MainMenuScene'); // Go back to menu
|
|
}
|
|
};
|
|
|
|
this.input.on('pointerdown', handleAction);
|
|
this.input.keyboard.on('keydown-SPACE', handleAction);
|
|
|
|
this.obstacles = this.physics.add.group();
|
|
this.physics.add.collider(this.player, this.obstacles, this.hitObstacle, null, this);
|
|
|
|
this.coins = this.physics.add.group({ allowGravity: false });
|
|
this.physics.add.overlap(this.player, this.coins, this.collectCoin, null, this);
|
|
|
|
this.shields = this.physics.add.group({ allowGravity: false });
|
|
this.physics.add.overlap(this.player, this.shields, this.collectShield, null, this);
|
|
|
|
this.obstacleTimer = this.time.addEvent({ delay: 1500, callback: this.addObstacle, callbackScope: this, loop: true });
|
|
this.coinTimer = this.time.addEvent({ delay: 2000, callback: this.addCoin, callbackScope: this, loop: true });
|
|
this.shieldTimer = this.time.addEvent({ delay: 10000, callback: this.addShield, callbackScope: this, loop: true });
|
|
|
|
this.scoreText = this.add.text(16, 16, 'Score: 0', { fontSize: '32px', fill: '#fff' });
|
|
this.highScoreText = this.add.text(16, 50, `Best: ${this.highScore}`, { fontSize: '24px', fill: '#fff', alpha: 0.7 });
|
|
|
|
this.scoreTimer = this.time.addEvent({
|
|
delay: 100,
|
|
callback: () => {
|
|
if (this.gameState === 'playing') {
|
|
this.score++;
|
|
this.scoreText.setText('Score: ' + this.score);
|
|
}
|
|
},
|
|
callbackScope: this,
|
|
loop: true
|
|
});
|
|
|
|
this.gameOverText = this.add.text(400, 250, 'Game Over', { fontSize: '64px', fill: '#ff0000', align: 'center' }).setOrigin(0.5).setVisible(false);
|
|
this.restartText = this.add.text(400, 380, 'Click or Press Space for Menu', { fontSize: '32px', fill: '#fff' }).setOrigin(0.5).setVisible(false);
|
|
}
|
|
|
|
update() {
|
|
this.bg1.tilePositionX += 0.2;
|
|
this.bg2.tilePositionX += 0.4;
|
|
this.bg3.tilePositionX += 0.8;
|
|
|
|
if (this.gameState !== 'playing') {
|
|
return;
|
|
}
|
|
|
|
const onGround = this.player.body.touching.down;
|
|
if (onGround && !this.wasOnGround) {
|
|
this.landParticles.emitParticleAt(this.player.x, this.player.y + 20);
|
|
}
|
|
this.wasOnGround = onGround;
|
|
|
|
['obstacles', 'coins', 'shields'].forEach(groupName => {
|
|
this[groupName].getChildren().forEach(child => {
|
|
if (child.x < -50) {
|
|
child.destroy();
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
addObstacle() {
|
|
if (this.gameState !== 'playing') return;
|
|
const isFlying = Phaser.Math.Between(0, 2) === 0;
|
|
const obstacleHeight = Phaser.Math.Between(50, 120);
|
|
let obstacleY = isFlying ? Phaser.Math.Between(400, 480) : 600 - obstacleHeight;
|
|
const obstacle = this.add.rectangle(850, obstacleY, 50, obstacleHeight, isFlying ? 0x00aaff : 0x00ff00).setOrigin(0.5, 0);
|
|
this.physics.add.existing(obstacle);
|
|
this.obstacles.add(obstacle);
|
|
obstacle.body.setVelocityX(-200).setImmovable(true).setAllowGravity(false);
|
|
}
|
|
|
|
addCoin() {
|
|
if (this.gameState !== 'playing') return;
|
|
const coin = this.add.circle(850, Phaser.Math.Between(300, 500), 15, 0xffff00);
|
|
this.physics.add.existing(coin);
|
|
this.coins.add(coin);
|
|
coin.body.setVelocityX(-200).setAllowGravity(false);
|
|
}
|
|
|
|
addShield() {
|
|
if (this.gameState !== 'playing') return;
|
|
const shield = this.add.rectangle(850, Phaser.Math.Between(300, 500), 30, 30, 0x0000ff).setOrigin(0.5);
|
|
this.physics.add.existing(shield);
|
|
this.shields.add(shield);
|
|
shield.body.setVelocityX(-200).setAllowGravity(false);
|
|
}
|
|
|
|
collectCoin(player, coin) {
|
|
coin.destroy();
|
|
this.score += 100;
|
|
this.scoreText.setText('Score: ' + this.score);
|
|
}
|
|
|
|
collectShield(player, shield) {
|
|
shield.destroy();
|
|
player.isInvincible = true;
|
|
player.setAlpha(0.5);
|
|
this.time.delayedCall(10000, () => {
|
|
player.isInvincible = false;
|
|
player.setAlpha(1);
|
|
}, [], this);
|
|
}
|
|
|
|
hitObstacle(player, obstacle) {
|
|
if (player.isInvincible) {
|
|
obstacle.destroy();
|
|
return;
|
|
}
|
|
if (this.gameState === 'gameOver') return;
|
|
|
|
this.gameState = 'gameOver';
|
|
this.physics.pause();
|
|
[this.obstacleTimer, this.scoreTimer, this.coinTimer, this.shieldTimer].forEach(timer => timer.remove());
|
|
|
|
player.list.forEach(child => child.setTint && child.setTint(0xff0000));
|
|
|
|
if (this.score > this.highScore) {
|
|
this.highScore = this.score;
|
|
localStorage.setItem('highScore', this.highScore);
|
|
}
|
|
|
|
this.gameOverText.setText(`Game Over\nScore: ${this.score}\nBest: ${this.highScore}`).setVisible(true);
|
|
this.restartText.setVisible(true);
|
|
this.highScoreText.setVisible(false);
|
|
}
|
|
}
|
|
|
|
// Add all scenes to the game
|
|
game.scene.add('MainMenuScene', MainMenuScene);
|
|
game.scene.add('GameScene', GameScene);
|
|
game.scene.add('RulesScene', RulesScene);
|
|
game.scene.add('HighScoresScene', HighScoresScene);
|
|
|
|
// Start with the Main Menu
|
|
game.scene.start('MainMenuScene');
|