Tests pass! Still havent drawn anything but we are making progress

main
Elijah Voigt 2 days ago
parent c9d962df8d
commit c3bc656435

@ -17,7 +17,10 @@ impl Plugin for BlocksPlugin {
.init_asset_loader::<ShapeAssetLoader>() .init_asset_loader::<ShapeAssetLoader>()
.add_systems(OnEnter(Loading(true)), load_assets.run_if(run_once)) .add_systems(OnEnter(Loading(true)), load_assets.run_if(run_once))
.add_systems(OnEnter(GameState::Setup), (setup_camera, setup_blocks)) .add_systems(OnEnter(GameState::Setup), (setup_camera, setup_blocks))
.add_systems(Update, updated_shape_asset.run_if(on_message::<AssetEvent<ShapeAsset>>)) .add_systems(
Update,
updated_shape_asset.run_if(on_message::<AssetEvent<ShapeAsset>>),
)
.add_observer(add_shape); .add_observer(add_shape);
} }
} }
@ -53,7 +56,7 @@ impl ShapeAsset {
} }
/// Block positions relative to the shape's center /// Block positions relative to the shape's center
#[derive(Component, PartialEq, Debug)] #[derive(Component, PartialEq, Debug, Clone, Copy)]
pub(crate) struct RelativePosition { pub(crate) struct RelativePosition {
pub x: isize, pub x: isize,
pub y: isize, pub y: isize,
@ -70,20 +73,37 @@ impl From<(isize, isize)> for RelativePosition {
pub(crate) struct ShapeLayout(pub Vec<Vec<u8>>); pub(crate) struct ShapeLayout(pub Vec<Vec<u8>>);
impl ShapeLayout { impl ShapeLayout {
pub(crate) fn positions(&self) -> [RelativePosition;4] { pub(crate) fn positions(&self) -> [RelativePosition; 4] {
let mut c: Vec<RelativePosition> = Vec::with_capacity(4); let mut c: Vec<RelativePosition> = Vec::with_capacity(4);
let center = { let center: (isize, isize) = {
// This could be less lines of code but it would be confusing af
let y = if self.0.len() % 2 == 1 {
self.0.len() / 2
} else {
self.0.len() / 2 - 1
};
let x = if self.0[0].len() % 2 == 1 {
self.0[0].len() / 2
} else {
self.0[0].len() / 2 - 1
};
(x as isize, y as isize)
}; };
for (y, nested) in self.0.iter().enumerate() {
for (y, nested) in self.0.iter().rev().enumerate() {
for (x, val) in nested.iter().enumerate() { for (x, val) in nested.iter().enumerate() {
if *val == 1 { if *val == 1 {
println!("{x}{y}"); c.push(RelativePosition {
x: x as isize - center.0,
y: y as isize - center.1,
});
} }
} }
} }
todo!() [c[0], c[1], c[2], c[3]]
} }
} }
@ -195,17 +215,16 @@ fn updated_shape_asset(
mut messages: MessageReader<AssetEvent<ShapeAsset>>, mut messages: MessageReader<AssetEvent<ShapeAsset>>,
query: Query<&AssetComponent<ShapeAsset>>, query: Query<&AssetComponent<ShapeAsset>>,
) { ) {
messages.read().for_each(|asset_event| { messages.read().for_each(|asset_event| match asset_event {
match asset_event {
AssetEvent::Added { id } => debug!("Asset added: {id:?}"), AssetEvent::Added { id } => debug!("Asset added: {id:?}"),
AssetEvent::Modified { id } => query.iter().filter(|AssetComponent { handle }| { AssetEvent::Modified { id } => query
handle.id() == *id .iter()
}).for_each(|ac| { .filter(|AssetComponent { handle }| handle.id() == *id)
.for_each(|ac| {
warn!("TODO: Update {ac:?}"); warn!("TODO: Update {ac:?}");
}), }),
AssetEvent::Removed { id } => warn!("Asset removed: {id:?}"), AssetEvent::Removed { id } => warn!("Asset removed: {id:?}"),
AssetEvent::Unused { id } => warn!("Asset is unused: {id:?}"), AssetEvent::Unused { id } => warn!("Asset is unused: {id:?}"),
AssetEvent::LoadedWithDependencies { id } => debug!("Asset lodaed: {id:?}"), AssetEvent::LoadedWithDependencies { id } => debug!("Asset lodaed: {id:?}"),
}
}) })
} }

@ -1,70 +1,51 @@
use super::*; use super::*;
#[test] #[test]
fn test_shape_layout_01() { fn test_shape_layout_01a() {
let actual = ShapeLayout(vec![ let actual = ShapeLayout(vec![vec![0, 1, 0], vec![1, 1, 1]]).positions();
vec![0, 1, 0],
vec![1, 1, 1],
]).positions();
let expected: [RelativePosition;4] = [ let expected: [RelativePosition; 4] =
(1, 1).into(), [(-1, 0).into(), (0, 0).into(), (1, 0).into(), (0, 1).into()];
(-1, 0).into(),
(0, 0).into(), debug_assert_eq!(expected, actual);
(1, 0).into() }
];
#[test]
fn test_shape_layout_01b() {
let actual = ShapeLayout(vec![vec![1, 0], vec![1, 1], vec![1, 0]]).positions();
let expected: [RelativePosition; 4] =
[(0, -1).into(), (0, 0).into(), (1, 0).into(), (0, 1).into()];
debug_assert_eq!(expected, actual); debug_assert_eq!(expected, actual);
} }
#[test] #[test]
fn test_shape_layout_02a() { fn test_shape_layout_02a() {
let actual = ShapeLayout(vec![ let actual = ShapeLayout(vec![vec![1], vec![1], vec![1], vec![1]]).positions();
vec![1],
vec![1],
vec![1],
vec![1],
]).positions();
let expected: [RelativePosition;4] = [ let expected: [RelativePosition; 4] =
(0, 2).into(), [(0, -1).into(), (0, 0).into(), (0, 1).into(), (0, 2).into()];
(0, 1).into(),
(0, 0).into(),
(0, -1).into()
];
debug_assert_eq!(expected, actual); debug_assert_eq!(expected, actual);
} }
#[test] #[test]
fn test_shape_layout_02b() { fn test_shape_layout_02b() {
let actual = ShapeLayout(vec![ let actual = ShapeLayout(vec![vec![1, 1, 1, 1]]).positions();
vec![1, 1, 1, 1],
]).positions();
let expected: [RelativePosition;4] = [ let expected: [RelativePosition; 4] =
(-1, 0).into(), [(-1, 0).into(), (0, 0).into(), (1, 0).into(), (2, 0).into()];
(0, 0).into(),
(0, 1).into(),
(0, 2).into()
];
debug_assert_eq!(expected, actual); debug_assert_eq!(expected, actual);
} }
#[test] #[test]
fn test_shape_layout_03() { fn test_shape_layout_03() {
let actual = ShapeLayout(vec![ let actual = ShapeLayout(vec![vec![1, 1, 0], vec![0, 1, 1]]).positions();
vec![1, 1, 0],
vec![0, 1, 1],
]).positions();
let expected: [RelativePosition;4] = [ let expected: [RelativePosition; 4] =
(-1, 1).into(), [(0, 0).into(), (1, 0).into(), (-1, 1).into(), (0, 1).into()];
(0, 1).into(),
(0, 0).into(),
(1, 0).into()
];
debug_assert_eq!(expected, actual); debug_assert_eq!(expected, actual);
} }

Loading…
Cancel
Save