-
Notifications
You must be signed in to change notification settings - Fork 0
[WIP] Supermath101's work on the player and controls #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Supermath101
wants to merge
3
commits into
main
Choose a base branch
from
supermath101
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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/ |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
singleplayerandplayercrates together. Does anybody have a better idea?There was a problem hiding this comment.
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.