class Example extends Phaser.Scene {
preload() {
this.load.image(
"bullet",
"https://assets.codepen.io/9367036/1bit-projectile.png"
);
this.load.image(
"enemyBullet",
"https://assets.codepen.io/9367036/1bit-enemy-projectile.png"
);
this.load.image(
"ship",
"https://assets.codepen.io/9367036/1bit-player.png"
);
this.load.image(
"starfield",
"https://assets.codepen.io/9367036/background_1.png"
);
this.load.spritesheet(
"enemy",
"https://assets.codepen.io/9367036/1bit-enemy.png",
{
frameWidth: 16 * 4,
frameHeight: 16 * 4
}
);
this.load.spritesheet(
"hit",
"https://assets.codepen.io/9367036/1bit-hit.png",
{
frameWidth: 16,
frameHeight: 16
}
);
this.load.spritesheet(
"explode",
"https://assets.codepen.io/9367036/explosion.png",
{
frameWidth: 100,
frameHeight: 100
}
);
}
create() {
this.stars = this.add
.tileSprite(0, 0, 800, 400, "starfield")
.setOrigin(0, 0);
this.anims.create({
key: "explode",
frames: this.anims.generateFrameNumbers("explode", { start: 0, end: 32 }),
frameRate: 30,
repeat: 0
});
this.anims.create({
key: "playerHit",
frames: this.anims.generateFrameNumbers("hit", { start: 0, end: 7 }),
frameRate: 4,
repeat: 0
});
this.anims.create({
key: "enemyDeath",
frames: this.anims.generateFrameNumbers("enemy", { start: 1, end: 6 }),
frameRate: 2,
repeat: 0
});
this.enemy = this.physics.add.sprite(50, 128, "enemy", 0);
this.enemy.setScale(2);
this.enemy.pointsDeVie = 5;
this.enemyMoving = this.tweens.add({
targets: this.enemy,
x: config.width - 50,
duration: 3000,
ease: "Circ.easeInOut",
yoyo: true,
repeat: -1
});
this.enemyBullets = this.physics.add.group({
defaultKey: "enemyBullet",
maxSize: 5
});
this.enemyFiring = this.time.addEvent({
delay: 666,
loop: true,
callback: () => {
const bullet = this.enemyBullets.get(this.enemy.x, this.enemy.y + 32);
if (bullet) {
bullet.setActive(true);
bullet.setVisible(true);
bullet.setVelocity(0, 200);
}
}
});
this.player = this.physics.add.sprite(256, config.height - 50, "ship");
this.player.setScale(2);
this.bullets = this.physics.add.group({
defaultKey: "bullet",
maxSize: 1
});
this.physics.add.overlap(this.enemy, this.bullets, (enemy, bullet) => {
enemy.pointsDeVie -= 1;
bullet.setActive(false);
bullet.setVisible(false);
bullet.y = -999999;
let explosion = this.add.sprite(enemy.x, enemy.y, "explode");
explosion.setScale(2);
explosion.play("explode");
explosion.on("animationcomplete", () => {
explosion.destroy();
});
if (enemy.pointsDeVie <= 0) {
enemy.body.checkCollision.none = true;
this.enemyFiring.remove();
this.enemyMoving.stop();
enemy.play("enemyDeath");
enemy.on("animationcomplete", () => {
enemy.destroy();
});
}
});
this.physics.add.overlap(
this.player,
this.enemyBullets,
(player, bullet) => {
bullet.setActive(false);
bullet.setVisible(false);
bullet.y = -999999;
player.setTexture("hit");
player.play("playerHit");
player.on("animationcomplete", () => {
player.setTexture("ship");
});
let flashTween = this.tweens.add({
targets: player,
alpha: { from: 0.33, to: 0.66 },
ease: "Linear",
duration: 100,
repeat: 10,
yoyo: true,
onComplete: () => {
player.setAlpha(1);
}
});
}
);
this.input.on("pointermove", (pointer) => {
this.player.x = pointer.worldX;
});
this.input.on("pointerdown", () => {
const bullet = this.bullets.get(this.player.x, this.player.y);
if (bullet) {
bullet.setActive(true);
bullet.setVisible(true);
bullet.setVelocity(0, -666);
}
});
}
update() {
this.bullets.children.each((bullet) => {
let cachee = !this.cameras.main.worldView.contains(bullet.x, bullet.y);
if (bullet.active && cachee) {
bullet.setActive(false);
bullet.setVisible(false);
}
});
this.enemyBullets.children.each((bullet) => {
let cachee = !this.cameras.main.worldView.contains(bullet.x, bullet.y);
if (bullet.active && cachee) {
bullet.setActive(false);
bullet.setVisible(false);
}
});
}
}
const config = {
type: Phaser.AUTO,
width: 800,
height: 400,
pixelArt: true,
parent: "phaser-example",
physics: {
default: "arcade",
arcade: { debug: false }
},
scene: Example
};
const game = new Phaser.Game(config);
activateControls(["LMB"]);