Improve netcode and simplify (#4)
- Simplify the logic wherever possible to remove overengineering and simplify the netcode - Actually improve the netcode - The new lines are mainly the package locks and tests Reviewed-on: https://home.schmelczer.dev/git/andras/decla-red/pulls/4
This commit is contained in:
parent
5d854440ec
commit
93a7fe01ab
1 changed files with 15073 additions and 4754 deletions
|
|
@ -2,16 +2,14 @@ import { vec2 } from 'gl-matrix';
|
|||
import { Random, PlanetBase, hsl, settings, evaluateSdf } from 'shared';
|
||||
import { LampPhysical } from './objects/lamp-physical';
|
||||
import { PlanetPhysical } from './objects/planet-physical';
|
||||
import { PhysicalContainer } from './physics/containers/physical-container';
|
||||
import { Physical } from './physics/physicals/physical';
|
||||
import { PhysicalContainer } from './physics/physical-container';
|
||||
|
||||
export const createWorld = (objectContainer: PhysicalContainer) => {
|
||||
const objects: Array<Physical> = [];
|
||||
const lights: Array<Physical> = [];
|
||||
const planets: Array<PlanetPhysical> = [];
|
||||
const lamps: Array<LampPhysical> = [];
|
||||
|
||||
for (let r = 0; r < settings.worldRadius; r += settings.radiusSteps) {
|
||||
const circumference = 2 * Math.PI * r;
|
||||
const stepCount = circumference * settings.objectsOnCircleLength;
|
||||
const stepCount = 2 * Math.PI * r * settings.objectsOnCircleLength;
|
||||
for (let rad = 0; rad < 2 * Math.PI; rad += (2 * Math.PI) / stepCount) {
|
||||
const position = vec2.rotate(
|
||||
vec2.create(),
|
||||
|
|
@ -20,12 +18,12 @@ export const createWorld = (objectContainer: PhysicalContainer) => {
|
|||
rad,
|
||||
);
|
||||
|
||||
if (objects.length !== 0 && Random.getRandom() > 0.5) {
|
||||
if (planets.length !== 0 && Random.getRandom() > 0.5) {
|
||||
if (
|
||||
evaluateSdf(position, objects) > 200 &&
|
||||
!lights.find((l) => l.distance(position) < 2500)
|
||||
evaluateSdf(position, planets) > 200 &&
|
||||
!lamps.find((l) => l.distance(position) < 2500)
|
||||
) {
|
||||
lights.push(
|
||||
lamps.push(
|
||||
new LampPhysical(
|
||||
position,
|
||||
hsl(
|
||||
|
|
@ -37,61 +35,43 @@ export const createWorld = (objectContainer: PhysicalContainer) => {
|
|||
),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
if (
|
||||
evaluateSdf(position, objects) > 1400 &&
|
||||
!lights.find((l) => l.distance(position) < 1700)
|
||||
) {
|
||||
const planet =
|
||||
objects.length === 0
|
||||
? // The first, central giant is the keystone "Heart": a named,
|
||||
// always-contested focal objective the whole match orbits.
|
||||
new PlanetPhysical(
|
||||
PlanetBase.createPlanetVertices(
|
||||
position,
|
||||
Random.getRandomInRange(1600, 2400),
|
||||
Random.getRandomInRange(1600, 2400),
|
||||
Random.getRandomInRange(80, 300),
|
||||
),
|
||||
true,
|
||||
)
|
||||
: new PlanetPhysical(
|
||||
PlanetBase.createPlanetVertices(
|
||||
position,
|
||||
Random.getRandomInRange(300, 1600),
|
||||
Random.getRandomInRange(300, 1600),
|
||||
Random.getRandomInRange(20, 100),
|
||||
),
|
||||
);
|
||||
|
||||
objects.push(planet);
|
||||
}
|
||||
} else if (
|
||||
evaluateSdf(position, planets) > 1400 &&
|
||||
!lamps.find((l) => l.distance(position) < 1700)
|
||||
) {
|
||||
const isKeystone = planets.length === 0;
|
||||
const [sizeMin, sizeMax, roughMin, roughMax] = isKeystone
|
||||
? [1600, 2400, 80, 300]
|
||||
: [300, 1600, 20, 100];
|
||||
planets.push(
|
||||
new PlanetPhysical(
|
||||
PlanetBase.createPlanetVertices(
|
||||
position,
|
||||
Random.getRandomInRange(sizeMin, sizeMax),
|
||||
Random.getRandomInRange(sizeMin, sizeMax),
|
||||
Random.getRandomInRange(roughMin, roughMax),
|
||||
),
|
||||
isKeystone,
|
||||
objectContainer.game,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
console.info(`Generated ${objects.length} planets`);
|
||||
console.info(`Generated ${lights.length} light`);
|
||||
console.info(`Generated ${planets.length} planets and ${lamps.length} lamps`);
|
||||
|
||||
// Associate each lamp with its NEAREST planet, so a planet can repaint "its"
|
||||
// lamps to the owning team's colour when it flips. Lamps are already placed by
|
||||
// proximity during world-gen, so the nearest planet is the one whose capture
|
||||
// they should advertise. Distances use the planet SDF (negative inside), which
|
||||
// is exactly the "closest planet" metric we want.
|
||||
const planets = objects.filter((o): o is PlanetPhysical => o instanceof PlanetPhysical);
|
||||
lights
|
||||
.filter((l): l is LampPhysical => l instanceof LampPhysical)
|
||||
.forEach((lamp) => {
|
||||
let nearest: PlanetPhysical | undefined;
|
||||
let nearestDistance = Infinity;
|
||||
planets.forEach((planet) => {
|
||||
const distance = planet.distance(lamp.center);
|
||||
if (distance < nearestDistance) {
|
||||
nearestDistance = distance;
|
||||
nearest = planet;
|
||||
}
|
||||
});
|
||||
nearest?.addLamp(lamp);
|
||||
});
|
||||
for (const lamp of lamps) {
|
||||
let nearest: PlanetPhysical | undefined;
|
||||
let nearestDistance = Infinity;
|
||||
for (const planet of planets) {
|
||||
const distance = planet.distance(lamp.center);
|
||||
if (distance < nearestDistance) {
|
||||
nearestDistance = distance;
|
||||
nearest = planet;
|
||||
}
|
||||
}
|
||||
nearest?.addLamp(lamp);
|
||||
}
|
||||
|
||||
[...objects, ...lights].forEach((o) => objectContainer.addObject(o));
|
||||
[...planets, ...lamps].forEach((o) => objectContainer.addObject(o));
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue