-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7aee19c
commit 401a692
Showing
5 changed files
with
163 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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,51 @@ | ||
use bevy::prelude::*; | ||
use rand::{ | ||
distributions::{Distribution, Standard}, | ||
Rng, | ||
}; | ||
|
||
// Player Component | ||
#[derive(Component)] | ||
pub struct Player { | ||
pub direction: MoveDirection, | ||
pub speed: f32, | ||
pub id: u32, | ||
} | ||
|
||
// Enemy Component | ||
#[derive(Component)] | ||
pub struct Enemy { | ||
pub direction: MoveDirection, | ||
pub speed: f32, | ||
} | ||
|
||
#[derive(Component)] | ||
pub enum Collider { | ||
Player, | ||
Enemy, | ||
} | ||
|
||
// MoveDirection Component | ||
#[derive(PartialEq, Copy, Clone)] | ||
pub enum MoveDirection { | ||
Left, | ||
Up, | ||
Right, | ||
Down, | ||
None, | ||
} | ||
|
||
/// | ||
/// Implement Distribution Trait to get a random direction | ||
/// see => https://stackoverflow.com/questions/48490049/how-do-i-choose-a-random-value-from-an-enum | ||
impl Distribution<MoveDirection> for Standard { | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> MoveDirection { | ||
match rng.gen_range(0..=3) { | ||
// rand 0.8 | ||
0 => MoveDirection::Up, | ||
1 => MoveDirection::Down, | ||
2 => MoveDirection::Right, | ||
_ => MoveDirection::Left, | ||
} | ||
} | ||
} |
This file contains 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 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 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,102 @@ | ||
use crate::*; | ||
|
||
pub struct PlayerPlugin; | ||
|
||
impl Plugin for PlayerPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_systems(OnEnter(AppState::InGame), setup_player) | ||
.add_systems(Update, player_input.run_if(in_state(AppState::InGame))) | ||
.add_systems(Update, player_movement.run_if(in_state(AppState::InGame))) | ||
.add_systems(OnExit(AppState::InGame), teardown); | ||
} | ||
} | ||
|
||
/// | ||
/// Player Setup | ||
/// | ||
fn setup_player(mut commands: Commands) { | ||
commands | ||
.spawn(SpriteBundle { | ||
sprite: Sprite { | ||
color: PLAYER_COLOR, | ||
custom_size: Some(Vec2::new(16.0, 16.0)), | ||
..Default::default() | ||
}, | ||
..Default::default() | ||
}) | ||
.insert(Player { | ||
speed: 250.0, | ||
direction: MoveDirection::Up, | ||
id: rand::random::<u32>(), | ||
}) | ||
.insert(Collider::Player); | ||
} | ||
|
||
/// | ||
/// Player Movement | ||
/// | ||
fn player_movement(mut players: Query<(&Player, &mut Transform)>) { | ||
let (player, mut transform) = players.single_mut(); | ||
let translation = &mut transform.translation; | ||
// println!("Player Input System: {}", player.id); | ||
|
||
match &player.direction { | ||
&MoveDirection::Left => { | ||
translation.x -= player.speed * TIME_STEP; | ||
} | ||
MoveDirection::Right => { | ||
translation.x += player.speed * TIME_STEP; | ||
} | ||
MoveDirection::Up => { | ||
translation.y += player.speed * TIME_STEP; | ||
} | ||
MoveDirection::Down => { | ||
translation.y -= player.speed * TIME_STEP; | ||
} | ||
MoveDirection::None => { | ||
// println!("Stop") | ||
} | ||
} | ||
// Keep in bounds | ||
translation.x = translation.x.min(WINDOW_WIDTH / 2.).max(-WINDOW_WIDTH / 2.); | ||
translation.y = translation | ||
.y | ||
.min(WINDOW_HEIGHT / 2.) | ||
.max(-WINDOW_HEIGHT / 2.); | ||
} | ||
|
||
/// | ||
/// Player Input | ||
/// | ||
pub fn player_input( | ||
mut next_state: ResMut<NextState<AppState>>, | ||
keyboard_input: Res<Input<KeyCode>>, | ||
mut players: Query<&mut Player>, | ||
) { | ||
if let Some(mut player) = players.iter_mut().next() { | ||
let dir: MoveDirection = | ||
if keyboard_input.pressed(KeyCode::Up) || keyboard_input.pressed(KeyCode::K) { | ||
MoveDirection::Up | ||
} else if keyboard_input.pressed(KeyCode::Down) || keyboard_input.pressed(KeyCode::J) { | ||
MoveDirection::Down | ||
} else if keyboard_input.pressed(KeyCode::Left) || keyboard_input.pressed(KeyCode::H) { | ||
MoveDirection::Left | ||
} else if keyboard_input.pressed(KeyCode::Right) || keyboard_input.pressed(KeyCode::L) { | ||
MoveDirection::Right | ||
} else { | ||
MoveDirection::None | ||
}; | ||
|
||
player.direction = dir; | ||
} | ||
|
||
if keyboard_input.pressed(KeyCode::Escape) { | ||
next_state.set(AppState::GameOver) | ||
} | ||
} | ||
|
||
fn teardown(mut commands: Commands, players: Query<Entity, With<Player>>) { | ||
for player in players.iter() { | ||
commands.entity(player).despawn_recursive(); | ||
} | ||
} |
This file contains 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