Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
codergautam committed Jul 7, 2022
1 parent cfe460c commit ed22731
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 33 deletions.
Binary file added assets/images/bluePepper.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
2 changes: 0 additions & 2 deletions classes/Bullet.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ class Bullet {
collidingPlayer(entity) {
if(entity.id == this.owner) return false;



var corners = entity.getCorners();
var arr = [];
corners.forEach(function(corner) {
Expand Down
31 changes: 23 additions & 8 deletions classes/Island.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const intersect = require("intersects");
const io = require("../helpers/io");
const idgen = require("../helpers/idgen");
const Pepper = require("./Pepper");
module.exports = class Island {
constructor(shape, size, position, canBeCaptured = true) {
this.shape = shape;
Expand All @@ -22,16 +23,27 @@ module.exports = class Island {
pepperGrew(room) {
this.lastPepperGrew = Date.now();

// check if captured
if(this.capturedBy == "none") return;

room.players.forEach(player => {
if(player.team == this.capturedBy) {
player.peppers += 1;
}
});
if(this.canBeCaptured && this.captureState == 2 && this.capturedBy != "none" && this.getPepperCount(room) < 10) {
const pepper = new Pepper(this);
room.peppers.set(pepper.id, pepper);
}

}
getPeppers(room) {
return [...room.peppers.values()].filter(pepper => this.isIn(pepper.pos));
}
getPepperCount(room) {
return this.getPeppers(room).length;
}
clearOtherPeppers(room) {
this.getPeppers(room).forEach(pepper => {
if(pepper.color != this.capturedBy) {
room.peppers.delete(pepper.id);
io.getio().to(room.id).emit("pepperCollected", pepper.id);
}
}
);
}
getRandomPoint(multiply=1) {
var radius = ((this.size * multiply) /2) * Math.sqrt(Math.random());
var angle = Math.random() * 2 * Math.PI;
Expand Down Expand Up @@ -76,6 +88,7 @@ module.exports = class Island {
if(this.capturedPercentage >= 100) {
this.capturedBy = this.capturingBy;
this.captureState = 2;
this.clearOtherPeppers(room);
}
}

Expand All @@ -99,6 +112,7 @@ module.exports = class Island {
this.capturedPercentage += (diff / 50) * count;
if(this.capturedPercentage >= 100) {
this.captureState = 2;
this.clearOtherPeppers(room);
this.capturedBy = team;
this.capturingBy = team;
this.capturedPercentage = 100;
Expand Down Expand Up @@ -134,6 +148,7 @@ module.exports = class Island {
this.capturedPercentage += (diff / 50) * count;
if(this.capturedPercentage >= 100) {
this.captureState = 2;
this.clearOtherPeppers(room);
this.capturedBy = team;
this.capturingBy = team;
this.capturedPercentage = 100;
Expand Down
3 changes: 3 additions & 0 deletions classes/Pepper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ class Pepper {
this.island = island;
this.pos = island.getRandomPoint();
this.id = idgen();
this.color = island.capturedBy;
}
touchingPlayer(player) {
// console.log(player.pos, this.pos);
//distance between player and pepper
if(player.team != this.color) return false;
var distance = Math.sqrt(Math.pow(player.pos.x - this.pos.x, 2) + Math.pow(player.pos.y - this.pos.y, 2));
// console.log(distance);
//if distance is less than radius of player + radius of pepper
Expand All @@ -20,6 +22,7 @@ class Pepper {
return {
pos: this.pos,
id: this.id,
color: this.color,
};
}
}
Expand Down
4 changes: 3 additions & 1 deletion classes/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ class Player {
bodySize: this.bodySize,
maxHealth: this.maxHealth,
health: this.health,
canFly: this.canFly
canFly: this.canFly,

joinTime: this.spawnTime,

}
}
Expand Down
9 changes: 2 additions & 7 deletions classes/Room.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Room {

this.islands.push(new Island(
"circle",
1500,
1000,
{ x: 0, y: 0 },
false
));
Expand Down Expand Up @@ -212,7 +212,7 @@ class Room {
this.bullets = this.bullets.filter((bullet) => {
for (var player of Array.from(this.players.values())) {
if(bullet.collidingPlayer(player)) {
if(bullet.team != player.team) {
if(!(bullet.team == player.team || Date.now() - player.spawnTime < 3000)) {

//emit to hitter

Expand Down Expand Up @@ -260,11 +260,6 @@ class Room {
island.tick(tickDiff, this);
});

if(this.peppers.size < 10) {
var p = new Pepper(this.islands[0]);
this.peppers.set(p.id, p);
}

//emit to all players
ioinstance.to(this.id).emit("peppers", [...this.peppers.values()].map((pepper) => pepper.getSendObject()));

Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ app.use("/", express.static(__dirname + "/dist"));
app.use("/", express.static(__dirname+"/public"));
const axios = require("axios");

const recaptcha = false;
// require("dotenv").config()


Expand Down Expand Up @@ -87,6 +88,7 @@ io.on("connection", async (socket) => {
f = f.data;
if (!f.success) {
console.log("Captcha failed " + f["error-codes"].toString());
if(!recaptcha) joinThemIn();
return;
}
if (f.score < 0.3) {
Expand Down
16 changes: 8 additions & 8 deletions src/GameScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import GameMap from "./components/Map";
import Pepper from "./components/Pepper";
import Player from "./components/Player";
import TeamPicker from "./components/TeamPicker";
import { Data, PlayerData, FirstPlayerData, BulletData, IslandData, BridgeData } from "./helpers/Packets";
import { PlayerData, FirstPlayerData, BulletData, IslandData, BridgeData, PepperData } from "./helpers/Packets";

interface Keys {
up: Phaser.Input.Keyboard.Key;
Expand Down Expand Up @@ -311,7 +311,7 @@ this.lastKnownMyDisplayWidth = 0;
this.loadingText.setFontSize(this.canvas.width / 20);
this.socket = io();
// this.socket.emit("go", this.name, team, this.mobile?true:false);
this.socket.emit("go", this.name, team, true, thetoken);
this.socket.emit("go", this.name, team, false, thetoken);

this.team = `${team}`;

Expand All @@ -330,8 +330,8 @@ this.lastKnownMyDisplayWidth = 0;
this.minimap.setZoom(zoom);
// this.minimap.setBackgroundColor(0x002244);
this.minimap.setBackgroundColor(this.team == "blue" ? 0x002244 : 0x440000);
this.minimap.scrollX = -150;
this.minimap.scrollY = -150;
this.minimap.scrollX = -75;
this.minimap.scrollY = -75;
this.minimap.setVisible(false);
this.minimap.ignore(this.background);

Expand All @@ -353,7 +353,7 @@ this.minimap.setVisible(false);

this.add.existing(this.killCount);
this.killCount.addImage("pepper", {
key: "pepper",
key: team == "blue"? "bluePepper" : "redPepper",
width: 45,
height: 45
});
Expand Down Expand Up @@ -415,17 +415,17 @@ this.minimap.setVisible(false);
this.dominationBar.bar.x -= this.dominationBar.width / 2;

const playerJoined = (data: FirstPlayerData) =>{
this.players.set(data.id, new Player(this, data.pos.x, data.pos.y, data.id, data.name, data.team).setDepth(2));
this.players.set(data.id, new Player(this, data.pos.x, data.pos.y, data.id, data.name, data.team, undefined, undefined, data.joinTime).setDepth(2));
if(this.socket.id === data.id) {
this.cameras.main.startFollow(this.players.get(data.id));
// this.minimap.startFollow(this.players.get(data.id));
}
}

this.socket.on("peppers", (data: Data[]) => {
this.socket.on("peppers", (data: PepperData[]) => {
data.forEach((d) => {
if(!this.peppers.has(d.id)) {
this.peppers.set(d.id, new Pepper(this, d.pos.x, d.pos.y, d.id).setDepth(2));
this.peppers.set(d.id, new Pepper(this, d.pos.x, d.pos.y, d.id, d.color).setDepth(1.9));
}
})
})
Expand Down
5 changes: 4 additions & 1 deletion src/OpenScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ var progress = (1 - (remainder / total) as any);

this.load.image("home", "/assets/images/home.png");
this.load.image("again", "/assets/images/again.jpeg");
this.load.image("pepper", "/assets/images/pepper.png");

this.load.image("redPepper", "/assets/images/redPepper.png");
this.load.image("bluePepper", "/assets/images/bluePepper.png");

this.load.image("grass", "/assets/images/circlegrass.png");
this.load.audio("titleMusic", "/assets/audio/title.mp3");
this.load.audio("pick", "/assets/audio/pick.wav");
Expand Down
2 changes: 1 addition & 1 deletion src/components/Bullet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Phaser from "phaser";
import GameScene from "../GameScene";
import { BulletData, Data } from "../helpers/Packets";
import { BulletData } from "../helpers/Packets";

export default class Bullet extends Phaser.GameObjects.Container {
id: string;
Expand Down
4 changes: 2 additions & 2 deletions src/components/Pepper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import GameScene from "../GameScene";
export default class Pepper extends Phaser.GameObjects.Container {
id: string;
image: Phaser.GameObjects.Image;
constructor(scene: GameScene, x: number, y: number, id: string) {
constructor(scene: GameScene, x: number, y: number, id: string, color: string) {
super(scene);

this.x = x;
this.y = y;
this.id = id;

this.image = new Phaser.GameObjects.Image(scene, 0, 0, "pepper").setScale(0.2).setAlpha(0);
this.image = new Phaser.GameObjects.Image(scene, 0, 0, color+"Pepper").setScale(0.2).setAlpha(0);
this.scene.tweens.add({
targets: this.image,
alpha: 1,
Expand Down
30 changes: 28 additions & 2 deletions src/components/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export default class Player extends Phaser.GameObjects.Container {
oldLevel: number;
oldUntilNextLevel: number[];
circle: Phaser.GameObjects.Ellipse;

joinTime: number;
constructor(
scene: Phaser.Scene,
x: number,
Expand All @@ -33,7 +35,8 @@ export default class Player extends Phaser.GameObjects.Container {
name: string,
team: string,
speed: number = 1,
size: number = 50
size: number = 50,
joinTime: number
) {
super(scene);
this.x = x;
Expand All @@ -42,6 +45,7 @@ export default class Player extends Phaser.GameObjects.Container {
this.lastTick = Date.now();
this.lastUpdate = Date.now();

this.joinTime = joinTime;

this.id = id;
this.name = name;
Expand Down Expand Up @@ -113,7 +117,29 @@ export default class Player extends Phaser.GameObjects.Container {
this.scene.add.existing(this);
(this.scene as GameScene).uiCam.ignore(this);
(this.scene as GameScene).minimap.ignore([this.healthBar, this.nameTag, this.image]);

console.log(this.joinTime, this.name, Date.now() - this.joinTime)
if(Date.now() - this.joinTime < 3000) {
this.scene.tweens.add({
targets: this.image,
alpha: 0.4,
duration: 100,
ease: "Linear",
repeat: 0,
delay: 0,
onComplete: () => {
setTimeout(() => {
this.scene.tweens.add({
targets: this.image,
alpha: 1,
duration: 300,
ease: "Linear",
repeat: 0,
delay: 0,
});
}, 3000-( Date.now() - this.joinTime))
}
});
}
}
tick(data: PlayerData, hit: boolean) {
this.toAngle = data.lookAngle + Math.PI + 0.35;
Expand Down
7 changes: 6 additions & 1 deletion src/helpers/Packets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ interface Data {
id: string;
pos: {x: number, y: number};
}
interface PepperData extends Data {
color: string;
}

interface PlayerData extends Data {
health: number;
lookAngle: number;
Expand All @@ -16,6 +20,7 @@ interface PlayerData extends Data {
interface FirstPlayerData extends PlayerData {
name: string;
team: string;
joinTime: number;
}
interface BulletData extends Data {
angle: number;
Expand All @@ -40,4 +45,4 @@ interface BridgeData {
corners: {x: number, y: number}[],
};

export { Data, BridgeData, IslandData, BulletData, PlayerData, FirstPlayerData };
export { BridgeData, IslandData, BulletData, PlayerData, FirstPlayerData, PepperData };

0 comments on commit ed22731

Please sign in to comment.