From 92ddddc29edd9dc7eb7eef556211f4a06eca7754 Mon Sep 17 00:00:00 2001 From: Trevor Settles Date: Thu, 7 Dec 2023 20:16:00 -0700 Subject: [PATCH] feat: failed acceptance test in the expected way --- src/lib.rs | 50 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d453b8c..4254f5c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,27 +1,59 @@ use bevy::prelude::*; -struct BasePlugin; -impl Plugin for BasePlugin { - fn build(&self, _app: &mut App) { - //app.; - } -} - struct CoreLogicPlugin; impl Plugin for CoreLogicPlugin { fn build(&self, app: &mut App) { - // app.; + app.add_event::(); + + app.add_systems(Update, foo); } } +fn foo(mut creations: EventReader, mut commands: Commands) { + for spawn in creations.read() { + match spawn { + SpawnUnit::Villager(entity, pos) => commands.entity(*entity).insert(Transform { + translation: Vec3::new(pos.x, pos.y, 0.0), + ..Default::default() + }), + }; + } +} + +#[derive(Debug, Event)] +enum SpawnUnit { + Villager(Entity, Vec2), +} + +#[derive(Debug, Event)] +enum PlayerCommand { + Move(Entity, Vec2), +} + #[cfg(test)] mod test { use super::*; #[test] fn move_a_villager() { + // arrange let mut app = App::new(); - app.add_plugins((MinimalPlugins, BasePlugin, CoreLogicPlugin)); + app.add_plugins((MinimalPlugins, CoreLogicPlugin)); + + let ent = app.world.spawn_empty().id(); + + let init_pos = Vec2::new(1.0, 1.0); + app.world.send_event(SpawnUnit::Villager(ent, init_pos)); + + // act + let goal_pos = Vec2::new(0.0, 0.0); + app.world.send_event(PlayerCommand::Move(ent, goal_pos)); + app.update(); + + // assert + // Within 5 seconds, the villager should be closer than where it started + let actual = app.world.get::(ent).unwrap(); + assert!(init_pos.length() > actual.translation.length()); } }