world-digger-amethyst

[Archive] Game about Digging a lot using upgradable tools. Made using the Amethyst Engine. Might resume work on it eventually.
git clone https://git.jojolepro.com/world-digger-amethyst.git
Log | Files | Refs | README | LICENSE

commit 365af45a4c821abfaec41d827d06f7abbb61c1f9
parent c9276b7435f524c2e293944bf7a9cae84162b9e1
Author: Joël Lupien (Jojolepro) <jojolepromain@gmail.com>
Date:   Fri,  9 Mar 2018 11:35:23 -0500

weird entity recreation leads to layouting not being applied

Diffstat:
Msrc/main.rs | 83++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
Msrc/ui.rs | 156+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
2 files changed, 197 insertions(+), 42 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -36,7 +36,7 @@ use amethyst::core::cgmath::{Deg, Array, Basis3,Basis2, One, Point3, Quaternion, mod player; use player::{Tool,Backpack,BlockDefinition,BlockDefinitions,BlockInstance,Inventory,UiUpdaterSystem,MineProgress}; mod ui; -use ui::{create_game_ui,load_tool_icon}; +use ui::{create_game_ui,load_tool_icon,UiShit,load_ui_shit,create_buy_ui}; /* TODO @@ -92,10 +92,10 @@ impl<'a> System<'a> for MiningSystem { println!("hit bounding volume of {:?} at point {:?}", v.value, p); - if let Some(mut f) = force.get_mut(v.value){ + /*if let Some(mut f) = force.get_mut(v.value){ f.add_force(Vector3::new(1.0,-10.0,1.0)); println!("ADDING FORCE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); - } + }*/ // TODO raycast lookat + dist check, are we mining same block that we were @@ -137,6 +137,12 @@ impl<'a> System<'a> for MiningSystem { } +/* +fn query_ray(tree: & DynamicBoundingVolumeTree3<f32>, ray: Ray3<f32>) -> Vec<(TreeValueWrapped<Entity, Aabb3<f32>>, Point3<f32>)> { + let mut visitor = ContinuousVisitor::new(&ray); + tree.query(&mut visitor) +} +*/ @@ -165,6 +171,50 @@ impl Component for ObjectType { } +fn event_was_key_pressed(event: Event,key: VirtualKeyCode)->bool{ + match event { + Event::WindowEvent { event, .. } => match event { + WindowEvent::KeyboardInput { + input: + KeyboardInput { + virtual_keycode, + state: ElementState::Pressed, + .. + }, + .. + } => match virtual_keycode { + Some(key2) if key == key2 => return true, + _ => (), + }, + _ => (), + }, + _ => (), + } + false +} + + +struct BuyMenuState{ + local_entities: Vec<Entity>, +} + +impl State for BuyMenuState{ + fn on_start(&mut self, mut world: &mut World){ + self.local_entities = create_buy_ui(&mut world); + } + + fn on_stop(&mut self, mut world: &mut World){ + world.delete_entities(self.local_entities.as_slice()); + } + fn handle_event(&mut self, _: &mut World, event: Event) -> Trans { + if event_was_key_pressed(event,VirtualKeyCode::P){ + println!("Pop buy menu"); + return Trans::Pop; + } + Trans::None + } +} + struct ExampleState; impl State for ExampleState { @@ -184,7 +234,7 @@ impl State for ExampleState { let radius = 4; - let cube_size = 2.0; + let cube_size = 1.0; let mut comps: Vec<(Material, Transform)> = vec![]; @@ -227,7 +277,7 @@ impl State for ExampleState { ObjectType::Box, ), BodyPose3::new(Point3::new(c.1.translation.x, c.1.translation.y,c.1.translation.z), Quaternion::one()), - Velocity3::from_linear(Vector3::new(0.0,-10.0,0.0)), + Velocity3::from_linear(Vector3::new(0.0,0.0,0.0)), RigidBody::default(), Mass3::new(1.0), ) @@ -253,7 +303,7 @@ impl State for ExampleState { Cuboid::new(50.0, 5.0,50.0).into(), ObjectType::Box, ), - BodyPose3::new(Point3::new(trans.translation.x, trans.translation.y,trans.translation.z), Quaternion::zero()), + BodyPose3::new(Point3::new(trans.translation.x, trans.translation.y,trans.translation.z), Quaternion::one()), RigidBody::default(), Mass3::infinite(), ) @@ -308,7 +358,7 @@ impl State for ExampleState { } fn handle_event(&mut self, _: &mut World, event: Event) -> Trans { - match event { + /*match event { Event::WindowEvent { event, .. } => match event { WindowEvent::KeyboardInput { input: @@ -325,7 +375,15 @@ impl State for ExampleState { _ => (), }, _ => (), + }*/ + + if event_was_key_pressed(event,VirtualKeyCode::X){ + println!("Push buy menu"); + return Trans::Push(Box::new(BuyMenuState{ + local_entities: Vec::<Entity>::new(), + })); } + Trans::None } @@ -391,19 +449,8 @@ fn run() -> Result<(), Error> { InputBundle::<String, String>::new().with_bindings_from_file(&key_bindings_path), )? .with_bundle(RenderBundle::new(pipeline_builder, Some(display_config)))? - //.with_bundle(DefaultBasicPhysicsBundle3::<ObjectType>::new())? .with_bundle(SpatialPhysicsBundle3::<Primitive3<f32>,Aabb3<f32>,ObjectType>::new())? .with(UiUpdaterSystem,"ui_updater",&[]) - - //PHYSICS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - - /*.with(SpatialSortingSystem3::<f32, BodyPose3<f32>, ObjectType>::new(),"1",&[]) - .with(SpatialCollisionSystem3::<f32, BodyPose3<f32>, ObjectType>::new().with_narrow_phase(GJK3::new()),"2",&["1"]) - .with(CurrentFrameUpdateSystem3::<f32>::new(),"3",&["2"]) - .with(NextFrameSetupSystem3::<f32>::new(),"4",&["3"]) - .with(ContactResolutionSystem3::<f32>::new(reader_2),"5",&["4"]) - .with_resource(EventChannel::<ContactEvent<Entity,Point3<f32>>>::new()) - .with_resource(channel)*/ .with(MiningSystem::new(),"mining",&[]) .with_bundle(TransformBundle::new().with_dep(&["fly_movement","sync_system"]))? .build()?; diff --git a/src/ui.rs b/src/ui.rs @@ -3,29 +3,60 @@ use amethyst::core::Parent; use amethyst::ui::{Anchor, Anchored, DrawUi, FontAsset, MouseReactive, Stretch, Stretched, TtfFormat, UiBundle, UiEvent, UiFocused, UiImage, UiText, UiTransform,TextEditing}; -use amethyst::ecs::World; +use amethyst::ecs::{World,Entity,Component,VecStorage,FetchMut}; use amethyst::renderer::Texture; +#[derive(Clone)] +pub struct UiShit{ + font: Handle<FontAsset>, + red: Handle<Texture>, + green: Handle<Texture>, + blue: Handle<Texture>, + gray_overlay: Handle<Texture>, + brown: Handle<Texture>, +} + +impl Component for UiShit{ + type Storage = VecStorage<Self>; +} +pub fn load_ui_shit(world: &World) -> UiShit{ + let loader = world.read_resource::<Loader>(); + let font = loader.load( + "fonts/Nunito-Black.ttf", + TtfFormat, + Default::default(), + (), + &world.read_resource::<AssetStorage<FontAsset>>(), + ); + let tex_storage = world.read_resource(); + let red = loader.load_from_data([1.0, 0.0, 0.0, 1.0].into(), (), &tex_storage); + let green = loader.load_from_data([0.0, 1.0, 0.0, 1.0].into(), (), &tex_storage); + let blue = loader.load_from_data([0.0, 0.0, 1.0, 1.0].into(), (), &tex_storage); + let gray_overlay = loader.load_from_data([0.8, 0.8, 0.8, 0.8].into(), (), &tex_storage); + let brown = loader.load_from_data([0.54, 0.27, 0.07, 1.0].into(), (), &tex_storage); -pub fn create_game_ui(world: &mut World){ - let (font,red,blue,green) = { - let loader = world.read_resource::<Loader>(); - let font = loader.load( - "fonts/Nunito-Black.ttf", - TtfFormat, - Default::default(), - (), - &world.read_resource::<AssetStorage<FontAsset>>(), - ); - let tex_storage = world.read_resource(); - let red = loader.load_from_data([1.0, 0.0, 0.0, 1.0].into(), (), &tex_storage); - let blue = loader.load_from_data([0.0, 0.0, 1.0, 1.0].into(), (), &tex_storage); - let green = loader.load_from_data([0.0, 1.0, 0.0, 1.0].into(), (), &tex_storage); - (font,red,blue,green) - }; + UiShit{ + font, + red, + green, + blue, + gray_overlay, + brown, + } +} +pub fn fetch_ui(mut world: &mut World) -> UiShit{ + if let Some(ui) = world.res.try_fetch::<UiShit>(0){ + return ui.clone(); + } + let ui = load_ui_shit(&world); + world.add_resource(ui.clone()); + ui +} +pub fn create_game_ui(mut world: &mut World){ + let ui = fetch_ui(&mut world); world .create_entity() .with(UiTransform::new( @@ -38,7 +69,7 @@ pub fn create_game_ui(world: &mut World){ 1, )) .with(UiText::new( - font.clone(), + ui.font.clone(), "0$".to_string(), [0.2, 0.2, 1.0, 1.0], 50., @@ -58,7 +89,7 @@ pub fn create_game_ui(world: &mut World){ 1, )) .with(UiText::new( - font.clone(), + ui.font.clone(), "".to_string(), [0.2, 0.2, 1.0, 1.0], 50., @@ -78,7 +109,7 @@ pub fn create_game_ui(world: &mut World){ 1, )) .with(UiText::new( - font.clone(), + ui.font.clone(), "".to_string(), [0.2, 0.2, 1.0, 1.0], 50., @@ -98,7 +129,7 @@ pub fn create_game_ui(world: &mut World){ 1, )) .with(UiText::new( - font.clone(), + ui.font.clone(), "0 Kg".to_string(), [0.2, 0.2, 1.0, 1.0], 50., @@ -118,7 +149,7 @@ pub fn create_game_ui(world: &mut World){ 1, )) .with(UiImage { - texture: red.clone(), + texture: ui.red.clone(), }) .with(Anchored::new(Anchor::BottomMiddle)) .build(); @@ -136,7 +167,7 @@ pub fn create_game_ui(world: &mut World){ 1, )) .with(UiImage { - texture: green.clone(), + texture: ui.green.clone(), }) .with(Anchored::new(Anchor::MiddleLeft)) .build(); @@ -153,18 +184,95 @@ pub fn create_game_ui(world: &mut World){ -1, )) .with(UiText::new( - font.clone(), + ui.font.clone(), "Sell".to_string(), [0.0, 0.0, 0.0, 1.0], 50., )) .with(Anchored::new(Anchor::Middle)) + .with(Stretched::new(Stretch::XY)) .with(Parent{ entity: sell_btn, }) .build(); } + +pub fn create_buy_ui(mut world: &mut World)->Vec<Entity>{ + let ui = fetch_ui(&mut world); + let mut out = Vec::<Entity>::new(); + + let bg = world + .create_entity() + .with(UiTransform::new( + "shopbg".to_string(), + 0., + 0., + -10., + 0., + 0., + 1, + )) + .with(UiImage { + texture: ui.gray_overlay.clone(), + }) + .with(Anchored::new(Anchor::Middle)) + .with(Stretched::new(Stretch::XY)) + .build(); + + let tool_bg = world + .create_entity() + .with(UiTransform::new( + "toolbg".to_string(), + 270., + 0., + -1., + 500., + 0., + 1, + )) + .with(UiImage { + texture: ui.brown.clone(), + }) + .with(Anchored::new(Anchor::MiddleLeft)) + .with(Stretched::new(Stretch::Y).with_margin(0.0,20.0)) + .with(Parent{ + entity: bg.clone(), + }) + .build(); + + out.push(world + .create_entity() + .with(UiTransform::new( + "shop".to_string(), + 0., + 0., + -1., + 500., + 75., + 1, + )) + .with(UiText::new( + ui.font.clone(), + "shop".to_string(), + [0.2, 0.2, 1.0, 1.0], + 50., + )) + .with(Anchored::new(Anchor::Middle)) + .with(Parent{ + entity: bg.clone(), + }) + .build()); + + println!("tool_bg: {:?}",tool_bg); + + out.push(bg); + out.push(tool_bg); + + out +} + + pub fn load_tool_icon(world: &World, name: String) -> Handle<Texture>{ let loader = world.read_resource::<Loader>(); let tex_storage = world.read_resource();