Improve gameplay
This commit is contained in:
parent
7412bc8af5
commit
793d9a81e8
2 changed files with 275 additions and 226 deletions
|
|
@ -1,9 +1,17 @@
|
|||
import { vec2 } from 'gl-matrix';
|
||||
import { CommandReceiver, Circle, PlayerInformation, CharacterTeam } from 'shared';
|
||||
import {
|
||||
CommandReceiver,
|
||||
Circle,
|
||||
PlayerInformation,
|
||||
CharacterTeam,
|
||||
Random,
|
||||
settings,
|
||||
} from 'shared';
|
||||
import { PhysicalContainer } from '../physics/containers/physical-container';
|
||||
import { getBoundingBoxOfCircle } from '../physics/functions/get-bounding-box-of-circle';
|
||||
import { isCircleIntersecting } from '../physics/functions/is-circle-intersecting';
|
||||
import { CharacterPhysical } from '../objects/character-physical';
|
||||
import { PlanetPhysical } from '../objects/planet-physical';
|
||||
import { PlayerContainer } from './player-container';
|
||||
|
||||
export abstract class PlayerBase extends CommandReceiver {
|
||||
|
|
@ -22,14 +30,14 @@ export abstract class PlayerBase extends CommandReceiver {
|
|||
super();
|
||||
}
|
||||
|
||||
protected createCharacter(preferredCenter: vec2) {
|
||||
protected createCharacter() {
|
||||
this.character = new CharacterPhysical(
|
||||
this.playerInfo.name.slice(0, 20),
|
||||
this.sumKills,
|
||||
this.sumDeaths,
|
||||
this.team,
|
||||
this.objectContainer,
|
||||
this.findEmptyPositionForPlayer(preferredCenter),
|
||||
this.findEmptyPositionForPlayer(this.findSpawnCenter()),
|
||||
);
|
||||
|
||||
this.objectContainer.addObject(this.character);
|
||||
|
|
@ -37,14 +45,37 @@ export abstract class PlayerBase extends CommandReceiver {
|
|||
|
||||
public abstract step(deltaTimeInSeconds: number): void;
|
||||
|
||||
protected findEmptyPositionForPlayer(preferredCenter: vec2): vec2 {
|
||||
if (!preferredCenter) {
|
||||
preferredCenter = vec2.create();
|
||||
private findSpawnCenter(): vec2 {
|
||||
const planets = this.objectContainer
|
||||
.findIntersecting(
|
||||
getBoundingBoxOfCircle(new Circle(vec2.create(), settings.worldRadius * 2)),
|
||||
)
|
||||
.filter((o): o is PlanetPhysical => o instanceof PlanetPhysical);
|
||||
|
||||
const friendly = planets.filter((p) => p.team === this.team);
|
||||
const neutral = planets.filter((p) => p.team === CharacterTeam.neutral);
|
||||
const candidates = friendly.length ? friendly : neutral.length ? neutral : planets;
|
||||
if (candidates.length === 0) {
|
||||
return vec2.create();
|
||||
}
|
||||
|
||||
const isContested = (planet: PlanetPhysical) =>
|
||||
this.playerContainer.players.some(
|
||||
(p) =>
|
||||
p.team !== this.team &&
|
||||
p.character?.isAlive &&
|
||||
vec2.distance(p.center, planet.center) < settings.spawnSafetyDistance,
|
||||
);
|
||||
const safe = candidates.filter((p) => !isContested(p));
|
||||
|
||||
// candidates is non-empty here, so choose() always returns a planet.
|
||||
return vec2.clone(Random.choose(safe.length ? safe : candidates)!.center);
|
||||
}
|
||||
|
||||
protected findEmptyPositionForPlayer(preferredCenter: vec2): vec2 {
|
||||
let rotation = 0;
|
||||
let radius = 0;
|
||||
for (;;) {
|
||||
for (; ;) {
|
||||
const playerPosition = vec2.fromValues(
|
||||
radius * Math.cos(rotation) + preferredCenter.x,
|
||||
radius * Math.sin(rotation) + preferredCenter.y,
|
||||
|
|
|
|||
|
|
@ -42,9 +42,8 @@ export class Player extends PlayerBase {
|
|||
(this.aspectRatio = v.aspectRatio),
|
||||
[MoveActionCommand.type]: (c: MoveActionCommand) =>
|
||||
this.character?.handleMovementAction(c),
|
||||
[PrimaryActionCommand.type]: (c: PrimaryActionCommand) => {
|
||||
this.character?.shootTowards(c.position);
|
||||
},
|
||||
[PrimaryActionCommand.type]: (c: PrimaryActionCommand) =>
|
||||
this.character?.shootTowards(c.position, c.charge),
|
||||
};
|
||||
|
||||
constructor(
|
||||
|
|
@ -60,11 +59,7 @@ export class Player extends PlayerBase {
|
|||
}
|
||||
|
||||
protected createCharacter() {
|
||||
const preferredCenter = this.playerContainer.players.find(
|
||||
(p) => p.character?.isAlive && p.team === this.team,
|
||||
)?.center;
|
||||
|
||||
super.createCharacter(preferredCenter ?? vec2.create());
|
||||
super.createCharacter();
|
||||
|
||||
this.objectsPreviouslyInViewArea.push(this.character!);
|
||||
this.queueCommandSend(new CreatePlayerCommand(this.character!));
|
||||
|
|
@ -206,8 +201,4 @@ export class Player extends PlayerBase {
|
|||
this.queueCommandSend(new ServerAnnouncement(announcement));
|
||||
}
|
||||
}
|
||||
|
||||
public destroy() {
|
||||
super.destroy();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue