Création des bars de vie et de bouclier. Ajout animation de tir. Les robots changent automatiquement de cible quand celle-ci meure. Les robots choisissent pour cible le robot le plus proche d'eux. Si deux robots sont à la meme distance, c'est aléatoire.

master
clmaisonha 5 years ago
parent 58863ea587
commit f0ea863610

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

File diff suppressed because one or more lines are too long

@ -11,6 +11,7 @@ class Boot extends Phaser.Scene {
this.load.spritesheet('btn_flag', 'assets/btnFlag.png', {frameWidth: 269, frameHeight: 262}); this.load.spritesheet('btn_flag', 'assets/btnFlag.png', {frameWidth: 269, frameHeight: 262});
this.load.spritesheet('btn_map', 'assets/btnMap.png', {frameWidth: 269, frameHeight: 262}); this.load.spritesheet('btn_map', 'assets/btnMap.png', {frameWidth: 269, frameHeight: 262});
this.load.image('background', 'assets/background.png'); this.load.image('background', 'assets/background.png');
this.load.image('bullet','assets/bullet.png');
} }
create() { create() {

@ -5,43 +5,73 @@ class GamingBoard extends Phaser.Scene {
preload() { preload() {
this.cameras.main.setViewport(WIDTH_WINDOW / 10, HEIGHT_WINDOW / 10, WIDTH_MAP, WIDTH_MAP); this.cameras.main.setViewport(WIDTH_WINDOW / 10, HEIGHT_WINDOW / 10, WIDTH_MAP, WIDTH_MAP);
} }
create() { create() {
this.add.image(0, 0, 'background').alpha = 0.1; this.add.image(0, 0, 'background').alpha = 0.1;
this.enemy = new Robot(WIDTH_MAP * 0.1, WIDTH_MAP * 0.1, WIDTH_MAP * 0.9, WIDTH_MAP * 0.9, 0x6666ff, this); this.listRobot = [];
//this.myself = this.add.circle(this.width * 0.1, this.width * 0.1, this.width * 0.05, 0x6666ff); this.listRobot.push(new Robot(WIDTH_MAP * 0.1, WIDTH_MAP * 0.1, WIDTH_MAP * 0.9, WIDTH_MAP * 0.9, 0x6666ff, this));
this.myself = new Robot(WIDTH_MAP * 0.1, WIDTH_MAP * 0.1, WIDTH_MAP * 0.1, WIDTH_MAP * 0.1, 0xff33cc, this); this.listRobot.push(new Robot(WIDTH_MAP * 0.1, WIDTH_MAP * 0.1, WIDTH_MAP * 0.1, WIDTH_MAP * 0.1, 0xff33cc, this));
this.myself2 = new Robot(WIDTH_MAP * 0.1, WIDTH_MAP * 0.1, WIDTH_MAP * 0.1, WIDTH_MAP * 0.9, 0xffffff, this); this.listRobot.push(new Robot(WIDTH_MAP * 0.1, WIDTH_MAP * 0.1, WIDTH_MAP * 0.1, WIDTH_MAP * 0.9, 0xffffff, this));
this.myself.setTarget(this.enemy); for (let i = 0; i < this.listRobot.length; i++) {
this.myself2.setTarget(this.enemy); this.chooseTarget(this.listRobot[i]);
}
this.events.on('resume', () => this.resume()); this.events.on('resume', () => this.resume());
} }
update(time, delta) { update(time, delta) {
super.update(time, delta); super.update(time, delta);
if (this.myself.target != null && this.myself.target.isAlive()) {
if (!this.myself.isTargetInRange())
this.myself.advanceToTarget();
else
this.myself.attackTarget();
}
if (this.myself2.target != null && this.myself2.target.isAlive()) { this.listRobot.forEach(function (robot) {
if (!this.myself2.isTargetInRange()) if (robot.haveTarget()) {
this.myself2.advanceToTarget(); if (!robot.isTargetInRange())
else robot.advanceToTarget();
this.myself2.attackTarget(); else
console.log(this.enemy.life); robot.attackTarget();
}
})
for (let i = 0; i < this.listRobot.length; i++) {
if (!this.listRobot[i].isAlive()) {
this.listRobot.splice(i, 1);
i -= 1;
} else {
if (!this.listRobot[i].haveTarget()) {
this.chooseTarget(this.listRobot[i]);
} else {
this.listRobot[i].updateTarget();
}
}
} }
} }
resume() { resume() {
this.myself2.updateTarget(); //this.myself2.updateTarget();
}
chooseTarget(robot) {
let minDist = WIDTH_MAP * 2;
let l = [];
this.listRobot.forEach(function (item) {
if (item !== robot && item.isAlive()) {
let hypot = Math.hypot(item.x - robot.x, item.y - robot.y);
if (minDist >= hypot) {
if (minDist > hypot) {
minDist = hypot;
l = [];
}
l.push(item);
}
}
});
if (this.listRobot.length > 1) {
robot.setTarget(l[Math.floor(Math.random() * (l.length + 1))]);
} else {
robot.setTarget(l[0]);
}
} }

@ -0,0 +1,68 @@
class HealthBar {
constructor(scene, width, height, x, y, valueMax, color) {
this.bar = new Phaser.GameObjects.Graphics(scene);
this.width = width;
this.height = height;
this.valueMax = valueMax;
this.value = valueMax;
this.color = color;
this.draw();
this.setX(x);
this.setY(y);
scene.add.existing(this.bar);
}
decrease(amount) {
this.value -= amount;
let diff = 0;
if (this.value < 0) {
diff = -this.value;
this.value = 0;
}
this.draw();
return diff;
}
setX(x) {
this.bar.setX(x - this.width / 2);
}
setY(y) {
this.bar.setY(y);
}
draw() {
this.bar.clear();
// BG
this.bar.fillStyle(0x000000);
this.bar.fillRect(0, 0, this.width, this.height);
// Health
this.bar.fillStyle(0xffffff);
this.bar.fillRect(2, 2, this.width - 4, this.height - 4);
let percent = this.value / this.valueMax;
if (percent < 0.3) {
this.bar.fillStyle(0xff0000);
} else {
this.bar.fillStyle(this.color);
}
let d = Math.floor(percent * (this.width - 4));
this.bar.fillRect(2, 2, d, this.height - 4);
}
destroy() {
this.bar.destroy();
}
}

@ -0,0 +1,9 @@
class Missile extends Phaser.GameObjects.Image {
constructor(scene, frame, width, height) {
super(scene, 0, 0, frame);
this.visible = false;
this.setOrigin(0.5, 0.5);
this.setScale(0.02);
}
}

@ -7,8 +7,13 @@ class Robot {
this.color = color; this.color = color;
this.range = WIDTH_MAP / 2; this.range = WIDTH_MAP / 2;
this.damage = DAMAGE; this.damage = DAMAGE;
this.life = LIFE; this.life = new HealthBar(scene, this.width * 2, this.width / 3, this.x, this.y - this.width, LIFE, 0x008000);
this.shield = SHIELD; this.shield = new HealthBar(scene, this.width * 2, this.width / 3, this.x, this.y - this.width * 1.5, SHIELD, 0x0000FF);
this.canAttack = true;
this.missile = new Missile(scene, 'bullet', this.width / 5, this.width / 4);
scene.add.existing(this.missile);
this.addScene(scene); this.addScene(scene);
} }
@ -20,21 +25,25 @@ class Robot {
setX(x) { setX(x) {
this.x = x; this.x = x;
this.circle.setX(this.x); this.circle.setX(this.x);
this.life.setX(this.x);
this.shield.setX(this.x);
} }
setY(y) { setY(y) {
this.y = y; this.y = y;
this.life.setY(this.y - this.width);
this.shield.setY(this.y - this.width * 1.5);
this.circle.setY(this.y); this.circle.setY(this.y);
} }
setTarget(target) { setTarget(target) {
this.target = target; this.target = target;
if (this.target != null) this.updateTarget();
this.updateTarget();
} }
updateTarget() { updateTarget() {
this.setTargetPos(this.target.x, this.target.y); if (this.verifyTarget())
this.setTargetPos(this.target.x, this.target.y);
} }
setTargetPos(x, y) { setTargetPos(x, y) {
@ -46,75 +55,120 @@ class Robot {
} }
advanceToTarget() { advanceToTarget() {
if ((this.velocityX >= 0 && this.x >= this.target.x) || (this.velocityX <= 0 && this.x <= this.target.x)) if (this.verifyTarget()) {
this.setX(this.target.x); if ((this.velocityX >= 0 && this.x >= this.target.x) || (this.velocityX <= 0 && this.x <= this.target.x))
else this.setX(this.target.x);
this.setX(this.x + this.velocityX); else
this.setX(this.x + this.velocityX);
if ((this.velocityY >= 0 && this.y >= this.target.y) || (this.velocityY <= 0 && this.y <= this.target.y))
this.setY(this.target.y); if ((this.velocityY >= 0 && this.y >= this.target.y) || (this.velocityY <= 0 && this.y <= this.target.y))
else this.setY(this.target.y);
this.setY(this.y + this.velocityY); else
this.setY(this.y + this.velocityY);
}
} }
fleeFromTarget() { fleeFromTarget() {
if (this.velocityX >= 0 && this.x - this.width / 2 <= 0) if (this.verifyTarget()) {
this.setX(this.width / 2); if (this.velocityX >= 0 && this.x - this.width / 2 <= 0)
else if (this.velocityX <= 0 && this.x + this.width / 2 >= WIDTH_MAP) this.setX(this.width / 2);
this.setX(WIDTH_MAP - this.width / 2); else if (this.velocityX <= 0 && this.x + this.width / 2 >= WIDTH_MAP)
else this.setX(WIDTH_MAP - this.width / 2);
this.setX(this.x - this.velocityX); else
this.setX(this.x - this.velocityX);
if (this.velocityY >= 0 && this.y - this.width / 2 <= 0)
this.setY(this.height / 2); if (this.velocityY >= 0 && this.y - this.width / 2 <= 0)
else if (this.velocityY <= 0 && this.y + this.width / 2 >= WIDTH_MAP) this.setY(this.height / 2);
this.setY(WIDTH_MAP - this.height / 2); else if (this.velocityY <= 0 && this.y + this.width / 2 >= WIDTH_MAP)
else this.setY(WIDTH_MAP - this.height / 2);
this.setY(this.y - this.velocityY); else
this.setY(this.y - this.velocityY);
}
} }
attackTarget() { attackTarget() {
if (this.isTargetInRange()) { if (this.verifyTarget() && this.canAttack && this.target.isAlive() && this.isTargetInRange()) {
this.attack(this.target); this.attack(this.target);
return true;
} }
return false; return false;
} }
verifyTarget() {
if (this.haveTarget()) {
if (!this.target.isAlive()) {
this.setTarget(null);
return false;
}
return true;
}
return false;
}
attack(target) { attack(target) {
this.canAttack = false;
this.missile.setPosition(this.x, this.y).setVisible(true);
this.missile.setRotation(Math.atan2(this.y - this.target.y, this.x - this.target.x) - 3.14 / 2);
this.scene.tweens.add({
targets: this.missile,
x: target.x,
y: target.y,
ease: 'Linear',
duration: 200,
onComplete: () => this.finishAttack(target)
});
this.scene.time.addEvent({delay: Phaser.Math.Between(1000, 3000), callback: this.reload, callbackScope: this});
}
finishAttack(target) {
if (target.beAttack(this.damage)) { if (target.beAttack(this.damage)) {
this.setTarget(null); this.setTarget(null);
} }
this.missile.setVisible(false);
}
reload() {
this.canAttack = true;
} }
beAttack(damage) { beAttack(damage) {
this.shield = this.shield - damage; let diff = this.shield.decrease(damage)
if (this.shield < 0) { if (diff > 0) {
this.life += this.shield; console.log("Shield Broken");
this.shield = 0; this.life.decrease(diff);
}
if (this.life <= 0) {
this.life = 0;
} }
return this.die(); return this.die();
} }
isTargetInRange() { isTargetInRange() {
let hypot = Math.hypot(this.target.x - this.x, this.target.y - this.y); if (this.verifyTarget()) {
return hypot < this.range; let hypot = Math.hypot(this.target.x - this.x, this.target.y - this.y);
return hypot < this.range;
}
return false;
} }
isAlive() { isAlive() {
return this.life > 0; return this.life.value > 0;
} }
die() { die() {
if (this.life === 0) { if (this.life.value === 0) {
this.circle.destroy(); this.circle.destroy();
this.shield.destroy();
this.life.destroy();
return true; return true;
} }
return false; return false;
} }
haveTarget() {
return this.target != null;
}
} }

Loading…
Cancel
Save