Started a player system and got input handling working
This commit is contained in:
parent
a6bdaace98
commit
d171be23d9
15 changed files with 396 additions and 21 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -3,3 +3,5 @@ target/
|
|||
|
||||
Cargo.lock
|
||||
|
||||
*.dll
|
||||
|
||||
|
|
11
Cargo.toml
11
Cargo.toml
|
@ -5,7 +5,16 @@ authors = ["Gnarwhal <git.aspect893@passmail.net>"]
|
|||
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"]
|
||||
|
|
21
LICENSE
Normal file
21
LICENSE
Normal file
|
@ -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.
|
|
@ -1,4 +0,0 @@
|
|||
(
|
||||
axes: {},
|
||||
actions: {}
|
||||
)
|
|
@ -1,4 +0,0 @@
|
|||
(
|
||||
title: "DJam 4!",
|
||||
dimensions: Some((500, 500))
|
||||
)
|
34
resources/bindings.ron
Normal file
34
resources/bindings.ron
Normal file
|
@ -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)],
|
||||
],
|
||||
}
|
||||
)
|
5
resources/display_config.ron
Normal file
5
resources/display_config.ron
Normal file
|
@ -0,0 +1,5 @@
|
|||
(
|
||||
title: "DJam 4",
|
||||
resizable: true,
|
||||
dimensions: Some((1280, 720)),
|
||||
)
|
BIN
resources/icon.png
Normal file
BIN
resources/icon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.8 KiB |
31
src/components/mod.rs
Normal file
31
src/components/mod.rs
Normal file
|
@ -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::{
|
||||
player::Player
|
||||
};
|
||||
|
||||
mod player;
|
45
src/components/player.rs
Normal file
45
src/components/player.rs
Normal file
|
@ -0,0 +1,45 @@
|
|||
/*******************************************************************************
|
||||
*
|
||||
* 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::{
|
||||
ecs::prelude::{Component, DenseVecStorage},
|
||||
};
|
||||
|
||||
pub struct Player {
|
||||
pub id: u32,
|
||||
}
|
||||
|
||||
impl Player {
|
||||
pub fn new(id: u32) -> Player {
|
||||
return Player{
|
||||
id
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl Component for Player {
|
||||
type Storage = DenseVecStorage<Self>;
|
||||
}
|
33
src/main.rs
33
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::<StringBindings>::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::<systems::PlayerBindings>::new()
|
||||
.with_bindings_from_file(binding_path)?;
|
||||
|
||||
let game_data = GameDataBuilder::default()
|
||||
.with_bundle(TransformBundle::new())?
|
||||
.with_bundle(input_bundle)?
|
||||
.with_bundle(
|
||||
RenderingBundle::<DefaultBackend>::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(())
|
||||
|
|
67
src/states/level.rs
Normal file
67
src/states/level.rs
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
31
src/states/mod.rs
Normal file
31
src/states/mod.rs
Normal file
|
@ -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;
|
32
src/systems/mod.rs
Normal file
32
src/systems/mod.rs
Normal file
|
@ -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;
|
97
src/systems/player.rs
Normal file
97
src/systems/player.rs
Normal file
|
@ -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<PlayerBindings>>,
|
||||
);
|
||||
|
||||
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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue