From d171be23d90953846033ec548db125c96873cb2d Mon Sep 17 00:00:00 2001 From: Gnarwhal Date: Wed, 7 Aug 2024 05:08:12 +0000 Subject: [PATCH] Started a player system and got input handling working --- .gitignore | 2 + Cargo.toml | 11 +++- LICENSE | 21 ++++++++ config/bindings.ron | 4 -- config/display.ron | 4 -- resources/bindings.ron | 34 ++++++++++++ resources/display_config.ron | 5 ++ resources/icon.png | Bin 0 -> 1861 bytes src/components/mod.rs | 31 +++++++++++ src/components/player.rs | 45 ++++++++++++++++ src/main.rs | 33 +++++++----- src/states/level.rs | 67 ++++++++++++++++++++++++ src/states/mod.rs | 31 +++++++++++ src/systems/mod.rs | 32 ++++++++++++ src/systems/player.rs | 97 +++++++++++++++++++++++++++++++++++ 15 files changed, 396 insertions(+), 21 deletions(-) create mode 100644 LICENSE delete mode 100644 config/bindings.ron delete mode 100644 config/display.ron create mode 100644 resources/bindings.ron create mode 100644 resources/display_config.ron create mode 100644 resources/icon.png create mode 100644 src/components/mod.rs create mode 100644 src/components/player.rs create mode 100644 src/states/level.rs create mode 100644 src/states/mod.rs create mode 100644 src/systems/mod.rs create mode 100644 src/systems/player.rs diff --git a/.gitignore b/.gitignore index 3f4c5cd..e120e7b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ target/ Cargo.lock +*.dll + diff --git a/Cargo.toml b/Cargo.toml index 6e234de..5d10895 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,16 @@ authors = ["Gnarwhal "] edition = "2018" [dependencies] -amethyst = "0.15.0" +amethyst_window = "0.5.0" +winit = "0.22.2" + +[dependencies.serde] +version = "1" +features = ["derive"] + +[dependencies.amethyst] +version = "0.15.0" +features = ["no-slow-safety-checks", "sdl_controller"] [features] default = ["vulkan"] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..70988e3 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +Mit License + +Copyright (c) 2020 Gnarly Narwhal + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the Software), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/config/bindings.ron b/config/bindings.ron deleted file mode 100644 index 093c4d2..0000000 --- a/config/bindings.ron +++ /dev/null @@ -1,4 +0,0 @@ -( - axes: {}, - actions: {} -) \ No newline at end of file diff --git a/config/display.ron b/config/display.ron deleted file mode 100644 index 3831bfa..0000000 --- a/config/display.ron +++ /dev/null @@ -1,4 +0,0 @@ -( - title: "DJam 4!", - dimensions: Some((500, 500)) -) \ No newline at end of file diff --git a/resources/bindings.ron b/resources/bindings.ron new file mode 100644 index 0000000..e50aae4 --- /dev/null +++ b/resources/bindings.ron @@ -0,0 +1,34 @@ +( + // YES! I KNOW THIS IS A BAD WAY TO SUPPORT CONTROLLERS! + // --- The Unnecessary Defense --- + // I'm new to the engine and it's a almost half way through a game jam and I have a black window to show for it, so please(!) give me a break 🥺? + axes: { + Horizontal(-1): Emulated(pos: ScanCode(32), neg: ScanCode(30)), + Horizontal( 0): Controller(controller_id: 0, axis: LeftX, invert: false, dead_zone: 0.1), + Horizontal( 1): Controller(controller_id: 1, axis: LeftX, invert: false, dead_zone: 0.1), + Horizontal( 2): Controller(controller_id: 2, axis: LeftX, invert: false, dead_zone: 0.1), + Horizontal( 3): Controller(controller_id: 3, axis: LeftX, invert: false, dead_zone: 0.1), + Horizontal( 4): Controller(controller_id: 4, axis: LeftX, invert: false, dead_zone: 0.1), + Horizontal( 5): Controller(controller_id: 5, axis: LeftX, invert: false, dead_zone: 0.1), + }, + actions: { + ShortHop: [ + [Controller(0, X)], + [Controller(1, X)], + [Controller(2, X)], + [Controller(3, X)], + [Controller(4, X)], + [Controller(5, X)], + ], + FullHop: [ + [ScanCode(17)], + [ScanCode(57)], + [Controller(0, A)], + [Controller(1, A)], + [Controller(2, A)], + [Controller(3, A)], + [Controller(4, A)], + [Controller(5, A)], + ], + } +) \ No newline at end of file diff --git a/resources/display_config.ron b/resources/display_config.ron new file mode 100644 index 0000000..524a494 --- /dev/null +++ b/resources/display_config.ron @@ -0,0 +1,5 @@ +( + title: "DJam 4", + resizable: true, + dimensions: Some((1280, 720)), +) \ No newline at end of file diff --git a/resources/icon.png b/resources/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..9e4ee167617ec0d2f003d75cd2e36a4445b11e88 GIT binary patch literal 1861 zcmeHH`!^H_6#nwaD@7jdjz?v%L>fh@F=0|3p~)&UB|Dg8l#oPf3KPZ}%S<+u;?PTI zmpqzLs10cutIf03N<=j+6^kT0{SEtT_ndpbd+#~-obP<+-kVDEa#2?$r~*)Tb9MHX zq2m`z1e9l}eF{(nI$-~jPJ3Xlq< z147_uAVVi9B|8v_o3oP--DNnt_gEiEBU#WY)bMtt zK1SP-r6KEA1pL4+n>t-WXmb!QBvaV!uXfd2*aV%*uqhJweRSCA&Mwp0cEYEuH`J$6 zHSD-_zOb)G@*j-x;B!W zXj|~t)9Lx+n;#AB9wX5oyS*W)Mqg|&XZSw+g@bb}8c|8B?fFJ#g7VQt+@NR1OU%{; zWE)dFPgNy1-U=0@@tR0#gL@Td-WEA>y3G-LB#ZNVTK(g$9j>x9a=l+{Pqa@9<*;V9 z585`zS-B^7GwJL)(IJztSd}Y9W+ips$A&XaZG=yJdrM3wp5~ahlZ^WWAKX}Ce)q8{ z?jtRAzNp$O^_ei-Sm8CxCaZRBcITY}Odm@B`?|YOzx!)yEBScsRQTu=Dz>N zH@#%o@9%gxVtKSUpXFf}*-U?;?oNp(=A;_e3$25T;)q<$;gAlq*CHO0#L+kIm~r@& zP^U{*cmrI%-htABiU%I6L$?{4$M-SF#0|WCR)U!W>z-usE#~X=DlRK=?ron9S(g{T z;Ydq9Q%c)%WP0+Ena1_Z#=IaYt$nB`Np8~`jer=x0I8E8wbPszuXrD6?$M!jIg}i*4W06?*Gg~)vAA3i(KHDxS(V&Tu+0WULqQ6!y&tkd%0MBw; zW$^Y4eQ$(?N)^wutY@lYcPuXFS&}B?!YX)E8l3kJsZ_NS@8te&5iX;x88X-YCN|+< zg|@QxTgCLe{=mGQm0joV_tQ=H(x>y)v?sZ%dOhS{NXrZ3ZbkH}B?MLEu4?+WCo1rF zx}BVLl2(PH+lXZ3VdVoU~a$yF1Bu*sTz=OtE$w3DJ6rqWKR`g~BhU9qssp^E&cZGJ>F zePCs+TFK~;N)o%P(kx_IahTPe$;P=Ai474X$v^ie$i%mT3*odSv-9IR-}S`S&FPb< zdl(JXYLcAWEL&z)>5R{y7U#n1Vyf@RCyh}<;q+4f8m_0qkK&v%I~yHB8B@<@V)?JR zEm_?$n!$ov7sq2SQo=JzEZPnHoZk#2u=+H!iRqh18H1aZR!!O8inZ0F_#389ul=cf zWA4y+YuPi)-DCa1cL|*Ly`4kkY7-IV Player { + return Player{ + id + }; + } +} + +impl Component for Player { + type Storage = DenseVecStorage; +} diff --git a/src/main.rs b/src/main.rs index abf6662..81d2a48 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,9 +24,14 @@ * *******************************************************************************/ +mod components; +mod states; +mod systems; + use amethyst::{ + core::TransformBundle, prelude::*, - input::{InputBundle, StringBindings}, + input::{InputBundle}, renderer::{ RenderingBundle, types::DefaultBackend, @@ -34,34 +39,38 @@ use amethyst::{ }, utils::application_root_dir, }; - -pub struct DJam; - -impl SimpleState for DJam {} +use amethyst_window::{DisplayConfig, Icon}; +use std::path::PathBuf; fn main() -> amethyst::Result<()> { amethyst::start_logger(Default::default()); let app_root = application_root_dir()?; - let assets_dir = app_root.join("assets"); - let display_config_path = app_root.join("config").join("display.ron"); - let binding_path = app_root.join("config").join("bindings.ron"); + let resources_dir = app_root.join("resources"); + let display_config_path = resources_dir.join("display_config.ron"); + let binding_path = resources_dir.join("bindings.ron"); - let input_bundle = InputBundle::::new().with_bindings_from_file(binding_path)?; + let mut display_config = DisplayConfig::load(display_config_path)?; + display_config.icon = Some(PathBuf::from("resources/icon.png")); + + let input_bundle = InputBundle::::new() + .with_bindings_from_file(binding_path)?; let game_data = GameDataBuilder::default() + .with_bundle(TransformBundle::new())? .with_bundle(input_bundle)? .with_bundle( RenderingBundle::::new() .with_plugin( - RenderToWindow::from_config_path(display_config_path)? + RenderToWindow::from_config(display_config) .with_clear([0.0, 0.0, 0.0, 1.0]), ) .with_plugin(RenderFlat2D::default()) - )?; + )? + .with(systems::PlayerSystem, "player_system", &["input_system"]); - let mut game = Application::new(assets_dir, DJam,game_data)?; + let mut game = Application::new(resources_dir, states::LevelState, game_data)?; game.run(); Ok(()) diff --git a/src/states/level.rs b/src/states/level.rs new file mode 100644 index 0000000..df06ebf --- /dev/null +++ b/src/states/level.rs @@ -0,0 +1,67 @@ +/******************************************************************************* + * + * Copyright (c) 2020 Gnarwhal + * + * ----------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files(the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + *******************************************************************************/ + +use amethyst::{ + assets::{AssetStorage, Loader, Handle}, + core::transform::Transform, + ecs::prelude::{Component, DenseVecStorage}, + prelude::*, + renderer::{Camera, ImageFormat, SpriteRender, SpriteSheet, SpriteSheetFormat, Texture} +}; +use crate::components::Player; + +pub const CAMERA_WIDTH: f32 = 1920.0; +pub const CAMERA_HEIGHT: f32 = 1080.0; + +fn initialize_camera(world: &mut World) { + let mut transform = Transform::default(); + transform.set_translation_xyz(CAMERA_WIDTH * 0.5, CAMERA_HEIGHT * 0.5, 1.0); + + world + .create_entity() + .with(Camera::standard_2d(CAMERA_WIDTH, CAMERA_HEIGHT)) + .with(transform) + .build(); +} + +fn initialize_player(world: &mut World) { + world + .create_entity() + .with(Player::new(0)) + .with(Transform::default()) + .build(); +} + +pub struct LevelState; + +impl SimpleState for LevelState { + fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) { + let world = data.world; + + initialize_camera(world); + initialize_player(world); + } +} \ No newline at end of file diff --git a/src/states/mod.rs b/src/states/mod.rs new file mode 100644 index 0000000..c100959 --- /dev/null +++ b/src/states/mod.rs @@ -0,0 +1,31 @@ +/******************************************************************************* + * + * Copyright (c) 2020 Gnarwhal + * + * ----------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files(the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + *******************************************************************************/ + +pub use self::{ + level::LevelState +}; + +mod level; \ No newline at end of file diff --git a/src/systems/mod.rs b/src/systems/mod.rs new file mode 100644 index 0000000..6c962ae --- /dev/null +++ b/src/systems/mod.rs @@ -0,0 +1,32 @@ +/******************************************************************************* + * + * Copyright (c) 2020 Gnarwhal + * + * ----------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files(the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + *******************************************************************************/ + +pub use self::{ + player::PlayerSystem, + player::PlayerBindings, +}; + +mod player; \ No newline at end of file diff --git a/src/systems/player.rs b/src/systems/player.rs new file mode 100644 index 0000000..1b8a58f --- /dev/null +++ b/src/systems/player.rs @@ -0,0 +1,97 @@ +/******************************************************************************* + * + * Copyright (c) 2020 Gnarwhal + * + * ----------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files(the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + *******************************************************************************/ + +use std::fmt::{self, Display}; + +use amethyst::{ + core::{Transform, SystemDesc}, + derive::SystemDesc, + ecs::{Join, Read, ReadStorage, System, SystemData, World, WriteStorage}, + input::{InputHandler, BindingTypes, Bindings}, +}; +use serde::{Serialize, Deserialize}; +use crate::components::Player; + +#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)] +pub enum MovementBindings { + Horizontal(i8), +} + +#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)] +pub enum ActionBindings { + ShortHop, + FullHop, +} + +impl Display for MovementBindings { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{:?}", self) + } +} + +impl Display for ActionBindings { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{:?}", self) + } +} + + +#[derive(Debug)] +pub struct PlayerBindings; + +impl BindingTypes for PlayerBindings { + type Axis = MovementBindings; + type Action = ActionBindings; +} + +#[derive(SystemDesc)] +pub struct PlayerSystem; + +impl <'s> System<'s> for PlayerSystem { + type SystemData = ( + WriteStorage<'s, Transform>, + ReadStorage<'s, Player>, + Read<'s, InputHandler>, + ); + + fn run(&mut self, (mut transforms, players, input): Self::SystemData) { + for (transform, _) in (&mut transforms, &players).join() { + let mut movement = 0.0f32; + for input_id in -1..6 { + movement += input + .axis_value(&MovementBindings::Horizontal(input_id)) + .unwrap_or(0.0); + } + + if movement != 0.0 { + println!("Movement: {}", movement); + } + if input.action_is_down(&ActionBindings::FullHop).unwrap_or(false) { + println!("Yump yump!"); + } + } + } +} \ No newline at end of file