Création d'un cube déplaçable - Création des nodes : Les actions se font de manière dynamique sur le robot - si la première action n'aboutit pas, la deuxième action est effectuée - Création des conditions - Pour le moment, si le robot passe en dessous de la condition de bouclier, il effectue les action qui lui sont liés (Ex : reculer)

master
clmaisonha 5 years ago
parent f0ea863610
commit 9ba17166a7

File diff suppressed because one or more lines are too long

@ -0,0 +1,12 @@
class Attack {
constructor() {
}
do(robot) {
if (robot.haveTarget() && robot.isTargetInRange()) {
return robot.attackTarget();
}
return false;
}
}

@ -0,0 +1,60 @@
class Condition {
constructor(target, shieldFilter = false, rangeFilter = false, shield, range) { //shieldFilter : true/false - rangeFilter : true/false
this.shieldFilter = shieldFilter;
this.rangeFilter = rangeFilter;
this.shield = shield;
this.range = range;
this.target = target;
this.lNode = [];
}
addNode(node) {
this.lNode.push(node);
}
do(robot) {
if (this.doCondition(robot)) {
for (let i = 0; i < this.lNode.length; i++) {
if (this.lNode[i].do(robot)) {
return true;
}
}
}
return false;
}
doCondition(robot) {
if (this.shieldFilter) {
if (this.shieldCondition()) {
if (this.rangeFilter) {
return this.rangeCondition(robot);
}
return true;
}
} else if (this.rangeFilter) {
return this.rangeCondition(robot);
}
return false;
}
rangeCondition(robot) {
return robot.calcDistance(this.target) <= this.range;
}
shieldCondition() {
if (this.target.shield.value / this.target.shield.valueMax <= this.shield) {
return true;
}
return false;
}
moveToward(robot) {
return robot.advanceToTarget();
}
fleeFrom(robot) {
return robot.fleeFromTarget();
}
}

@ -6,7 +6,7 @@ class Game extends Phaser.Scene {
preload() {
this.gm = new GamingBoard();
this.game.scene.add('GamingBoard',this.gm);
this.game.scene.add('GamingBoard', this.gm);
}
create() {
@ -19,10 +19,26 @@ class Game extends Phaser.Scene {
btn.on('pointerdown', () => this.clickPlay(btn));
this.scene.launch('GamingBoard');
this.scene.pause('GamingBoard');
let rect = this.add.rectangle(WIDTH_WINDOW / 2, 500, 100, 100, 0xffffff);
rect.setInteractive();
this.input.setDraggable(rect);
this.input.on('drag', this.doDrag);
}
clickPlay(btn){
if(this.scene.isPaused('GamingBoard'))
startDrag(pointer, targets) {
this.input.off('pointerdown', this.startDrag, this);
this.dragObj = targets[0];
this.input.on('pointermove', this.doDrag, this);
this.input.on('pointerup', this.stopDrag, this);
}
doDrag(pointer, target, dragX, dragY) {
target.x = dragX;
target.y = dragY;
}
clickPlay(btn) {
if (this.scene.isPaused('GamingBoard'))
this.scene.resume('GamingBoard');
else
this.scene.pause('GamingBoard');

@ -12,7 +12,7 @@ class GamingBoard extends Phaser.Scene {
this.listRobot = [];
this.listRobot.push(new Robot(WIDTH_MAP * 0.1, WIDTH_MAP * 0.1, WIDTH_MAP * 0.9, WIDTH_MAP * 0.9, 0x6666ff, 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.listRobot.push(new Robot(WIDTH_MAP * 0.1, WIDTH_MAP * 0.1, WIDTH_MAP * 0.2, 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.9, 0xffffff, this));
for (let i = 0; i < this.listRobot.length; i++) {
@ -26,12 +26,7 @@ class GamingBoard extends Phaser.Scene {
super.update(time, delta);
this.listRobot.forEach(function (robot) {
if (robot.haveTarget()) {
if (!robot.isTargetInRange())
robot.advanceToTarget();
else
robot.attackTarget();
}
robot.read();
})
for (let i = 0; i < this.listRobot.length; i++) {

@ -0,0 +1,24 @@
class Move {
constructor(toward) { //true = move toward / false = flee from
this.toward = toward;
}
do(robot) {
if (robot.haveTarget()) {
if (this.toward === true) {
return this.moveToward(robot);
} else {
return this.fleeFrom(robot);
}
}
return false;
}
moveToward(robot) {
return robot.advanceToTarget();
}
fleeFrom(robot) {
return robot.fleeFromTarget();
}
}

@ -15,6 +15,14 @@ class Robot {
scene.add.existing(this.missile);
this.addScene(scene);
this.lNode = [];
let condition = new Condition(this, true, false, 0.5, 0);
condition.addNode(new Move(false));
this.addNode(condition);
this.addNode(new Attack());
this.addNode(new Move(true));
}
addScene(scene) {
@ -65,7 +73,9 @@ class Robot {
this.setY(this.target.y);
else
this.setY(this.y + this.velocityY);
return true;
}
return false;
}
fleeFromTarget() {
@ -83,12 +93,15 @@ class Robot {
this.setY(WIDTH_MAP - this.height / 2);
else
this.setY(this.y - this.velocityY);
return true;
}
return false;
}
attackTarget() {
if (this.verifyTarget() && this.canAttack && this.target.isAlive() && this.isTargetInRange()) {
if (this.verifyTarget() && this.target.isAlive() && this.isTargetInRange()) {
if (this.canAttack)
this.attack(this.target);
return true;
}
@ -103,7 +116,6 @@ class Robot {
}
return true;
}
return false;
}
@ -148,12 +160,15 @@ class Robot {
isTargetInRange() {
if (this.verifyTarget()) {
let hypot = Math.hypot(this.target.x - this.x, this.target.y - this.y);
return hypot < this.range;
return this.calcDistance(this.target) < this.range;
}
return false;
}
calcDistance(robot) {
return Math.hypot(robot.x - this.x, robot.y - this.y);
}
isAlive() {
return this.life.value > 0;
}
@ -171,4 +186,16 @@ class Robot {
haveTarget() {
return this.target != null;
}
read() {
for (let i = 0; i < this.lNode.length; i++) {
if (this.lNode[i].do(this)) {
break;
}
}
}
addNode(node) {
this.lNode.push(node);
}
}

Loading…
Cancel
Save