even more
This commit is contained in:
parent
82e092ba55
commit
8f50ebdb97
1 changed files with 1842 additions and 3095 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,56 +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
|
||||
? 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`);
|
||||
|
||||
// Each lamp is associated with its nearest planet via the planet SDF
|
||||
// (negative inside), so it advertises that planet's capture.
|
||||
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