let width = window.innerWidth;
let height = window.innerHeight;
let ratio = 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.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');
	this.load.image('spark0', 'img/blue.png');
	this.load.image('spark1', 'img/red.png');
}

function killVirus(player, virus) {
	virus.visible = false;
	--enemiesCount;
	enemiesText.setText('virus restant : ' + enemiesCount);
	if (enemiesCount === 0) {
		enemiesText.visible = false;
		let p0 = new Phaser.Math.Vector2(width/2 - 300, height / 2);
		let p1 = new Phaser.Math.Vector2(width/2 - 300, 200);
		let p2 = new Phaser.Math.Vector2(width/2 + 300, 200);
		let p3 = new Phaser.Math.Vector2(width/2 + 300, height / 2);
		
		let curve = new Phaser.Curves.CubicBezier(p0, p1, p2, p3);
		
		let max = 28;
		let points = [];
		let tangents = [];
		
		for (let c = 0; c <= max; c++) {
			let t = curve.getUtoTmapping(c / max);
			
			points.push(curve.getPoint(t));
			tangents.push(curve.getTangent(t));
		}
		
		let tempVec = new Phaser.Math.Vector2();
		
		let spark0 = that.add.particles('spark0');
		let spark1 = that.add.particles('spark1');
		
		for (let i = 0; i < points.length; i++) {
			let p = points[i];
			
			tempVec.copy(tangents[i]).normalizeRightHand().scale(-32).add(p);
			
			let angle = Phaser.Math.RadToDeg(Phaser.Math.Angle.BetweenPoints(p, tempVec));
			
			let particles = (i % 2 === 0) ? spark0 : spark1;
			
			particles.createEmitter({
				x: tempVec.x,
				y: tempVec.y,
				angle: angle,
				speed: {min: -100, max: 500},
				gravityY: 200,
				scale: {start: 0.4, end: 0.1},
				lifespan: 800,
				blendMode: 'SCREEN'
			});
		}
		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 = new Phaser.Geom.Rectangle(0, 0, width, height);
	
	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 / ratio && pointerX <= virusX + 94 / ratio &&
				pointerY >= virusY - 94 / ratio && pointerY <= virusY + 94 / ratio
			) {
				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);
}