class Example extends Phaser.Scene {
// Sources : https://ansimuz.itch.io/space-background
// https://helianthus-games.itch.io/animated-pixel-art-planets
preload() {
this.load.image("bg", "https://assets.codepen.io/9367036/blue-back.png");
this.load.spritesheet(
"moon",
"https://assets.codepen.io/9367036/moon-spritesheet.png",
{
frameWidth: 3000 / 60,
frameHeight: 50
}
);
this.load.spritesheet(
"rocket",
"https://assets.codepen.io/9367036/exhaust_01_spritesheet.png",
{
frameWidth: 896 / 7,
frameHeight: 384 / 3
}
);
}
create() {
let cameraScale = 1.5;
this.initX = 80;
this.initY = config.height - 50;
// Background
this.add.image(config.width / 2, config.height / 2, "bg").setScale(2);
// Fusée
this.currentTween = null;
this.anims.create({
key: "feu",
frames: this.anims.generateFrameNumbers("rocket", { start: 7, end: 14 }),
frameRate: 16,
repeat: -1
});
this.rocket = this.physics.add
.sprite(this.initX, this.initY, "rocket")
.setOrigin(0.5, 0.25)
.setScale(2)
.setRotation(Math.PI / 3)
.play("feu")
.setDepth(1);
// Lune
this.anims.create({
key: "spin",
frames: this.anims.generateFrameNumbers("moon", { start: 0, end: 59 }),
frameRate: 32,
repeat: -1
});
this.moon = this.physics.add
.sprite(config.width / 2, config.height / 2, "moon")
.setScale(cameraScale)
.play("spin");
let glowEffect = this.moon.postFX.addGlow(0xffffff, 1, 0, false, 1, 10);
this.tweens.add({
targets: glowEffect,
outerStrength: { from: 1, to: 16 },
innerStrength: { from: 0, to: 2 },
duration: 1000,
yoyo: true,
repeat: -1,
ease: "Sine.easeInOut"
});
// Caméra
this.mainCamera = this.cameras.main;
this.mainCamera.setZoom(cameraScale);
// Ajouter un écouteur de clic
//this.input.on("pointerdown", this.triggerFx, this);
}
update() {}
triggerFx() {
if (this.currentTween && this.currentTween.isPlaying()) {
this.currentTween.stop();
}
this.currentTween = this.tweens.add({
targets: this.rocket,
x: this.scale.width / 2,
y: this.scale.height / 2,
scale: 0.5,
ease: "Quart.easeIn",
duration: 1000,
onStart: () => {
this.rocket.setAlpha(1);
this.rocket.setPosition(this.initX, this.initY);
},
onComplete: () => {
this.rocket.setScale(2);
this.rocket.setAlpha(0);
this.rocket.setPosition(this.initX, this.initY);
this.cameras.main.flash();
}
});
}
}
const config = {
type: Phaser.AUTO,
width: 800,
height: 400,
scene: Example,
pixelArt: true,
transparent: true,
physics: {
default: "arcade",
arcade: {
debug: false
}
}
};
const game = new Phaser.Game(config);
//activateControls(["LMB"]);