Added worldsim API, ticking

Former-commit-id: 50a63e44557c634f2b39ef7a580cdbeec5ed0d70
This commit is contained in:
Joshua Barretto 2019-05-18 09:59:58 +01:00
parent 5b055f54a5
commit 2548c1c31f
3 changed files with 64 additions and 7 deletions

View File

@ -30,6 +30,8 @@ use world::World;
const CLIENT_TIMEOUT: f64 = 20.0; // Seconds
const DEFAULT_WORLD_SEED: u32 = 1337;
pub enum Event {
ClientConnected { entity: EcsEntity },
ClientDisconnected { entity: EcsEntity },
@ -66,7 +68,7 @@ impl Server {
let mut this = Self {
state,
world: World::new(),
world: World::generate(DEFAULT_WORLD_SEED),
postoffice: PostOffice::bind(addrs.into())?,
clients: Clients::empty(),
@ -184,6 +186,9 @@ impl Server {
// Tick the client's LocalState (step 3).
self.state.tick(dt);
// Tick the world
self.world.tick(dt);
// Fetch any generated `TerrainChunk`s and insert them into the terrain.
// Also, send the chunk data to anybody that is close by.
if let Ok((key, chunk)) = self.chunk_rx.try_recv() {

View File

@ -1,8 +1,8 @@
// Library
mod sim;
use std::time::Duration;
use noise::{NoiseFn, Perlin, Seedable};
use vek::*;
// Project
use common::{
terrain::{Block, TerrainChunk, TerrainChunkMeta, TerrainChunkSize},
vol::{SizedVol, VolSize, Vox, WriteVol},
@ -13,11 +13,19 @@ pub enum Error {
Other(String),
}
pub struct World;
pub const WORLD_SIZE: Vec2<usize> = Vec2 { x: 1024, y: 1024 };
pub struct World {
sim: sim::WorldSim,
}
impl World {
pub fn new() -> Self {
Self
pub fn generate(seed: u32) -> Self {
Self { sim: sim::WorldSim::generate(seed) }
}
pub fn tick(&mut self, dt: Duration) {
// TODO
}
pub fn generate_chunk(chunk_pos: Vec2<i32>) -> TerrainChunk {

44
world/src/sim.rs Normal file
View File

@ -0,0 +1,44 @@
use noise::{NoiseFn, OpenSimplex, Seedable};
use crate::WORLD_SIZE;
pub struct WorldSim {
seed: u32,
chunks: Vec<SimChunk>,
}
impl WorldSim {
pub fn generate(seed: u32) -> Self {
let mut gen_ctx = GenCtx {
alt_nz: OpenSimplex::new()
.set_seed(seed),
};
let mut chunks = Vec::new();
for x in 0..WORLD_SIZE.x {
for y in 0..WORLD_SIZE.y {
chunks.push(SimChunk::generate(&mut gen_ctx));
}
}
Self {
seed,
chunks,
}
}
}
struct GenCtx {
alt_nz: OpenSimplex,
}
struct SimChunk {
alt: f32,
}
impl SimChunk {
pub fn generate(gen_ctx: &mut GenCtx) -> Self {
Self {
alt: 0.0
}
}
}