Skip to content

Commit f9ca753

Browse files
Use util.randomFrom function instead of doing this manually
1 parent 32d5a87 commit f9ca753

File tree

4 files changed

+12
-15
lines changed

4 files changed

+12
-15
lines changed

src/Gamemodes/Domination.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import TankBody from "../Entity/Tank/TankBody";
2525
import GameServer from "../Game";
2626
import ArenaEntity, { ArenaState } from "../Native/Arena";
2727
import { Entity } from "../Native/Entity";
28-
28+
import { randomFrom } from "../util";
2929

3030
const arenaSize = 11150;
3131
const baseSize = arenaSize / (3 + 1/3); // 3345, must scale with arena size
@@ -88,7 +88,7 @@ export default class DominationArena extends ArenaEntity {
8888
const xOffset = (Math.random() - 0.5) * baseSize,
8989
yOffset = (Math.random() - 0.5) * baseSize;
9090

91-
const team = this.playerTeamMap.get(client) || this.teams[~~(Math.random() * this.teams.length)];
91+
const team = this.playerTeamMap.get(client) || randomFrom(this.teams);
9292
const teamBase: TeamBase = this.game.entities.inner.find((entity) => entity instanceof TeamBase && entity.relationsData.values.team === team) as TeamBase;
9393

9494
tank.relationsData.values.team = teamBase.relationsData.values.team;

src/Gamemodes/Mothership.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import TankBody from "../Entity/Tank/TankBody";
2424
import GameServer from "../Game";
2525
import ArenaEntity, { ArenaState } from "../Native/Arena";
2626
import { Entity } from "../Native/Entity";
27-
import { PI2 } from "../util";
27+
import { PI2, randomFrom } from "../util";
2828

2929
const arenaSize = 11150;
3030
const TEAM_COLORS = [Color.TeamBlue, Color.TeamRed];
@@ -70,7 +70,7 @@ export default class MothershipArena extends ArenaEntity {
7070

7171
public spawnPlayer(tank: TankBody, client: Client) {
7272
if (!this.motherships.length && !this.playerTeamMotMap.has(client)) {
73-
const team = this.teams[~~(Math.random()*this.teams.length)];
73+
const team = randomFrom(this.teams);
7474
const { x, y } = this.findPlayerSpawnLocation();
7575

7676
tank.positionData.values.x = x;
@@ -80,22 +80,17 @@ export default class MothershipArena extends ArenaEntity {
8080
return;
8181
}
8282

83-
const mothership = this.playerTeamMotMap.get(client) || this.motherships[~~(Math.random() * this.motherships.length)];
83+
const mothership = this.playerTeamMotMap.get(client) || randomFrom(this.motherships);
8484
this.playerTeamMotMap.set(client, mothership);
8585

8686
tank.relationsData.values.team = mothership.relationsData.values.team;
8787
tank.styleData.values.color = mothership.styleData.values.color;
8888

8989
// TODO: Possess mothership if its unpossessed
90-
if (Entity.exists(mothership)) {
91-
tank.positionData.values.x = mothership.positionData.values.x;
92-
tank.positionData.values.y = mothership.positionData.values.y;
93-
} else {
94-
const { x, y } = this.findPlayerSpawnLocation();
90+
const { x, y } = this.findPlayerSpawnLocation();
9591

96-
tank.positionData.values.x = x;
97-
tank.positionData.values.y = y;
98-
}
92+
tank.positionData.values.x = x;
93+
tank.positionData.values.y = y;
9994

10095
if (client.camera) client.camera.relationsData.team = tank.relationsData.values.team;
10196
}

src/Gamemodes/Team2.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import TankBody from "../Entity/Tank/TankBody";
2525

2626
import { TeamEntity } from "../Entity/Misc/TeamEntity";
2727
import { Color } from "../Const/Enums";
28+
import { randomFrom } from "../util";
2829

2930
const arenaSize = 11150;
3031
const baseWidth = arenaSize / (3 + 1/3) * 0.6; // 2007
@@ -54,7 +55,7 @@ export default class Teams2Arena extends ArenaEntity {
5455

5556
const xOffset = (Math.random() - 0.5) * baseWidth;
5657

57-
const base = this.playerTeamMap.get(client) || [this.blueTeamBase, this.redTeamBase][0|Math.random()*2];
58+
const base = this.playerTeamMap.get(client) || randomFrom([this.blueTeamBase, this.redTeamBase]);
5859
tank.relationsData.values.team = base.relationsData.values.team;
5960
tank.styleData.values.color = base.styleData.values.color;
6061
tank.positionData.values.x = base.positionData.values.x + xOffset;

src/Gamemodes/Team4.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import TankBody from "../Entity/Tank/TankBody";
2525

2626
import { TeamEntity } from "../Entity/Misc/TeamEntity";
2727
import { Color } from "../Const/Enums";
28+
import { randomFrom } from "../util";
2829

2930
const arenaSize = 11150;
3031
const baseSize = arenaSize / (3 + 1/3); // 3345
@@ -63,7 +64,7 @@ export default class Teams4Arena extends ArenaEntity {
6364
const xOffset = (Math.random() - 0.5) * baseSize,
6465
yOffset = (Math.random() - 0.5) * baseSize;
6566

66-
const base = this.playerTeamMap.get(client) || [this.blueTeamBase, this.redTeamBase, this.greenTeamBase, this.purpleTeamBase][0|Math.random()*4];
67+
const base = this.playerTeamMap.get(client) || randomFrom([this.blueTeamBase, this.redTeamBase, this.greenTeamBase, this.purpleTeamBase]);
6768
tank.relationsData.values.team = base.relationsData.values.team;
6869
tank.styleData.values.color = base.styleData.values.color;
6970
tank.positionData.values.x = base.positionData.values.x + xOffset;

0 commit comments

Comments
 (0)