mirror of
https://github.com/kristoferssolo/maze-ascension.git
synced 2025-10-21 19:20:34 +00:00
feat(player): add tile movement
This commit is contained in:
parent
0fb3504b81
commit
4ac52cfb38
@ -1,7 +1,20 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
use hexx::Hex;
|
||||||
|
|
||||||
#[derive(Component, Debug, Clone, Copy, PartialEq, Default, Reflect)]
|
#[derive(Component, Debug, Clone, Copy, PartialEq, Reflect)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct Player {
|
pub struct Player {
|
||||||
pub speed: f32,
|
pub speed: f32,
|
||||||
|
pub current_hex: Hex,
|
||||||
|
pub target_hex: Option<Hex>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Player {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
speed: 50.,
|
||||||
|
current_hex: Hex::ZERO,
|
||||||
|
target_hex: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
use bevy::prelude::*;
|
|
||||||
use hexx::{EdgeDirection, Hex};
|
|
||||||
|
|
||||||
use crate::{maze::MazeConfig, player::components::Player};
|
use crate::{maze::MazeConfig, player::components::Player};
|
||||||
|
use bevy::prelude::*;
|
||||||
|
use hexx::EdgeDirection;
|
||||||
|
|
||||||
const fn create_direction(key: &KeyCode) -> Option<EdgeDirection> {
|
const fn create_direction(key: &KeyCode) -> Option<EdgeDirection> {
|
||||||
match key {
|
match key {
|
||||||
@ -18,21 +17,36 @@ const fn create_direction(key: &KeyCode) -> Option<EdgeDirection> {
|
|||||||
pub fn player_movement(
|
pub fn player_movement(
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
input: Res<ButtonInput<KeyCode>>,
|
input: Res<ButtonInput<KeyCode>>,
|
||||||
mut player_query: Query<(&Player, &mut Transform)>,
|
mut player_query: Query<(&mut Player, &mut Transform)>,
|
||||||
maze_config: Res<MazeConfig>,
|
maze_config: Res<MazeConfig>,
|
||||||
) {
|
) {
|
||||||
for (player, mut transform) in player_query.iter_mut() {
|
for (mut player, mut transform) in player_query.iter_mut() {
|
||||||
let direction = input.get_pressed().find_map(|key| create_direction(key));
|
if let Some(direction) = input.get_pressed().find_map(|key| create_direction(key)) {
|
||||||
|
let next_hex = player.current_hex + direction.into_hex();
|
||||||
|
player.target_hex = Some(next_hex);
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(hex_dir) = direction {
|
if let Some(target_hex) = player.target_hex {
|
||||||
let hex_vec = Hex::from(hex_dir);
|
let current_pos = transform.translation;
|
||||||
let world_pos = maze_config.layout.hex_to_world_pos(hex_vec);
|
let target_pos = {
|
||||||
|
let world_pos = maze_config.layout.hex_to_world_pos(target_hex);
|
||||||
|
Vec3::new(world_pos.x, current_pos.y, world_pos.y)
|
||||||
|
};
|
||||||
|
let direction = target_pos - current_pos;
|
||||||
|
let distance = direction.length();
|
||||||
|
|
||||||
let move_vec = Vec3::new(world_pos.x, 0.0, world_pos.y).normalize()
|
if distance < 0.1 {
|
||||||
* player.speed
|
transform.translation = target_pos;
|
||||||
* time.delta_seconds();
|
player.current_hex = target_hex;
|
||||||
|
player.target_hex = None;
|
||||||
transform.translation += move_vec;
|
} else {
|
||||||
|
let movement = direction.normalize() * player.speed * time.delta_seconds();
|
||||||
|
if movement.length() > distance {
|
||||||
|
transform.translation = target_pos;
|
||||||
|
} else {
|
||||||
|
transform.translation += movement;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,7 @@ pub fn spawn_player(
|
|||||||
|
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
Name::new("Player"),
|
Name::new("Player"),
|
||||||
Player { speed: 50. },
|
Player::default(),
|
||||||
PbrBundle {
|
PbrBundle {
|
||||||
mesh: meshes.add(generate_pill_mesh(player_radius, player_height / 2.)),
|
mesh: meshes.add(generate_pill_mesh(player_radius, player_height / 2.)),
|
||||||
material: materials.add(blue_material()),
|
material: materials.add(blue_material()),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user