Création du premier bonus (SpeedBonus) - Implémentation du bonus sur la carte - Implémentation de l'action aller vers le bonus pour le robot - Lorsque deux robots sont les uns sur les autres, l'attaque est possible. Par contre, les déplacments pour fuir ou pour avancer sont impossible

master
clmaisonha 5 years ago
parent 66c292a919
commit c1509d8ccb

@ -1 +1 @@
function selectWord(e,t){switch(LANG){case"fr":return t;default:return e}}function newGame(e){-1!==e.scene.getIndex("Game")&&e.scene.remove("Game"),e.scene.add("Game",new Game(e))}var LANG=navigator.language.substr(0,2).toLowerCase();const MYSELF="myself",SHIELD_PER_SECOND=5,COLOR_ATTACK=16711680,COLOR_MOVE=255,COLOR_CONDITION=921102,DURATION_DAMAGE=700,WIDTH_WINDOW=$(document).width()-20,HEIGHT_WINDOW=$(document).height()-20,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"};let father=new Phaser.Game(config);father.scene.add("Boot",new Boot(father)),father.scene.start("Boot");
function selectWord(e,t){switch(LANG){case"fr":return t;default:return e}}function newGame(e){-1!==e.scene.getIndex("Game")&&e.scene.remove("Game"),e.scene.add("Game",new Game(e))}function chooseTarget(e,t){let n=2*WIDTH_MAP,a=[];return t.forEach(function(t){if(t!==e){let o=Math.hypot(t.x-e.x,t.y-e.y);n>=o&&(n>o&&(n=o,a=[]),a.push(t))}}),a.length>1?a[Math.floor(Math.random()*(a.length+1))]:a[0]}var listBonus=[],LANG=navigator.language.substr(0,2).toLowerCase();const MYSELF="myself",SHIELD_PER_SECOND=5,COLOR_ATTACK=16711680,COLOR_MOVE=255,COLOR_CONDITION=921102,DURATION_DAMAGE=700,WIDTH_WINDOW=$(document).width()-20,HEIGHT_WINDOW=$(document).height()-20,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"};let father=new Phaser.Game(config);father.scene.add("Boot",new Boot(father)),father.scene.start("Boot");

File diff suppressed because one or more lines are too long

@ -1 +1 @@
class AddNode extends Phaser.Scene{constructor(e,t,n){super("AddNode"),this.father=e,this.gameR=t,this.selected=n,this.lCategory=[]}create(e){let t=HEIGHT_WINDOW/(4/3);this.add.rectangle(0,0,WIDTH_WINDOW,HEIGHT_WINDOW,0).setOrigin(0,0).setAlpha(.5),this.add.rectangle(0,HEIGHT_WINDOW/2,WIDTH_WINDOW,t,3487290).setOrigin(0,.5),this.createButtonCancel(),this.createButtonAdd(e)}addButton(e,t,n,s,i,r=new Category,o){let a=this.createButton(e,t,n,s,i);return a.on("pointerdown",()=>this.click(a,r,o)),a}createButton(e,t,n,s,i){let r=this.add.image(e,t,i).setOrigin(.5,.5).setInteractive();return r.on("pointerover",()=>r.setFrame(1)),r.on("pointerout",()=>r.setFrame(0)),r.displayHeight=s,null===n?r.scaleX=r.scaleY:r.displayWidth=n,r}click(e,t,n){e.isTinted?t.remove(e):t.add(e,n)}newCategory(e,t,n){let s=new Category(e,t,n);return this.lCategory.push(s),s}addTitle(e,t,n){let s={font:(HEIGHT_WINDOW/12).toString()+"px stencil",fill:"#e2e2e2"};this.add.text(e,t,n,s).setOrigin(.5,.5)}createButtonCancel(){this.createButton(WIDTH_WINDOW/3,HEIGHT_WINDOW/(16/15),null,HEIGHT_WINDOW/9,"cancel").on("pointerdown",()=>this.cancel())}createButtonAdd(e){this.createButton(WIDTH_WINDOW/1.5,HEIGHT_WINDOW/(16/15),null,HEIGHT_WINDOW/9,"add").on("pointerdown",()=>e.addNode())}verifyCategory(){for(let e=0;e<this.lCategory.length;e++)if(!this.lCategory[e].validate())return!1;return!0}cancel(){this.scene.resume("Game"),this.scene.stop("AddNode"),this.father.scene.remove("AddNode")}}class Finish extends Phaser.Scene{constructor(e){super("Finish"),this.father=e}create(){this.add.rectangle(0,0,WIDTH_WINDOW,HEIGHT_WINDOW,0).setOrigin(0,0).setAlpha(.5),this.add.rectangle(0,HEIGHT_WINDOW/2,WIDTH_WINDOW,HEIGHT_WINDOW/2,3487290).setOrigin(0,.5)}addTitle(e){let t={font:(HEIGHT_WINDOW/6).toString()+"px stencil",fill:"#e2e2e2"};this.add.text(WIDTH_WINDOW/2,HEIGHT_WINDOW/2,e,t).setOrigin(.5,.5)}createButton(e,t){let n=this.add.image(e,HEIGHT_WINDOW/8*7,t).setOrigin(.5,.5).setInteractive();return n.on("pointerover",()=>n.setFrame(1)),n.on("pointerout",()=>n.setFrame(0)),n.displayHeight=HEIGHT_WINDOW/9,n.scaleX=n.scaleY,n}btnRetry(e){e.on("pointerdown",()=>this.retry())}retry(){console.log("RETRY"),this.close(),newGame(this.father),this.father.scene.start("Game")}btnHome(e){e.on("pointerdown",()=>this.home())}home(){console.log("HOME"),this.close(),this.father.scene.start("Type")}btnNext(e){e.on("pointerdown",()=>this.next())}next(){console.log("NEXT")}close(){this.sound.stopAll(),this.father.scene.remove("GamingBoard"),this.father.scene.remove("Game"),this.father.scene.remove("Finish")}}
class AddNode extends Phaser.Scene{constructor(e,t,s){super("AddNode"),this.father=e,this.gameR=t,this.selected=s,this.lCategory=[]}create(e){let t=HEIGHT_WINDOW/(4/3);this.add.rectangle(0,0,WIDTH_WINDOW,HEIGHT_WINDOW,0).setOrigin(0,0).setAlpha(.5),this.add.rectangle(0,HEIGHT_WINDOW/2,WIDTH_WINDOW,t,3487290).setOrigin(0,.5),this.createButtonCancel(),this.createButtonAdd(e)}addButton(e,t,s,n,i,r=new Category,a){let o=this.createButton(e,t,s,n,i);return o.on("pointerdown",()=>this.click(o,r,a)),o}createButton(e,t,s,n,i){let r=this.add.image(e,t,i).setOrigin(.5,.5).setInteractive();return r.on("pointerover",()=>r.setFrame(1)),r.on("pointerout",()=>r.setFrame(0)),r.displayHeight=n,null===s?r.scaleX=r.scaleY:r.displayWidth=s,r}click(e,t,s){e.isTinted?t.remove(e):t.add(e,s)}newCategory(e,t,s){let n=new Category(e,t,s);return this.lCategory.push(n),n}addTitle(e,t,s){let n={font:(HEIGHT_WINDOW/12).toString()+"px stencil",fill:"#e2e2e2"};this.add.text(e,t,s,n).setOrigin(.5,.5)}createButtonCancel(){this.createButton(WIDTH_WINDOW/3,HEIGHT_WINDOW/(16/15),null,HEIGHT_WINDOW/9,"cancel").on("pointerdown",()=>this.cancel())}createButtonAdd(e){this.createButton(WIDTH_WINDOW/1.5,HEIGHT_WINDOW/(16/15),null,HEIGHT_WINDOW/9,"add").on("pointerdown",()=>e.addNode())}verifyCategory(){for(let e=0;e<this.lCategory.length;e++)if(!this.lCategory[e].validate())return!1;return!0}cancel(){this.scene.resume("Game"),this.scene.stop("AddNode"),this.father.scene.remove("AddNode")}}class Bonus extends Phaser.GameObjects.Image{constructor(e,t,s,n,i,r){super(t,s,n,i),this.setOrigin(.5,.5),t.add.existing(this),this.name=e,this.value=r,this.scene=t}}class Finish extends Phaser.Scene{constructor(e){super("Finish"),this.father=e}create(){this.add.rectangle(0,0,WIDTH_WINDOW,HEIGHT_WINDOW,0).setOrigin(0,0).setAlpha(.5),this.add.rectangle(0,HEIGHT_WINDOW/2,WIDTH_WINDOW,HEIGHT_WINDOW/2,3487290).setOrigin(0,.5)}addTitle(e){let t={font:(HEIGHT_WINDOW/6).toString()+"px stencil",fill:"#e2e2e2"};this.add.text(WIDTH_WINDOW/2,HEIGHT_WINDOW/2,e,t).setOrigin(.5,.5)}createButton(e,t){let s=this.add.image(e,HEIGHT_WINDOW/8*7,t).setOrigin(.5,.5).setInteractive();return s.on("pointerover",()=>s.setFrame(1)),s.on("pointerout",()=>s.setFrame(0)),s.displayHeight=HEIGHT_WINDOW/9,s.scaleX=s.scaleY,s}btnRetry(e){e.on("pointerdown",()=>this.retry())}retry(){console.log("RETRY"),this.close(),newGame(this.father),this.father.scene.start("Game")}btnHome(e){e.on("pointerdown",()=>this.home())}home(){console.log("HOME"),this.close(),this.father.scene.start("Type")}btnNext(e){e.on("pointerdown",()=>this.next())}next(){console.log("NEXT")}close(){this.sound.stopAll(),this.father.scene.remove("GamingBoard"),this.father.scene.remove("Game"),this.father.scene.remove("Finish")}}

@ -14,6 +14,30 @@ function newGame(father) {
father.scene.add('Game', new Game(father));
}
function chooseTarget(robot, list) {
let minDist = WIDTH_MAP * 2;
let l = [];
list.forEach(function (item) {
if (item !== robot) {
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 (l.length > 1) {
return l[Math.floor(Math.random() * (l.length + 1))];
} else {
return l[0];
}
}
var listBonus = [];
var LANG = navigator.language.substr(0, 2).toLowerCase();
const MYSELF = 'myself';
const SHIELD_PER_SECOND = 5;

@ -5,7 +5,7 @@ class BonusShield extends Bonus {
useOn(robot) {
robot.shield.valueMax = robot.shield.valueMax + this.value;
robot.shield.value = robot.shield.value + this.value;
robot.shield.value = robot.shield.value + this.val
this.destroy();
}
}

@ -1,13 +1,12 @@
class BonusSpeed extends Bonus {
constructor(name, scene, x, y, value, time) {
super(name, scene, x, y, 'bonusSpeed', value);
super(name, scene, x, y, 'play', value);
this.time = time;
}
useOn(robot) {
let oldSpeed = robot.speed;
robot.setSpeed(oldSpeed + this.value);
this.scene.time.addEvent({delay: this.time, callback: robot.setSpeed(oldSpeed)});
robot.increaseSpeed(this.value);
this.scene.time.addEvent({delay: this.time, callback: () => robot.decreaseSpeed(this.value)});
this.destroy();
}
}

@ -20,7 +20,8 @@ class Game extends Phaser.Scene {
let centerX = WIDTH_WINDOW / 4 * 3;
let diffX = WIDTH_WINDOW / 10;
this.tree = new RectangleNode(centerX, HEIGHT_WINDOW / 10, this, 'waria');
this.tree.addRect(new RectangleNode(centerX + diffX, HEIGHT_WINDOW / 3, this, 'move', true))
this.tree.addRect(new RectangleNode(centerX - 2 * diffX, HEIGHT_WINDOW / 3, this, 'move', true, false));
this.tree.addRect(new RectangleNode(centerX + diffX, HEIGHT_WINDOW / 3, this, 'move', true));
this.tree.addRect(new RectangleNode(centerX, HEIGHT_WINDOW / 3, this, 'attack', 1 / 3));

@ -2,22 +2,24 @@ class GamingBoard extends Phaser.Scene {
constructor(father) {
super('GamingBoard');
this.listRobot = [];
listBonus = [];
this.father = father;
this.end = false;
}
preload() {
this.cameras.main.setViewport(WIDTH_WINDOW / 10, HEIGHT_WINDOW / 10, WIDTH_MAP, WIDTH_MAP);
this.sound.add('shot');
this.sound.add('hit');
}
create() {
this.end = false;
this.sound.add('shot');
this.sound.add('hit');
listBonus.push(new BonusSpeed('Balle Argent', this, WIDTH_MAP / 2, WIDTH_MAP / 2, 3, 2000));
this.add.image(0, 0, 'background').alpha = 0.1;
for (let i = 0; i < this.listRobot.length; i++) {
this.chooseTarget(this.listRobot[i]);
this.listRobot[i].setTarget(chooseTarget(this.listRobot[i], this.listRobot));
}
this.listRobot[0].drawRange();
@ -29,7 +31,6 @@ class GamingBoard extends Phaser.Scene {
update(time, delta) {
super.update(time, delta);
this.listRobot.forEach(function (robot) {
robot.read();
})
@ -40,9 +41,7 @@ class GamingBoard extends Phaser.Scene {
i -= 1;
} else {
if (!this.listRobot[i].haveTarget()) {
this.chooseTarget(this.listRobot[i]);
} else {
this.listRobot[i].updateTarget();
this.listRobot[i].setTarget(chooseTarget(this.listRobot[i], this.listRobot));
}
}
}
@ -54,7 +53,6 @@ class GamingBoard extends Phaser.Scene {
}
finish() {
console.log("QUOI ?");
this.pause();
this.father.finish();
}
@ -88,28 +86,6 @@ class GamingBoard extends Phaser.Scene {
return false;
}
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]);
}
}
upShield() {
this.listRobot.forEach(robot => robot.addShield(SHIELD_PER_SECOND));

@ -1,24 +1,43 @@
class Move {
constructor(toward) { //true = move toward / false = flee from
constructor(toward, enemyBot = true) { //true = move toward / false = flee from
this.toward = toward;
this.enemyBot = enemyBot;
}
do(robot) {
if (robot.haveTarget()) {
if (this.toward === true) {
return this.moveToward(robot);
} else {
return this.fleeFrom(robot);
if (this.enemyBot) {
if (robot.verifyTarget()) {
if (this.toward === true) {
return this.moveToward(robot);
} else {
return this.fleeFrom(robot);
}
}
} else {
let bonusTarget = chooseTarget(robot, listBonus);
if (bonusTarget !== undefined) {
let ret;
if (this.toward === true) {
ret = this.moveToward(robot, bonusTarget);
} else {
ret = this.fleeFrom(robot, bonusTarget);
}
if (!ret) {
bonusTarget.useOn(robot);
listBonus.splice(listBonus.indexOf(bonusTarget), 1);
}
return true;
}
}
return false;
}
moveToward(robot) {
return robot.advanceToTarget();
moveToward(robot, target = robot.target) {
return robot.advanceTo(target);
}
fleeFrom(robot) {
return robot.fleeFromTarget();
fleeFrom(robot, target = robot.target) {
return robot.fleeFrom(target);
}
}

@ -11,7 +11,7 @@ class RectangleNode {
break;
case 'move' :
this.rect = new Phaser.GameObjects.Image(scene, x, y, 'moveNode');
this.node = new Move(option[0]);
this.node = new Move(option[0], option[1]);
this.canAddNode = false;
this.line = scene.add.graphics();
if (option[0] === true) {

@ -64,7 +64,6 @@ class Robot {
setTarget(target) {
this.target = target;
this.updateTarget();
}
updateTarget() {
@ -73,57 +72,91 @@ class Robot {
}
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.speed;
this.velocityY = diffY / hypot * WIDTH_MAP / 1000 * SPEED * this.speed;
}
setSpeed(speed) {
this.speed = speed;
this.updateTarget();
}
increaseSpeed(amount) {
this.speed += amount;
}
decreaseSpeed(amount) {
this.speed -= amount;
}
advanceToTarget() {
if (this.verifyTarget()) {
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);
this.advanceTo(this.target);
}
return false;
}
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);
return true;
advanceTo(target) {
if (this.target.x !== this.x || this.target.y !== this.y) {
let x = false;
let y = false;
let velocity = this.getVelocity(target);
let newX = velocity[0] + this.x;
let newY = velocity[1] + this.y;
if ((velocity[0] >= 0 && newX >= target.x) || (velocity[0] <= 0 && newX <= target.x)) {
this.setX(target.x);
x = true;
} else
this.setX(this.x + velocity[0]);
if ((velocity[1] >= 0 && newY >= target.y) || (velocity[1] <= 0 && newY <= target.y)) {
this.setY(target.y);
y = true;
} else
this.setY(this.y + velocity[1]);
return !(x && y);
}
return false;
}
fleeFromTarget() {
if (this.verifyTarget()) {
if (this.velocityX >= 0 && this.x - this.width / 2 <= 0)
return this.fleeFrom(this.target)
}
return false;
}
fleeFrom(target) {
if (this.target.x !== this.x || this.target.y !== this.y) {
let velocity = this.getVelocity(target);
if (velocity[0] >= 0 && this.x - this.width / 2 <= 0)
this.setX(this.width / 2);
else if (this.velocityX <= 0 && this.x + this.width / 2 >= WIDTH_MAP)
else if (velocity[0] <= 0 && this.x + this.width / 2 >= WIDTH_MAP)
this.setX(WIDTH_MAP - this.width / 2);
else
this.setX(this.x - this.velocityX);
this.setX(this.x - velocity[0]);
if (this.velocityY >= 0 && this.y - this.width / 2 <= 0)
if (velocity[1] >= 0 && this.y - this.width / 2 <= 0)
this.setY(this.height / 2);
else if (this.velocityY <= 0 && this.y + this.width / 2 >= WIDTH_MAP)
else if (velocity[1] <= 0 && this.y + this.width / 2 >= WIDTH_MAP)
this.setY(WIDTH_MAP - this.height / 2);
else
this.setY(this.y - this.velocityY);
this.setY(this.y - velocity[1]);
return true;
}
return false;
}
getVelocity(target) {
let velocity = [];
let diffX = target.x - this.x;
let diffY = target.y - this.y;
let hypot = Math.hypot(diffX, diffY);
velocity[0] = diffX / hypot * WIDTH_MAP / 1000 * SPEED * this.speed;
velocity[1] = diffY / hypot * WIDTH_MAP / 1000 * SPEED * this.speed;
return velocity;
}
attackTarget(percentBonus = 0) {
if (this.verifyTarget() && this.target.isAlive() && this.isTargetInRange()) {
if (this.verifyTarget()) {
if (this.canAttack)
this.attack(this.target, percentBonus);
return true;
@ -203,7 +236,7 @@ class Robot {
}
isTargetInRange(percentRange = 1) {
if (this.verifyTarget()) {
if (this.haveTarget()) {
return this.calcDistance(this.target) < this.range * percentRange;
}
return false;

@ -1,7 +1,10 @@
class Bonus extends Phaser.GameObjects.Image {
constructor(name, scene, x, y, texture, value) {
super(scene, x, y, texture);
this.setOrigin(0.5, 0.5);
scene.add.existing(this);
this.name = name;
this.value = value;
this.scene = scene;
}
}
Loading…
Cancel
Save