Skip to content
This repository has been archived by the owner on Nov 10, 2024. It is now read-only.

Commit

Permalink
more use of TimewarpComponent trait alias
Browse files Browse the repository at this point in the history
  • Loading branch information
RJ committed Aug 30, 2023
1 parent af93ee4 commit fbca70e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
16 changes: 8 additions & 8 deletions src/components.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{FrameBuffer, FrameNumber};
use crate::{FrameBuffer, FrameNumber, TimewarpComponent};
use bevy::prelude::*;

/// entities with NotRollbackable are ignored, even if they have components which
Expand Down Expand Up @@ -58,11 +58,11 @@ impl Anachronous {
/// commands.entity(e).insert(InsertComponentAtFrame::<Shield>(shield_comp, past_frame))
/// ```
#[derive(Component, Debug)]
pub struct InsertComponentAtFrame<T: Component + Clone + std::fmt::Debug> {
pub struct InsertComponentAtFrame<T: TimewarpComponent> {
pub component: T,
pub frame: FrameNumber,
}
impl<T: Component + Clone + std::fmt::Debug> InsertComponentAtFrame<T> {
impl<T: TimewarpComponent> InsertComponentAtFrame<T> {
pub fn new(frame: FrameNumber, component: T) -> Self {
Self { component, frame }
}
Expand All @@ -74,18 +74,18 @@ impl<T: Component + Clone + std::fmt::Debug> InsertComponentAtFrame<T> {
/// ie, the values before and after the rollback differ.
/// in your game, look for Changed<TimewarpCorrection<T>> and use for any visual smoothing/interp stuff.
#[derive(Component, Debug, Clone)]
pub struct TimewarpCorrection<T: Component + Clone + std::fmt::Debug> {
pub struct TimewarpCorrection<T: TimewarpComponent> {
pub before: T,
pub after: T,
pub frame: FrameNumber,
}

/// Buffers the last few authoritative component values received from the server
#[derive(Component)]
pub struct ServerSnapshot<T: Component + Clone + std::fmt::Debug> {
pub struct ServerSnapshot<T: TimewarpComponent> {
pub values: FrameBuffer<T>,
}
impl<T: Component + Clone + std::fmt::Debug> ServerSnapshot<T> {
impl<T: TimewarpComponent> ServerSnapshot<T> {
pub fn with_capacity(len: usize) -> Self {
Self {
values: FrameBuffer::with_capacity(len),
Expand All @@ -105,7 +105,7 @@ pub type FrameRange = (FrameNumber, Option<FrameNumber>);

/// Buffers component values for the last few frames.
#[derive(Component)]
pub struct ComponentHistory<T: Component + Clone + std::fmt::Debug> {
pub struct ComponentHistory<T: TimewarpComponent> {
pub values: FrameBuffer<T>, // not pub!
pub alive_ranges: Vec<FrameRange>, // inclusive! unlike std:range
/// when we insert at this frame, compute diff between newly inserted val and whatever already exists in the buffer.
Expand All @@ -117,7 +117,7 @@ pub struct ComponentHistory<T: Component + Clone + std::fmt::Debug> {

// lazy first version - don't need a clone each frame if value hasn't changed!
// just store once and reference from each unchanged frame number.
impl<T: Component + Clone + std::fmt::Debug> ComponentHistory<T> {
impl<T: TimewarpComponent> ComponentHistory<T> {
pub fn with_capacity(len: usize, birth_frame: FrameNumber) -> Self {
let mut this = Self {
values: FrameBuffer::with_capacity(len),
Expand Down
8 changes: 2 additions & 6 deletions tests/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn setup_test_app() -> App {
// and do 1 app.update
pub fn tick(app: &mut App) {
let mut fxt = app.world.resource_mut::<FixedTime>();
let period = fxt.period.clone();
let period = fxt.period;
info!("<tick>");
fxt.tick(period);
app.update();
Expand All @@ -46,11 +46,7 @@ pub fn tick(app: &mut App) {

// some syntactic sugar, just to make tests less of an eyesore:
pub(crate) trait TimewarpTestTraits {
fn comp_val_at<T: Component + Clone + std::fmt::Debug>(
&self,
entity: Entity,
frame: FrameNumber,
) -> Option<&T>;
fn comp_val_at<T: TimewarpComponent>(&self, entity: Entity, frame: FrameNumber) -> Option<&T>;
}

impl TimewarpTestTraits for App {
Expand Down

0 comments on commit fbca70e

Please sign in to comment.