La vitesse de déplacement est maintenant la meme dans toute les directions possibles. Les robots ne peuvent pas sortir de la carte. Ajout des mouvemenet avancer et reculer d'une cible. Ajout des distcances. Ajout des attaques si l'objectif est dans la range du robot. Ajout de point de vie, de bouclier, de dégat.

master
clmaisonha 5 years ago
parent 237207c495
commit 58863ea587

@ -1 +1 @@
var EnemyBots=new Array(5),myself=new Robot(5,5);EnemyBots.push(new Robot(1,1)),console.log(myself.height);const WIDTH_WINDOW=$(document).width()-30,HEIGHT_WINDOW=$(document).height()-30;var config={type:Phaser.AUTO,width:WIDTH_WINDOW,height:HEIGHT_WINDOW,parent:"all",backgroundColor:"#35363A"},game=new Phaser.Game(config);game.scene.add("Boot",Boot),game.scene.add("Type",Type),game.scene.add("Game",new Game(game)),game.scene.start("Boot");
const WIDTH_WINDOW=$(document).width()-30,HEIGHT_WINDOW=$(document).height()-30,WIDTH_MAP=WIDTH_WINDOW<HEIGHT_WINDOW?.8*WIDTH_WINDOW:.8*HEIGHT_WINDOW,SPEED=2,LIFE=100,DAMAGE=40,SHIELD=100,RANGE=WIDTH_MAP/2;var config={type:Phaser.AUTO,width:WIDTH_WINDOW,height:HEIGHT_WINDOW,parent:"all",backgroundColor:"#35363A"},game=new Phaser.Game(config);game.scene.add("Boot",Boot),game.scene.add("Type",Type),game.scene.add("Game",new Game(game)),game.scene.start("Boot");

File diff suppressed because one or more lines are too long

@ -1,9 +1,18 @@
var EnemyBots = new Array(5);
var myself = new Robot(5, 5);
/*var EnemyBots = new Array(5);
//var myself = new Robot(5, 5);
EnemyBots.push(new Robot(1, 1));
console.log(myself.height);
*/
const WIDTH_WINDOW = $(document).width() - 30;
const HEIGHT_WINDOW = $(document).height() - 30;
const WIDTH_MAP = WIDTH_WINDOW < HEIGHT_WINDOW ? WIDTH_WINDOW * 0.8 : HEIGHT_WINDOW * 0.8;
const SPEED = 2;
const LIFE = 100;
const DAMAGE = 40;
const SHIELD = 100;
const RANGE = WIDTH_MAP / 2;
var config = {
type: Phaser.AUTO,

@ -4,33 +4,45 @@ class GamingBoard extends Phaser.Scene {
}
preload() {
if (WIDTH_WINDOW < HEIGHT_WINDOW)
this.width = WIDTH_WINDOW * 0.8;
else
this.width = HEIGHT_WINDOW * 0.8;
this.cameras.main.setViewport(WIDTH_WINDOW / 10, HEIGHT_WINDOW / 10, this.width, this.width);
this.cameras.main.setViewport(WIDTH_WINDOW / 10, HEIGHT_WINDOW / 10, WIDTH_MAP, WIDTH_MAP);
}
create() {
this.add.image(0, 0, 'background').alpha = 0.1;
this.enemy = this.add.circle(this.width * 0.1, this.width * 0.1, this.width * 0.05, 0x6666ff);
this.myself = this.add.circle(this.width * 0.9, this.width * 0.9, this.width * 0.05, 0xff33cc);
this.enemy = new Robot(WIDTH_MAP * 0.1, WIDTH_MAP * 0.1, WIDTH_MAP * 0.9, WIDTH_MAP * 0.9, 0x6666ff, this);
//this.myself = this.add.circle(this.width * 0.1, this.width * 0.1, this.width * 0.05, 0x6666ff);
this.myself = 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.myself.setTarget(this.enemy);
this.myself2.setTarget(this.enemy);
this.events.on('resume', () => this.resume());
}
update(time, delta) {
super.update(time, delta);
if (this.enemy.x < this.myself.x)
this.myself.setX(this.myself.x - 0.5);
else if (-0.5 < this.enemy.x - this.myself.x < 0.5)
this.myself.setX(this.myself.x + 0.5);
if (this.enemy.y < this.myself.y)
this.myself.setY(this.myself.y - 0.5);
else if (-0.5 < this.enemy.y - this.myself.y < 0.5)
this.myself.setY(this.myself.y - 0.5);
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()) {
if (!this.myself2.isTargetInRange())
this.myself2.advanceToTarget();
else
this.myself2.attackTarget();
console.log(this.enemy.life);
}
}
resume() {
this.myself2.updateTarget();
}
}

@ -1,6 +1,120 @@
class Robot {
constructor(height, width) {
this.height = height;
this.width = width;
}
constructor(height, width, posX, posY, color, scene) {
this.height = height;
this.width = width;
this.x = posX;
this.y = posY;
this.color = color;
this.range = WIDTH_MAP / 2;
this.damage = DAMAGE;
this.life = LIFE;
this.shield = SHIELD;
this.addScene(scene);
}
addScene(scene) {
this.scene = scene;
this.circle = this.scene.add.circle(this.x, this.y, this.width / 2, this.color);
}
setX(x) {
this.x = x;
this.circle.setX(this.x);
}
setY(y) {
this.y = y;
this.circle.setY(this.y);
}
setTarget(target) {
this.target = target;
if (this.target != null)
this.updateTarget();
}
updateTarget() {
this.setTargetPos(this.target.x, this.target.y);
}
setTargetPos(x, y) {
let diffX = this.target.x - this.x;
let diffY = this.target.y - this.y;
let hypot = Math.hypot(diffX, diffY);
this.velocityX = diffX / hypot * WIDTH_MAP / 1000 * SPEED;
this.velocityY = diffY / hypot * WIDTH_MAP / 1000 * SPEED;
}
advanceToTarget() {
if ((this.velocityX >= 0 && this.x >= this.target.x) || (this.velocityX <= 0 && this.x <= this.target.x))
this.setX(this.target.x);
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);
else
this.setY(this.y + this.velocityY);
}
fleeFromTarget() {
if (this.velocityX >= 0 && this.x - this.width / 2 <= 0)
this.setX(this.width / 2);
else if (this.velocityX <= 0 && this.x + this.width / 2 >= WIDTH_MAP)
this.setX(WIDTH_MAP - this.width / 2);
else
this.setX(this.x - this.velocityX);
if (this.velocityY >= 0 && this.y - this.width / 2 <= 0)
this.setY(this.height / 2);
else if (this.velocityY <= 0 && this.y + this.width / 2 >= WIDTH_MAP)
this.setY(WIDTH_MAP - this.height / 2);
else
this.setY(this.y - this.velocityY);
}
attackTarget() {
if (this.isTargetInRange()) {
this.attack(this.target);
}
return false;
}
attack(target) {
if (target.beAttack(this.damage)) {
this.setTarget(null);
}
}
beAttack(damage) {
this.shield = this.shield - damage;
if (this.shield < 0) {
this.life += this.shield;
this.shield = 0;
}
if (this.life <= 0) {
this.life = 0;
}
return this.die();
}
isTargetInRange() {
let hypot = Math.hypot(this.target.x - this.x, this.target.y - this.y);
return hypot < this.range;
}
isAlive() {
return this.life > 0;
}
die() {
if (this.life === 0) {
this.circle.destroy();
return true;
}
return false;
}
}

Loading…
Cancel
Save