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.
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,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…
Reference in new issue