veloren/client/src/lib.rs

125 lines
3.8 KiB
Rust
Raw Normal View History

2019-01-02 17:23:31 +00:00
// Standard
use std::time::Duration;
2019-01-15 15:13:11 +00:00
// Library
use specs::Entity as EcsEntity;
use vek::*;
2019-01-23 20:01:58 +00:00
use threadpool;
2019-01-15 15:13:11 +00:00
// Project
use common::{comp::phys::Vel, state::State, terrain::TerrainChunk};
2019-01-15 15:13:11 +00:00
use world::World;
2019-01-02 17:23:31 +00:00
#[derive(Debug)]
pub enum Error {
2019-01-02 17:23:31 +00:00
ServerShutdown,
Other(String),
}
pub struct Input {
// TODO: Use this type to manage client input
pub move_dir: Vec2<f32>,
2019-01-02 17:23:31 +00:00
}
pub struct Client {
2019-01-23 20:01:58 +00:00
thread_pool: threadpool::ThreadPool,
2019-01-02 17:23:31 +00:00
2019-01-23 20:01:58 +00:00
tick: u64,
state: State,
2019-01-15 15:13:11 +00:00
player: Option<EcsEntity>,
// Testing
world: World,
pub chunk: Option<TerrainChunk>,
2019-01-02 17:23:31 +00:00
}
impl Client {
/// Create a new `Client`.
2019-01-02 17:23:31 +00:00
pub fn new() -> Self {
Self {
2019-01-23 20:01:58 +00:00
thread_pool: threadpool::Builder::new()
.thread_name("veloren-worker".into())
.build(),
2019-01-15 15:13:11 +00:00
2019-01-23 20:01:58 +00:00
tick: 0,
state: State::new(),
2019-01-15 15:13:11 +00:00
player: None,
// Testing
world: World::new(),
chunk: None,
2019-01-02 17:23:31 +00:00
}
}
2019-01-23 20:01:58 +00:00
/// Get a reference to the client's worker thread pool. This pool should be used for any
/// computationally expensive operations that run outside of the main thread (i.e: threads that
/// block on I/O operations are exempt).
pub fn thread_pool(&self) -> &threadpool::ThreadPool { &self.thread_pool }
// TODO: Get rid of this
2019-01-15 15:13:11 +00:00
pub fn with_test_state(mut self) -> Self {
self.chunk = Some(self.world.generate_chunk(Vec3::zero()));
self.player = Some(self.state.new_test_player());
2019-01-15 15:13:11 +00:00
self
}
2019-01-23 20:01:58 +00:00
// TODO: Get rid of this
pub fn load_chunk(&mut self, pos: Vec3<i32>) {
self.state.terrain_mut().insert(pos, self.world.generate_chunk(pos));
self.state.changes_mut().new_chunks.push(pos);
}
/// Get a reference to the client's game state.
pub fn state(&self) -> &State { &self.state }
/// Get a mutable reference to the client's game state.
pub fn state_mut(&mut self) -> &mut State { &mut self.state }
/// Get the player entity
pub fn player(&self) -> Option<EcsEntity> {
self.player
}
2019-01-23 20:01:58 +00:00
/// Get the current tick number.
pub fn get_tick(&self) -> u64 {
self.tick
}
2019-01-02 17:23:31 +00:00
/// Execute a single client tick, handle input and update the game state by the given duration
pub fn tick(&mut self, input: Input, dt: Duration) -> Result<(), Error> {
2019-01-02 17:23:31 +00:00
// This tick function is the centre of the Veloren universe. Most client-side things are
// managed from here, and as such it's important that it stays organised. Please consult
// the core developers before making significant changes to this code. Here is the
// approximate order of things. Please update it as this code changes.
//
// 1) Collect input from the frontend, apply input effects to the state of the game
// 2) Go through any events (timer-driven or otherwise) that need handling and apply them
// to the state of the game
// 3) Perform a single LocalState tick (i.e: update the world and entities in the world)
// 4) Go through the terrain update queue and apply all changes to the terrain
// 5) Finish the tick, passing control of the main thread back to the frontend
// (step 1)
if let Some(p) = self.player {
// TODO: remove this
const PLAYER_VELOCITY: f32 = 100.0;
// TODO: Set acceleration instead
self.state.write_component(p, Vel(Vec3::from(input.move_dir * PLAYER_VELOCITY)));
}
2019-01-02 17:23:31 +00:00
// Tick the client's LocalState (step 3)
self.state.tick(dt);
// Finish the tick, pass control back to the frontend (step 6)
2019-01-23 20:01:58 +00:00
self.tick += 1;
2019-01-02 17:23:31 +00:00
Ok(())
}
2019-01-23 20:01:58 +00:00
/// Clean up the client after a tick
pub fn cleanup(&mut self) {
// Cleanup the local state
self.state.cleanup();
}
2019-01-02 17:23:31 +00:00
}