Spawning and Destruction - SC-SGS/surviving-sarntal GitHub Wiki
This page aims to explain the implementation details of the spawning and destruction process. The lifetime of entities is managed in the spawning and destruction phase.
Items are spawned by the ItemSpawner.
The `ItemSpawner` class determines the next spawn time by generating a random interval between 2 and 10 seconds, this can be changed in the configuration. This interval is added to the current game time to set `nextSpawnTime`. The spawn time is updated after each item is spawned.
The item type to be spawned is selected based on predefined spawn weights. Each item type has a spawn weight, and a random number between 1 and the sum of all spawn weights is generated. This random number is used to select an item type, with higher-weighted items being more likely to be chosen
The item spawn position is selected based on the current maximum x position plus an offset so that the spawning happens off camera. The height of the spawn position is determined as an offset of the mountain height at the set position. The offset is a random number from a given range that can be configured in the configuration.
The terrain generation is performed concurrently. See here.
Rocks are modelled as random convex polygons with a type (e.g. snow/ice/lava/crystal).
The type is chosen randomly with varying probabilities as the game progresses.
It determines a rock's texture and its density
Polygons are obtained by generating a random set of points within a random radius and the subsequent application of Andrew's monotone chain algorithm \cite{polygon_generation}.
The algorithm determines the convex hull of the set of points as the resulting polygon.
In addition to density, polygons possess mass and a moment of inertia as static properties.
The mass
After the polygon's static properties are calculated, rocks are spawned at a random position, with random angular momentum, and random linear momentum in x-direction.
Figure: The convex polygon is triangulated.
The inertia of the polygon is the sum of the inertia of all right subtriangles.
On the right, the blue triangle
At the end of a physics update, rocks that have left the scope of the world or hit the hiker are destroyed and removed. Similarly, old biomes are destructed. This garbage collection ensures a consistent performance no matter how long the game runs.