137 lines
3.3 KiB
JavaScript
137 lines
3.3 KiB
JavaScript
|
|
let width = window.innerWidth * window.devicePixelRatio;
|
|
let height = window.innerHeight * window.devicePixelRatio;
|
|
let config = {
|
|
title: "Happy 2021!",
|
|
url: "http://shikiryu.ovh/new_year_2021",
|
|
type: Phaser.AUTO,
|
|
width: width,
|
|
height: height,
|
|
background: 0xffffff,
|
|
scene: {
|
|
preload: preload,
|
|
create: create,
|
|
update: update
|
|
}
|
|
};
|
|
|
|
let game = new Phaser.Game(config);
|
|
|
|
let that;
|
|
let shoot;
|
|
let win;
|
|
let graphics;
|
|
let rect;
|
|
let viruses = [];
|
|
let target;
|
|
let maxEnemies = 20;
|
|
let enemiesCount = maxEnemies;
|
|
let enemiesText;
|
|
let colors = ['bleu', 'rouge', 'vert'];
|
|
|
|
function preload() {
|
|
that = this;
|
|
this.scale.scaleMode = Phaser.Scale.ScaleManager.SHOW_ALL;
|
|
this.scale.pageAlignHorizontally = true;
|
|
this.scale.pageAlignVertically = true;
|
|
this.load.audio('shoot', [ 'sound/shoot.ogg', 'sound/shoot.mp3' ]);
|
|
this.load.audio('win', [ 'sound/win.ogg', 'sound/win.mp3' ]);
|
|
this.load.image('target', 'img/target.png');
|
|
this.load.image('bleu', 'img/virus-bleu.png');
|
|
this.load.image('rouge', 'img/virus-rouge.png');
|
|
this.load.image('vert', 'img/virus-vert.png');
|
|
}
|
|
|
|
function killVirus(player, virus) {
|
|
virus.visible = false;
|
|
--enemiesCount;
|
|
enemiesText.setText('virus restant : '+enemiesCount);
|
|
if (enemiesCount === 0) {
|
|
enemiesText.visible = false;
|
|
that.add.text(width/2-200, height/2-50, 'Bonne annnée 2021 !', { fontSize: '32px', fill: '#000' });
|
|
that.add.text(width/2-50, height/2, 'Si possible, sans virus', { fontSize: '14px', fill: '#000' });
|
|
win.play();
|
|
}
|
|
}
|
|
|
|
function getRandomInt(max) {
|
|
return Math.floor(Math.random() * Math.floor(max));
|
|
}
|
|
|
|
function create() {
|
|
shoot = this.sound.add('shoot');
|
|
win = this.sound.add('win');
|
|
|
|
graphics = this.add.graphics();
|
|
graphics.fillGradientStyle(0x3d6d7d, 0x3d6d7d, 0x9addf3, 0x9addf3, 1);
|
|
graphics.fillRect(0, 0, width, height);
|
|
|
|
enemiesText = this.add.text(40, 40, 'virus restant : '+maxEnemies, { fontSize: '32px', fill: '#000' });
|
|
|
|
viruses = new Array(maxEnemies).fill(null).map(
|
|
function() {
|
|
let color = colors[Math.floor(Math.random() * colors.length)];
|
|
return that.add.image(getRandomInt(width), getRandomInt(height), color);
|
|
}
|
|
);
|
|
|
|
rect = Phaser.Geom.Rectangle.Clone(this.cameras.main);
|
|
|
|
target = this.add.image(400, 300, 'target');
|
|
|
|
this.input.on('pointermove', function (pointer) {
|
|
target.x = pointer.x;
|
|
target.y = pointer.y;
|
|
});
|
|
|
|
this.input.on('pointerdown', function (pointer) {
|
|
target.x = pointer.x;
|
|
target.y = pointer.y;
|
|
let pointerX = pointer.x;
|
|
let pointerY = pointer.y;
|
|
shoot.play();
|
|
for (let i = 0; i < maxEnemies; i++) {
|
|
let virus = viruses[i];
|
|
let virusX = virus.x;
|
|
let virusY = virus.y;
|
|
if (
|
|
virus.visible &&
|
|
pointerX >= virusX - 94 && pointerX <= virusX + 94 &&
|
|
pointerY >= virusY - 94 && pointerY <= virusY + 94
|
|
) {
|
|
killVirus(null, virus);
|
|
break;
|
|
}
|
|
}
|
|
}, this);
|
|
|
|
}
|
|
|
|
function update() {
|
|
viruses.forEach(function (virus, i) {
|
|
let direction = getRandomInt(4);
|
|
let x = (1 + Math.random() * i);
|
|
let y = (1 + Math.random() * i);
|
|
switch (direction) {
|
|
case 1:
|
|
virus.x += x;
|
|
virus.y += y;
|
|
break;
|
|
case 2:
|
|
virus.x += x;
|
|
virus.y -= y;
|
|
break;
|
|
case 3:
|
|
virus.x -= x;
|
|
virus.y += y;
|
|
break;
|
|
case 4:
|
|
virus.x -= x;
|
|
virus.y -= y;
|
|
break;
|
|
}
|
|
|
|
});
|
|
|
|
Phaser.Actions.WrapInRectangle(viruses, rect, 72);
|
|
} |