Improve world creation
This commit is contained in:
parent
dee72440bb
commit
dd21c20b3a
5 changed files with 57 additions and 65 deletions
|
|
@ -5,4 +5,5 @@ export const defaultOptions: Options = {
|
|||
name: 'Localhost',
|
||||
playerLimit: 16,
|
||||
seed: 51,
|
||||
worldSize: 10000,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ export class GameServer {
|
|||
this.serverName = options.name;
|
||||
this.playerLimit = options.playerLimit;
|
||||
|
||||
createWorld(this.objects);
|
||||
createWorld(this.objects, options.worldSize);
|
||||
this.objects.initialize();
|
||||
|
||||
io.on('connection', (socket: SocketIO.Socket) => {
|
||||
|
|
|
|||
|
|
@ -1,73 +1,65 @@
|
|||
import { vec2, vec3 } from 'gl-matrix';
|
||||
import { Random, settings, PlanetBase, hsl } from 'shared';
|
||||
import { vec2 } from 'gl-matrix';
|
||||
import { Random, PlanetBase, hsl, settings } from 'shared';
|
||||
import { LampPhysical } from '../objects/lamp-physical';
|
||||
|
||||
import { PlanetPhysical } from '../objects/planet-physical';
|
||||
import { PhysicalContainer } from '../physics/containers/physical-container';
|
||||
import { evaluateSdf } from '../physics/functions/evaluate-sdf';
|
||||
import { Physical } from '../physics/physicals/physical';
|
||||
|
||||
export const createWorld = (objectContainer: PhysicalContainer) => {
|
||||
export const createWorld = (objectContainer: PhysicalContainer, worldRadius: number) => {
|
||||
const objects: Array<Physical> = [];
|
||||
const lights: Array<Physical> = [];
|
||||
|
||||
const worldSize = vec2.length(
|
||||
vec2.fromValues(
|
||||
settings.worldTopEdge - settings.worldBottomEdge,
|
||||
settings.worldRightEdge - settings.worldLeftEdge,
|
||||
),
|
||||
);
|
||||
|
||||
for (let i = 0; i < 15; i++) {
|
||||
console.log('planet', i);
|
||||
|
||||
let position: vec2;
|
||||
|
||||
do {
|
||||
position = vec2.fromValues(
|
||||
Random.getRandomInRange(settings.worldLeftEdge, settings.worldRightEdge),
|
||||
Random.getRandomInRange(settings.worldBottomEdge, settings.worldTopEdge),
|
||||
for (let r = 0; r < worldRadius; r += settings.radiusSteps) {
|
||||
const circumference = 2 * Math.PI * r;
|
||||
const stepCount = circumference * settings.objectsOnCircleLength;
|
||||
for (let rad = 0; rad < 2 * Math.PI; rad += (2 * Math.PI) / stepCount) {
|
||||
const position = vec2.rotate(
|
||||
vec2.create(),
|
||||
vec2.fromValues(r, 0),
|
||||
vec2.create(),
|
||||
rad,
|
||||
);
|
||||
} while (
|
||||
evaluateSdf(position, objects) < 800 ||
|
||||
evaluateSdf(position, objects) > 2500
|
||||
);
|
||||
|
||||
objects.push(
|
||||
new PlanetPhysical(
|
||||
PlanetBase.createPlanetVertices(
|
||||
position,
|
||||
Random.getRandomInRange(300, 800),
|
||||
Random.getRandomInRange(300, 800),
|
||||
Random.getRandomInRange(20, 40),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
for (let i = 0; i < 15; i++) {
|
||||
console.log('light', i);
|
||||
let position: vec2;
|
||||
do {
|
||||
position = vec2.fromValues(
|
||||
Random.getRandomInRange(settings.worldLeftEdge, settings.worldRightEdge),
|
||||
Random.getRandomInRange(settings.worldBottomEdge, settings.worldTopEdge),
|
||||
);
|
||||
} while (
|
||||
evaluateSdf(position, objects) < 200 ||
|
||||
lights.find((l) => l.distance(position) < 1500)
|
||||
);
|
||||
lights.push(
|
||||
new LampPhysical(
|
||||
position,
|
||||
hsl(
|
||||
Random.getRandomInRange(0, 360),
|
||||
Random.getRandomInRange(50, 100),
|
||||
Random.getRandomInRange(25, 50),
|
||||
),
|
||||
Random.getRandomInRange(0.5, 2),
|
||||
),
|
||||
);
|
||||
if (Random.getRandom() > 0.5) {
|
||||
if (
|
||||
evaluateSdf(position, objects) > 200 &&
|
||||
!lights.find((l) => l.distance(position) < 2500)
|
||||
) {
|
||||
lights.push(
|
||||
new LampPhysical(
|
||||
position,
|
||||
hsl(
|
||||
(rad / (2 * Math.PI)) * 360,
|
||||
Random.getRandomInRange(50, 100),
|
||||
Random.getRandomInRange(40, 50),
|
||||
),
|
||||
Random.getRandomInRange(0.35, 1),
|
||||
),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
if (
|
||||
evaluateSdf(position, objects) > 1400 &&
|
||||
!lights.find((l) => l.distance(position) < 1700)
|
||||
) {
|
||||
objects.push(
|
||||
new PlanetPhysical(
|
||||
PlanetBase.createPlanetVertices(
|
||||
position,
|
||||
Random.getRandomInRange(300, 1600),
|
||||
Random.getRandomInRange(300, 1600),
|
||||
Random.getRandomInRange(20, 100),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log('Generated planet count', objects.length);
|
||||
console.log('Generated light count', lights.length);
|
||||
|
||||
[...objects, ...lights].forEach((o) => objectContainer.addObject(o));
|
||||
};
|
||||
|
|
|
|||
|
|
@ -3,4 +3,5 @@ export interface Options {
|
|||
name: string;
|
||||
playerLimit: number;
|
||||
seed: number;
|
||||
worldSize: number;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue