120 lines
3.0 KiB
JavaScript
120 lines
3.0 KiB
JavaScript
const config = {
|
|
type: Phaser.AUTO,
|
|
scale: {
|
|
mode: Phaser.Scale.RESIZE,
|
|
width: 1920,
|
|
height: 1080,
|
|
},
|
|
parent: 'game',
|
|
backgroundColor: '#111',
|
|
physics: {
|
|
default: 'arcade',
|
|
arcade: {
|
|
gravity: { y: 0 },
|
|
debug: false
|
|
}
|
|
},
|
|
scene: {
|
|
preload: preload,
|
|
create: create,
|
|
update: update
|
|
},
|
|
pixelArt: true
|
|
};
|
|
|
|
const game = new Phaser.Game(config);
|
|
|
|
let ship;
|
|
let cursors;
|
|
let bullets;
|
|
let bulletSpeed = 500;
|
|
let thrustSpeed = 200;
|
|
|
|
function preload() {
|
|
// Load assets here if needed
|
|
this.load.image('ship', 'assets/player.png');
|
|
this.load.image('player-thrust', 'assets/player-thrust.png');
|
|
this.load.image('starfield', 'assets/starfield.png');
|
|
}
|
|
|
|
function create() {
|
|
// Prevent right-click context menu
|
|
this.input.on('contextmenu', (event) => {
|
|
event.preventDefault();
|
|
});
|
|
|
|
// Change cursor to custom reticle (you need a 'reticle.png' image)
|
|
this.input.setDefaultCursor('url(assets/reticle.png), auto');
|
|
|
|
// Create the spaceship
|
|
ship = this.physics.add.sprite(400, 300, 'ship');
|
|
ship.setCollideWorldBounds(true);
|
|
ship.setDisplaySize(48, 48);
|
|
|
|
// Create bullet group
|
|
bullets = this.add.group({
|
|
classType: Phaser.Physics.Arcade.Sprite,
|
|
key: 'bullet',
|
|
repeat: 10,
|
|
setXY: { x: 0, y: 0, stepX: 0, stepY: 0 }
|
|
});
|
|
|
|
// Set bullet properties
|
|
bullets.getChildren().forEach(bullet => {
|
|
bullet.setActive(false);
|
|
bullet.setVisible(false);
|
|
});
|
|
|
|
// Input handling
|
|
this.input.on('pointerdown', (pointer) => {
|
|
if (pointer.leftButtonDown()) {
|
|
fireBullet();
|
|
} else if (pointer.rightButtonDown()) {
|
|
ship.setVelocity(0, 0);
|
|
ship.setAcceleration(0, 0);
|
|
ship.setDrag(0);
|
|
}
|
|
});
|
|
|
|
// Mouse movement
|
|
this.input.on('pointermove', (pointer) => {
|
|
const angle = Phaser.Math.Angle.Between(
|
|
ship.x, ship.y,
|
|
pointer.x, pointer.y
|
|
);
|
|
ship.setRotation(angle);
|
|
});
|
|
}
|
|
|
|
function update(time, delta) {
|
|
// Thrust on right click - continuous movement towards the mouse
|
|
if (this.input.activePointer.rightButtonDown()) {
|
|
const angle = Phaser.Math.Angle.Between(
|
|
ship.x, ship.y,
|
|
this.input.activePointer.x, this.input.activePointer.y
|
|
);
|
|
ship.setVelocity(
|
|
Math.cos(angle) * thrustSpeed,
|
|
Math.sin(angle) * thrustSpeed
|
|
);
|
|
} else {
|
|
// If not thrusting, apply drag to slow down
|
|
ship.setDrag(100);
|
|
}
|
|
}
|
|
|
|
function fireBullet() {
|
|
const bullet = bullets.getFirstDead();
|
|
if (bullet) {
|
|
bullet.setPosition(ship.x, ship.y);
|
|
bullet.setActive(true);
|
|
bullet.setVisible(true);
|
|
|
|
// Set direction based on the ship's rotation
|
|
const angle = ship.rotation;
|
|
bullet.setVelocity(
|
|
Math.cos(angle) * bulletSpeed,
|
|
Math.sin(angle) * bulletSpeed
|
|
);
|
|
}
|
|
} |