Struct crystalorb::client::Client [−][src]
pub struct Client<WorldType: World> { /* fields omitted */ }
Expand description
This is the top-level structure of CrystalOrb for your game client, analogous to the
Server
for game servers. You create, store, and update this client
instance to run your game on the client side.
Implementations
Constructs a new Client
.
Examples
Using the default configuration parameters:
use crystalorb::{Config, client::Client}; use crystalorb_demo::DemoWorld; let client = Client::<DemoWorld>::new(Config::new());
Overriding some configuration parameter:
use crystalorb::{Config, client::Client}; use crystalorb_demo::DemoWorld; let client = Client::<DemoWorld>::new(Config { lag_compensation_latency: 0.5, ..Config::new() });
Perform the next update for the current rendering frame. You would typically call this in your game engine’s update loop of some kind.
Examples
Here is how one might call Client::update
outside of any game engine:
use crystalorb::{Config, client::Client}; use crystalorb_demo::DemoWorld; use crystalorb_mock_network::MockNetwork; use std::time::Instant; // Using a mock network as an example... let (_, (mut network, _)) = MockNetwork::new_mock_network::<DemoWorld>(); let mut client = Client::<DemoWorld>::new(Config::new()); let startup_time = Instant::now(); let mut previous_time = Instant::now(); loop { let current_time = Instant::now(); let delta_seconds = current_time.duration_since(previous_time).as_secs_f64(); let seconds_since_startup = current_time.duration_since(startup_time).as_secs_f64(); client.update(delta_seconds, seconds_since_startup, &mut network); // ...Other update code omitted... }
Get the current stage of the Client
, which provides access to extra functionality
depending on what stage it is currently in. See Client::stage_mut
, which provides
functionality to mutate the client in some way.
Example
To check on the client’s progress at connecting with the server, you can check the number
of clocksync samples during the client’s SyncingClock
stage:
use crystalorb::{Config, client::{Client, stage::Stage}}; use crystalorb_demo::DemoWorld; let client = Client::<DemoWorld>::new(Config::new()); // ...Later on... if let Stage::SyncingClock(syncing_clock_client) = client.stage() { println!( "Connection progress: {}%", syncing_clock_client.sample_count() as f64 / syncing_clock_client.samples_needed() as f64 * 100.0 ); }
Get the current stage f the Client
, which provides access to extra functionality
depending on what stage it is currently in. For shareable immutable version, see
Client::stage
.
Example
To issue a command, you’ll need to access the client in its Ready
stage:
use crystalorb::{Config, client::{Client, stage::StageMut}}; use crystalorb_demo::{DemoWorld, DemoCommand, PlayerSide, PlayerCommand}; use crystalorb_mock_network::MockNetwork; // Using a mock network as an example... let (_, (mut network, _)) = MockNetwork::new_mock_network::<DemoWorld>(); let mut client = Client::<DemoWorld>::new(Config::new()); // ...Later on... if let StageMut::Ready(mut ready_client) = client.stage_mut() { let command = DemoCommand::new(PlayerSide::Left, PlayerCommand::Jump, true); ready_client.issue_command(command, &mut network); }
Trait Implementations
Auto Trait Implementations
impl<WorldType> RefUnwindSafe for Client<WorldType> where
WorldType: RefUnwindSafe,
<WorldType as World>::CommandType: RefUnwindSafe,
<WorldType as World>::DisplayStateType: RefUnwindSafe,
<WorldType as World>::SnapshotType: RefUnwindSafe,
impl<WorldType> Unpin for Client<WorldType> where
WorldType: Unpin,
<WorldType as World>::DisplayStateType: Unpin,
<WorldType as World>::SnapshotType: Unpin,
impl<WorldType> UnwindSafe for Client<WorldType> where
WorldType: UnwindSafe,
<WorldType as World>::CommandType: RefUnwindSafe,
<WorldType as World>::DisplayStateType: UnwindSafe,
<WorldType as World>::SnapshotType: UnwindSafe,