Skip to content

Commit

Permalink
optimise islands
Browse files Browse the repository at this point in the history
  • Loading branch information
codergautam committed Jul 11, 2022
1 parent f38d4eb commit 762a9de
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 45 deletions.
69 changes: 66 additions & 3 deletions classes/Island.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,41 @@ module.exports = class Island {
this.canBeCaptured = canBeCaptured;
this.capturedBy = "none";
this.captureState = 0;

this.destroyingBy = "none";
// state 0 = not capturing
// state 1 = capturing
// state 2 = captured
// state 4 = destroying
this.capturingBy = 0.1;
this.capturingBy = "none";
this.lastPepperGrew = Date.now();
this.id = idgen();
this.capturedPercentage = 0;


this.lastSendWhat = {
state: this.captureState,
capturedBy: this.capturedBy,
capturingBy: this.capturingBy,
dir: 0,
}

this.currentwhat = {
state: this.captureState,
capturedBy: this.capturedBy,
capturingBy: this.capturingBy,
dir: 0,
}
}

check() {
for (var key of Object.keys(this.currentwhat)) {
if(this.currentwhat[key] != this.lastSendWhat[key]) {
this.lastSendWhat[key] = this.currentwhat[key];
return false;
}
}
return true;
}
pepperGrew(room) {
this.lastPepperGrew = Date.now();
Expand Down Expand Up @@ -61,22 +87,35 @@ module.exports = class Island {
capturedPercentage: this.capturedPercentage,
capturingBy: this.capturingBy,
people: this.people,
currentwhat: this.currentwhat
}
}
tick(diff, room) {
if(!this.canBeCaptured) return;

var players = Array.from(room.players.values()).filter(player => this.isIn(player.pos));
this.people = players.map(player => player.id);
io.getio().to(room.id).emit("islandUpdate", this.getSendObject());
if(this.lastPepperGrew + 1000 < Date.now()) this.pepperGrew(room);

this.currentwhat.state = this.captureState;
this.currentwhat.capturedBy = this.capturedBy;
this.currentwhat.capturingBy = this.capturingBy;

if(!this.check()) {
io.getio().to(room.id).emit("islandState", this.id, this.currentwhat, this.capturedPercentage);
console.log("islandState", this.currentwhat);
this.lastSendWhat = JSON.parse(JSON.stringify(this.currentwhat));
}

if(players.length < 1) {

if(this.captureState == 1) {
this.capturedPercentage -= (diff / 50);
this.currentwhat.dir = -1;

if(this.capturedPercentage <= 0) {
this.captureState = 0;
this.currentwhat.dir = 0;
this.capturedPercentage = 0;
this.capturedBy = "none";
this.capturingBy = "none";
Expand All @@ -85,8 +124,12 @@ module.exports = class Island {

if(this.captureState == 4) {
this.capturedPercentage += (diff / 50);
this.currentwhat.dir = 1;

if(this.capturedPercentage >= 100) {
this.capturedBy = this.capturingBy;
this.currentwhat.dir = 0;

this.captureState = 2;
this.clearOtherPeppers(room);
}
Expand All @@ -96,7 +139,8 @@ module.exports = class Island {

//make sure everyone on the island is on the same team
if(!players.every((player) => player.team == players[0].team)) {
return;
this.currentwhat.dir = 0;
return;
}

var team = players[0].team;
Expand All @@ -110,19 +154,29 @@ module.exports = class Island {
if(this.captureState == 1) {
if (this.capturingBy == team) {
this.capturedPercentage += (diff / 50) * count;
this.currentwhat.dir = count;

if(this.capturedPercentage >= 100) {
this.captureState = 2;
this.clearOtherPeppers(room);
this.capturedBy = team;
this.currentwhat.dir = 0;

this.capturingBy = team;
this.capturedPercentage = 100;
}
} else {
this.capturedPercentage -= (diff / 50) * count;
this.currentwhat.dir = -1*count;

if(this.capturedPercentage <= 0) {
this.captureState = 0;
this.currentwhat.dir = 0;

this.capturedPercentage = 0;
this.capturedBy = "none";
this.currentwhat.dir = 0;

this.capturingBy = "none";
}
}
Expand All @@ -131,26 +185,35 @@ module.exports = class Island {
if(this.captureState == 2 && this.capturedBy != team) {
this.captureState = 4;
this.capturedBy = "none";
this.currentwhat.dir = 0;

this.destroyingBy = team;
}

if(this.captureState == 4) {
if(this.destroyingBy == team) {
this.capturedPercentage -= (diff / 50) * count;
this.currentwhat.dir = -1*count;
if(this.capturedPercentage <= 0) {
this.captureState = 0;
this.currentwhat.dir = 0;

this.capturedBy = "none";
this.capturingBy = "none";
this.capturedPercentage = 0;
}
}
else {
this.capturedPercentage += (diff / 50) * count;
this.currentwhat.dir = count;

if(this.capturedPercentage >= 100) {
this.captureState = 2;
this.clearOtherPeppers(room);
this.capturedBy = team;
this.capturingBy = team;
this.currentwhat.dir = 0;

this.capturedPercentage = 100;
}
}
Expand Down
4 changes: 4 additions & 0 deletions classes/Room.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ class Room {
player.socket.emit("bullets", this.bullets.map((bullet) => bullet.getSendObject()));
player.socket.emit("islands", this.islands.map((island) => island.getSendObject()));
player.socket.emit("bridges", this.bridges.map((bridge) => bridge.getSendObject()));

// this.islands.forEach((island) => {
// player.socket.emit("islandState", island.id, island.currentwhat)
// });
}
removePlayer(id) {
var ioinstance = io.getio();
Expand Down
62 changes: 23 additions & 39 deletions src/GameScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,52 +461,22 @@ this.minimap.setVisible(false);
// console.log("playerJoined", data);
playerJoined(data);
});
this.socket.on("islandState", (id: number, data: {state: number, capturedBy: string, capturingBy: string, dir: number}, capturedPercentage: number) => {
if(this.islands.find(i => i.id == id)) {
console.log("islandUpdate", data);

var island = this.islands.find(i => i.id == id);
island.setCurState(data, capturedPercentage);
}
})
this.socket.on("islandUpdate", (data: IslandData) => {
if(this.islands.find(i => i.id === data.id)) {
var island = this.islands.find(i => i.id === data.id);
island.setTeam(data.capturedBy);
island.setPercent(data.capturedPercentage, data.capturingBy);
// console.log(data);

if(data.people.includes(this.socket.id) && this.team && data.capturedBy == "none") {
if(Math.ceil(data.capturedPercentage) < 100) {
this.captureText.setData('island', data.id);
if(data.capturingBy == this.team) this.captureText.setText("Capturing..." + Math.round(data.capturedPercentage) + "%");
else this.captureText.setText("Destroying..." + Math.round(data.capturedPercentage) + "%");
}
else {
this.captureText.setText("");
if(data.capturedBy == this.team || data.capturingBy == this.team) {
var t = this.add.text(this.canvas.width / 2, this.canvas.height / 5, "Island Captured!", {
fontSize: Math.min(this.canvas.width/10, 70)+"px",
fontFamily: "Arial",
color: "#000000",
align: "center"
}).setDepth(10).setAlpha(0);
this.captured.play();
t.setOrigin(0.5);
this.cameras.main.ignore(t);
this.minimap.ignore(t);
this.tweens.add({
targets: t,
alpha: 1,
onComplete: () => {
this.tweens.add({
targets: t,
alpha: 0,
duration: 1000,
onComplete: () => {
t.destroy();
}
});
}
});
}
}
} else {
// console.log(this.captureText.data)
if(this.captureText.getData('island') == data.id) this.captureText.setText("");
}



}
Expand Down Expand Up @@ -1168,6 +1138,20 @@ if(this.vid && this.vid.visible) {
newZoom
);
// console.log(this.background.tileScaleX, this.background.tileScaleY);

var me = this.players.get(this.socket?.id);

if(me && this.captureText && this.islands.some(island => island.inIsland(me.x, me.y))) {
var island = this.islands.find(island => island.inIsland(me.x, me.y));
if(island && island.dir != 0 && (island.dir > 0 ? island.capturingBy == this.team : island.capturingBy != this.team)) {
this.captureText.setVisible(true);
this.captureText.setText(island.dir < 0 ? "Destroying.. "+(100-Math.max(0,Math.min(100, Math.round(island.capturingCircle.scaleX*100))))+"%" : "Capturing.. "+Math.max(0,Math.min(100, Math.round(island.capturingCircle.scaleX*100)))+"%");
} else {
this.captureText.setText("");
}
} else {
this.captureText?.setText("");
}

}
}
Expand Down
76 changes: 73 additions & 3 deletions src/components/Island.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export default class Island extends Phaser.GameObjects.Container {
capturingCircle: Phaser.GameObjects.Ellipse;
flag: Phaser.GameObjects.Image;
background: Phaser.GameObjects.Image;
dir: number;
lastUpdate: number;
capturingBy: string;
constructor(scene: Phaser.Scene, data: IslandData) {
super(scene);
this.x = data.pos.x;
Expand All @@ -16,6 +19,8 @@ export default class Island extends Phaser.GameObjects.Container {
this.capturedBy = data.capturedBy;
this.capturingCircle = new Phaser.GameObjects.Ellipse(scene, 0, 0, data.size, data.size, 0x00FFFF).setOrigin(0.5).setVisible(false).setDepth(1);
this.id = data.id;
this.dir = 0;
this.lastUpdate = Date.now();

this.flag = new Phaser.GameObjects.Image(scene, 0, -200, "redFlag").setOrigin(0.5).setVisible(false).setVisible(false);
// set scale to island size
Expand All @@ -28,7 +33,8 @@ export default class Island extends Phaser.GameObjects.Container {


// if(this.capturedBy == "red") console.log(this.id + " is captured by red");
this.island = new Phaser.GameObjects.Ellipse(scene, 0, 0, data.size, data.size, data.capturedBy == "none" ? 0x838579: data.capturedBy == "red" ? 0xFF0000 : 0x0000FF).setOrigin(0.5).setDepth(1);

this.island = new Phaser.GameObjects.Ellipse(scene, 0, 0, data.size, data.size, data.capturedBy == "none" ? 0x838579: data.capturedBy == "red" ? 0xFF3632 : 0x009dff).setOrigin(0.5).setDepth(1);

if(data.capturedPercentage < 100) {
this.setPercent(data.capturedPercentage, data.capturingBy);
Expand All @@ -40,6 +46,8 @@ export default class Island extends Phaser.GameObjects.Container {
if(data.size >= 1000) this.add(this.background);
this.scene.add.existing(this);
(this.scene as GameScene).uiCam.ignore(this);

this.setCurState(data.currentwhat, data.capturedPercentage);
}
getDomination() {
var d = {
Expand All @@ -56,11 +64,17 @@ export default class Island extends Phaser.GameObjects.Container {
// console.log(d);
return d;
}
setCurState(state: {state: number, capturedBy: string, capturingBy: string, dir: number}, capturedPercentage: number) {
this.setTeam(state.capturedBy);
this.dir = state.dir;
this.capturingBy = state.capturingBy;
console.log(state.capturedBy, state.capturingBy);
this.setPercent(capturedPercentage, state.capturingBy);
}
setTeam(team: string) {
this.capturingCircle.setVisible(false);
this.island.setFillStyle(team == "red" ? 0xFF3632 : team == "none" ? 0x838579 : 0x009dff);
this.capturedBy = team;


this.flag.setTexture(team == "red" ? "redFlag" : "blueFlag");
if(team != "none") {
Expand All @@ -77,6 +91,35 @@ export default class Island extends Phaser.GameObjects.Container {
}


var gameScene = this.scene as GameScene;
var player = gameScene.players.get(gameScene.socket.id);

if(player && this.inIsland(player.x, player.y) && player.team == team) {
var r = gameScene.add.text(gameScene.canvas.width / 2, gameScene.canvas.height / 5, "Island Captured!", {
fontSize: Math.min(gameScene.canvas.width/10, 70)+"px",
fontFamily: "Arial",
color: "#000000",
align: "center"
}).setDepth(10).setAlpha(0);
gameScene.captured.play();
r.setOrigin(0.5);
gameScene.cameras.main.ignore(r);
gameScene.minimap.ignore(r);
gameScene.tweens.add({
targets: r,
alpha: 1,
onComplete: () => {
gameScene.tweens.add({
targets: r,
alpha: 0,
duration: 1000,
onComplete: () => {
r.destroy();
}
});
}
});
}
}
else {
this.flag.setAlpha(1);
Expand All @@ -97,11 +140,38 @@ export default class Island extends Phaser.GameObjects.Container {
}
}
setPercent(percent: number, team: string) {
// console.log(team)
console.log(team, percent)
this.capturingCircle.setFillStyle(team == "red" ? 0xFF3632 : team == "none" ? 0x838579 : 0x0096ff);
this.capturingCircle.setVisible(true);
this.capturingCircle.setScale(percent/100);

}
inIsland(x: number, y: number) {
//check if point in isalnd

var radius = this.island.displayWidth/2;
var x2 = this.x;
var y2 = this.y;
var dist = Math.sqrt(Math.pow(x - x2, 2) + Math.pow(y - y2, 2));
console.log(dist, radius, dist < radius);
return dist < radius;
}
preUpdate(delta) {

var curPercent = this.capturingCircle.scaleX * 100;
var diff = Date.now() - this.lastUpdate;
// console.log(curPercent, this.capturingCircle.scaleX);

this.lastUpdate = Date.now();

if(this.capturingBy) this.setPercent(curPercent + ((diff / 50) * this.dir), this.capturingBy);






// this.setPercent()
}
}

Loading

0 comments on commit 762a9de

Please sign in to comment.