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

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.