Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ bevy = { version = "0.6" }
bevy_ecs_tilemap = "0.5"
bevy_egui = "0.11"
bevy_rapier2d = "0.12"
anyhow = "1.0"
anyhow = "1.0"
leafwing-input-manager = "0.2"
1 change: 1 addition & 0 deletions assets/asset-licenses.csv
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Asset Name,License,Author,Link
images/player.png,Unknown,clipartstation.com,https://clipartstation.com/human-figure-clipart-4/
Binary file added assets/images/player.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin};
use bevy::prelude::*;
use bevy::window::WindowMode;
use bevy_egui::EguiPlugin;
use leafwing_input_manager::plugin::InputManagerPlugin;

mod menus;
mod singleplayer;
Expand Down Expand Up @@ -35,8 +36,10 @@ fn main() {
.add_plugin(FrameTimeDiagnosticsPlugin::default())
.add_plugin(EguiPlugin)
.add_plugin(menus::MainMenuScene)
// This plugin maps inputs to an input-type agnostic action-state
// We need to provide it with an enum which stores the possible actions a player could take
.add_plugin(InputManagerPlugin::<singleplayer::player::Action>::default())
.add_plugin(singleplayer::SinglePlayerScene)
.add_state(GameState::MainMenu)
.run();
}

5 changes: 5 additions & 0 deletions src/menus/common.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use bevy::prelude::*;

/// Marks a UI element as disabled.
#[derive(Component)]
pub struct Disabled;

pub const NORMAL_COLOR: UiColor = UiColor(Color::rgb(0.15, 0.15, 0.15));
pub const HOVERED_COLOR: UiColor = UiColor(Color::rgb(0.25, 0.25, 0.25));
pub const PRESSED_COLOR: UiColor = UiColor(Color::rgb(0.35, 0.75, 0.35));

/// Creates the [`Style`]s that we add to most buttons.
pub fn button_style() -> Style {
Style {
size: Size::new(Val::Px(150.0), Val::Px(35.0)),
Expand All @@ -17,6 +19,7 @@ pub fn button_style() -> Style {
}
}

/// Creates the [`Style`]s that we add to most text.
pub fn text_style() -> Style {
Style {
align_self: AlignSelf::Center,
Expand All @@ -26,6 +29,7 @@ pub fn text_style() -> Style {
}
}

/// Creates the [`TextStyle`]s that we add to most text.
pub fn text_textstyle(asset_server: &AssetServer) -> TextStyle {
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
Expand All @@ -42,6 +46,7 @@ pub fn text_textstyle_disabled(asset_server: &AssetServer) -> TextStyle {
}
}

/// Creates the [`TextAlignment`]s that we add to most buttons.
pub fn button_text_alignment() -> TextAlignment {
TextAlignment {
horizontal: HorizontalAlign::Center,
Expand Down
8 changes: 7 additions & 1 deletion src/singleplayer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use bevy::prelude::*;
use bevy_ecs_tilemap::prelude::*;

pub mod player;
use crate::GameState;

pub struct SinglePlayerScene;
Expand All @@ -10,7 +11,12 @@ impl Plugin for SinglePlayerScene {
app.add_plugin(TilemapPlugin)
.add_system(crate::utils::set_texture_filters_to_nearest)
.add_system_set(
SystemSet::on_enter(GameState::Playing).with_system(singleplayer_setup),
SystemSet::on_enter(GameState::Playing)
.with_system(singleplayer_setup.label("2d_map_setup"))
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the next line is a hacky way to integrate the singleplayer and player crates together. Does anybody have a better idea?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine. Alternatively you can create a single setup system and from there call the create map and create player fns but then you would need to pass in the system params.

.with_system(player::setup_player.after("2d_map_setup")),
)
.add_system_set(
SystemSet::on_update(GameState::Playing).with_system(player::player_movement),
);
}
}
Expand Down
115 changes: 115 additions & 0 deletions src/singleplayer/player.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
use bevy::{math::const_vec2, prelude::*};
use leafwing_input_manager::prelude::*;
use std::ops::{Add, Not};

pub fn setup_player(mut commands: Commands, asset_server: Res<AssetServer>) {
commands
.spawn_bundle(SpriteBundle {
texture: asset_server.load("images/player.png"),
transform: Transform::from_xyz(0., 0., 0.),
sprite: Sprite {
custom_size: Some(Vec2::new(32.0, 64.0)),
..Default::default()
},
..Default::default()
})
.insert(Player)
.insert(MovementDirection::NotMoving)
.insert_bundle(InputManagerBundle::<Action> {
// Stores "which virtual action buttons are currently pressed"
action_state: ActionState::default(),
// Stores how those actions relate to inputs from your player
input_map: InputMap::new([
(Action::MoveLeft, KeyCode::A),
(Action::MoveRight, KeyCode::D),
]),
});
}

pub fn player_movement(
time: Res<Time>,
mut player: Query<(&mut Transform, &mut MovementDirection, &ActionState<Action>), With<Player>>,
) {
let (mut transform, mut direction, input) = player.single_mut();
//todo!();
for action in input.get_just_pressed().iter() {
*direction = *direction + MovementDirection::from_action(*action);
}

for action in input.get_just_released().iter() {
*direction = *direction + !MovementDirection::from_action(*action);
}

transform.translation += direction.as_vec2().extend(0.) * time.delta_seconds() * 250.;
}

/// The player.
#[derive(Debug, Component, Clone, Copy)]
pub struct Player;

/// The possible keyboard button actions a player can do.
#[derive(Actionlike, PartialEq, Eq, Clone, Copy, Hash, Debug)]
pub enum Action {
MoveLeft,
MoveRight,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Component)]
pub enum MovementDirection {
Left,
Right,
NotMoving,
}

impl MovementDirection {
pub fn from_action(action: Action) -> Self {
use MovementDirection::*;
match action {
Action::MoveLeft => Left,
Action::MoveRight => Right,
_ => NotMoving,
}
}

pub const fn as_vec2(self) -> Vec2 {
match self {
MovementDirection::Left => const_vec2!([-1., 0.]),
MovementDirection::Right => const_vec2!([1., 0.]),
MovementDirection::NotMoving => const_vec2!([0., 0.]),
}
}

pub fn from_vec2(vec2: Vec2) -> Self {
let vec2 = vec2.normalize_or_zero();
use MovementDirection::*;
if vec2.x >= 0.5 {
Right
} else if vec2.x <= -0.5 {
Left
} else {
NotMoving
}
}
}

impl Add for MovementDirection {
type Output = Self;

/// Finds the overall direction headed when two directions are given.
fn add(self, rhs: Self) -> Self::Output {
Self::from_vec2(self.as_vec2() + rhs.as_vec2())
}
}

impl Not for MovementDirection {
type Output = Self;

fn not(self) -> Self::Output {
use MovementDirection::*;
match self {
Left => Right,
Right => Left,
NotMoving => NotMoving,
}
}
}