Skip to content

Commit

Permalink
feat: add updates needed for Jumpy. (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
zicklag committed Sep 14, 2023
1 parent 6bee45d commit d10e2c1
Show file tree
Hide file tree
Showing 24 changed files with 897 additions and 670 deletions.
470 changes: 278 additions & 192 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ allow = [
"CC0-1.0",
"ISC",
"BSD-2-Clause",
"BSD-2-Clause-Patent",
"OpenSSL",
]
default = "deny"
Expand Down
8 changes: 7 additions & 1 deletion framework_crates/bones_asset/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,13 @@ impl AssetIo for FileAssetIo {
Some(folder) => self.packs_dir.join(folder),
None => self.core_dir.clone(),
};
let path = base_dir.join(loc.path);
// Make sure absolute paths are relative to pack.
let path = if loc.path.is_absolute() {
loc.path.strip_prefix("/").unwrap()
} else {
loc.path
};
let path = base_dir.join(path);
Ok(std::fs::read(path)?)
}

Expand Down
3 changes: 1 addition & 2 deletions framework_crates/bones_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/// Helper to export the same types in the crate root and in the prelude.
macro_rules! pub_use {
() => {
pub use crate::{asset::*, cid::*, handle::*, io::*, path::*, serde::*, server::*};
pub use crate::{asset::*, cid::*, handle::*, io::*, path::*, server::*};
pub use anyhow;
pub use bones_schema::prelude::*;
pub use semver::Version;
Expand All @@ -27,5 +27,4 @@ mod handle;
mod io;
mod parse;
mod path;
mod serde;
mod server;
56 changes: 0 additions & 56 deletions framework_crates/bones_asset/src/serde.rs

This file was deleted.

13 changes: 9 additions & 4 deletions framework_crates/bones_asset/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ impl AssetServer {

fn impl_load_asset(&mut self, loc: AssetLocRef, force: bool) -> anyhow::Result<UntypedHandle> {
let contents = self.io.load_file(loc).context(format!(
"Could not load asset file: {:?} from path {:?}",
"Could not load asset file: {:?} from pack {:?}",
loc.path,
loc.pack.unwrap_or("[core]")
))?;
Expand Down Expand Up @@ -886,13 +886,18 @@ mod metadata {
{
// SOUND: schema asserts this is a SchemaMap.
let v = unsafe { &mut *(self.ptr.as_ptr() as *mut SchemaMap) };
if v.key_schema() != String::schema() {
let is_ustr = v.key_schema() == Ustr::schema();
if v.key_schema() != String::schema() && !is_ustr {
return Err(A::Error::custom(
"Can only deserialize maps with string keys.",
"Can only deserialize maps with `String` or `Ustr` keys.",
));
}
while let Some(key) = map.next_key::<String>()? {
let key = SchemaBox::new(key);
let key = if is_ustr {
SchemaBox::new(ustr(&key))
} else {
SchemaBox::new(key)
};
let mut value = SchemaBox::default(v.value_schema());
map.next_value_seed(SchemaPtrLoadCtx {
ctx: self.ctx,
Expand Down
17 changes: 16 additions & 1 deletion framework_crates/bones_ecs/src/entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ use crate::prelude::*;
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Entity(u32, u32);
impl Entity {
/// An invalid entity, useful for placeholder entity values.
const INVALID: Entity = Entity(u32::MAX, u32::MAX);

/// Creates a new `Entity` from the provided index and generation.
///
/// > ⚠️ **Warning:** It is not generally recommended to manually create [`Entity`]s unless you
Expand All @@ -41,6 +44,11 @@ impl Entity {
self.1
}
}
impl Default for Entity {
fn default() -> Self {
Self::INVALID
}
}

/// Holds a list of alive entities.
///
Expand Down Expand Up @@ -330,7 +338,14 @@ impl Entities {
if i >= BITSET_SIZE {
panic!("Exceeded maximum amount of concurrent entities.");
}
Entity::new(i as u32, self.generation[i])
let entity = Entity::new(i as u32, self.generation[i]);

// Make sure we never return the invalid entity.
if unlikely(entity == Entity::INVALID) {
panic!("Ran out of entity IDs");
}

entity
}
}

Expand Down
2 changes: 2 additions & 0 deletions framework_crates/bones_framework/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ document-features = ["dep:document-features"]
# Bones
bones_lib = { version = "0.3", path = "../bones_lib", features = ["glam"] }
bones_asset = { version = "0.3", path = "../bones_asset" }
bones_schema = { version = "0.3", path = "../bones_schema", features = ["humantime"] }

# Other
glam = "0.24"
thiserror = "1.0"
instant = "0.1"
hex = "0.4"
tracing = "0.1"
noise = "0.8"

# Render
csscolorparser = "0.6"
Expand Down
26 changes: 15 additions & 11 deletions framework_crates/bones_framework/src/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

use crate::prelude::*;

use std::sync::Arc;

pub(crate) mod prelude {
pub use super::{AnimatedSprite, AnimationBankSprite};
}
Expand All @@ -17,13 +15,15 @@ pub fn animation_plugin(core: &mut Session) {

/// Component that may be added to entities with an [`AtlasSprite`] to animate them.
#[derive(Clone, HasSchema, Debug)]
#[repr(C)]
pub struct AnimatedSprite {
/// The current frame in the animation.
pub index: usize,
pub index: u32,
/// The frames in the animation.
///
/// These are the indexes into the atlas, specified in the order they will be played.
pub frames: Arc<[u32]>,
// TODO: Put Animation Frames in an `Arc` to Avoid Snapshot Clone Cost.
pub frames: SVec<u32>,
/// The frames per second to play the animation at.
pub fps: f32,
/// The amount of time the current frame has been playing
Expand All @@ -45,19 +45,21 @@ fn default_true() -> bool {
#[derive(Clone, HasSchema, Debug, Default)]
pub struct AnimationBankSprite {
/// The current animation.
pub current: Key,
pub current: Ustr,
/// The collection of animations in this animation bank.
pub animations: Arc<HashMap<Key, AnimatedSprite>>,
// TODO: Put Animation Frames in an `Arc` to Avoid Snapshot Clone Cost.
// TODO: Use more economic key type such as `ustr`
pub animations: SMap<Ustr, AnimatedSprite>,
#[cfg_attr(feature = "serde", serde(default))]
/// The last animation that was playing.
pub last_animation: Key,
pub last_animation: Ustr,
}

impl Default for AnimatedSprite {
fn default() -> Self {
Self {
index: 0,
frames: Arc::from([]),
frames: default(),
fps: 0.0,
timer: 0.0,
repeat: true,
Expand All @@ -82,18 +84,20 @@ pub fn animate_sprites(
animated_sprite.timer += time.delta_seconds();

// If we are ready to go to the next frame
if (animated_sprite.index != animated_sprite.frames.len() - 1 || animated_sprite.repeat)
if (animated_sprite.index != animated_sprite.frames.len() as u32 - 1
|| animated_sprite.repeat)
&& animated_sprite.timer > 1.0 / animated_sprite.fps.max(f32::MIN_POSITIVE)
{
// Restart the timer
animated_sprite.timer = 0.0;

// Increment and loop around the current index
animated_sprite.index = (animated_sprite.index + 1) % animated_sprite.frames.len();
animated_sprite.index =
(animated_sprite.index + 1) % animated_sprite.frames.len() as u32;
}

// Set the atlas sprite to match the current frame of the animated sprite
atlas_sprite.index = animated_sprite.frames[animated_sprite.index];
atlas_sprite.index = animated_sprite.frames[animated_sprite.index as usize];
}
}

Expand Down
Loading

0 comments on commit d10e2c1

Please sign in to comment.