From dd21c20b3a703c30de2e189cc4fb8f42f0e5add1 Mon Sep 17 00:00:00 2001 From: schmelczerandras Date: Thu, 22 Oct 2020 21:48:13 +0200 Subject: [PATCH] Improve world creation --- backend/src/default-options.ts | 1 + backend/src/game-server.ts | 2 +- backend/src/map/create-world.ts | 108 +++++++++++++++----------------- backend/src/options.ts | 1 + shared/src/settings.ts | 10 ++- 5 files changed, 57 insertions(+), 65 deletions(-) diff --git a/backend/src/default-options.ts b/backend/src/default-options.ts index 4ce4c30..8ae4ab8 100644 --- a/backend/src/default-options.ts +++ b/backend/src/default-options.ts @@ -5,4 +5,5 @@ export const defaultOptions: Options = { name: 'Localhost', playerLimit: 16, seed: 51, + worldSize: 10000, }; diff --git a/backend/src/game-server.ts b/backend/src/game-server.ts index 991069b..71838de 100644 --- a/backend/src/game-server.ts +++ b/backend/src/game-server.ts @@ -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) => { diff --git a/backend/src/map/create-world.ts b/backend/src/map/create-world.ts index f6c7001..7b0ee18 100644 --- a/backend/src/map/create-world.ts +++ b/backend/src/map/create-world.ts @@ -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 = []; const lights: Array = []; - 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)); }; diff --git a/backend/src/options.ts b/backend/src/options.ts index c5fc944..3b738a5 100644 --- a/backend/src/options.ts +++ b/backend/src/options.ts @@ -3,4 +3,5 @@ export interface Options { name: string; playerLimit: number; seed: number; + worldSize: number; } diff --git a/shared/src/settings.ts b/shared/src/settings.ts index 98aec09..e7f5edc 100644 --- a/shared/src/settings.ts +++ b/shared/src/settings.ts @@ -11,8 +11,10 @@ export const settings = { physicsMaxStep: 2, maxVelocityX: 1000, maxVelocityY: 1000, + radiusSteps: 500, + objectsOnCircleLength: 0.002, planetEdgeCount: 7, - takeControlTimeInSeconds: 10, + takeControlTimeInSeconds: 4, maxGravityDistance: 700, maxGravityQ: 180, planetControlThreshold: 0.2, @@ -27,13 +29,9 @@ export const settings = { projectileMaxBounceCount: 1, projectileTimeout: 3, projectileFadeSpeed: 20, - projectileStartOffset: 150, projectileCreationInterval: 0.1, playerColorIndexOffset: 3, - worldTopEdge: 3000, - worldRightEdge: 3000, - worldLeftEdge: -3000, - worldBottomEdge: -3000, + worldRadius: 10000, backgroundGradient: [rgb255(90, 38, 43), rgb255(43, 39, 73)], declaColor, declaPlanetColor,