Physics refactored out
This commit is contained in:
BIN
assets/flowerfriend.png
Normal file
BIN
assets/flowerfriend.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 516 B |
61
src/main.rs
61
src/main.rs
@@ -4,7 +4,9 @@ mod player;
|
|||||||
|
|
||||||
use bevy::{image::Image, math::I64Vec2, prelude::*};
|
use bevy::{image::Image, math::I64Vec2, prelude::*};
|
||||||
|
|
||||||
use crate::player::Player;
|
use crate::{physics::PhysicsBody2D, player::Player};
|
||||||
|
|
||||||
|
const UNITS_TO_PX_SCALE: i64 = 256;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, States)]
|
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, States)]
|
||||||
enum AppState {
|
enum AppState {
|
||||||
@@ -151,10 +153,8 @@ fn levelload(
|
|||||||
));
|
));
|
||||||
match ei.identifier.as_str() {
|
match ei.identifier.as_str() {
|
||||||
"Player" => {
|
"Player" => {
|
||||||
spawned_ent.insert(Player {
|
spawned_ent.insert(Player {});
|
||||||
pos,
|
spawned_ent.insert(PhysicsBody2D { pos, ..default() });
|
||||||
vel: I64Vec2::ZERO,
|
|
||||||
});
|
|
||||||
spawned_ent.insert(Sprite::from_color(Color::WHITE, vec2(16., 16.)));
|
spawned_ent.insert(Sprite::from_color(Color::WHITE, vec2(16., 16.)));
|
||||||
}
|
}
|
||||||
"Parsnip" => {
|
"Parsnip" => {
|
||||||
@@ -173,10 +173,8 @@ fn levelload(
|
|||||||
|
|
||||||
fn gameloop(
|
fn gameloop(
|
||||||
keyb: Res<ButtonInput<KeyCode>>,
|
keyb: Res<ButtonInput<KeyCode>>,
|
||||||
mut player_sing: Single<(&mut Player, &mut Transform)>,
|
mut player_sing: Single<(&mut PhysicsBody2D, &mut Transform), With<Player>>,
|
||||||
) {
|
) {
|
||||||
const UNITS_TO_PX_SCALE: i64 = 100;
|
|
||||||
const MAX_SPEED: i64 = 2 * UNITS_TO_PX_SCALE;
|
|
||||||
let mut dir = I64Vec2::ZERO;
|
let mut dir = I64Vec2::ZERO;
|
||||||
|
|
||||||
if keyb.pressed(KeyCode::KeyA) || keyb.pressed(KeyCode::ArrowLeft) {
|
if keyb.pressed(KeyCode::KeyA) || keyb.pressed(KeyCode::ArrowLeft) {
|
||||||
@@ -195,51 +193,22 @@ fn gameloop(
|
|||||||
dir.y = -1;
|
dir.y = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
let player = player_sing.0.as_mut();
|
let play_phys = player_sing.0.as_mut();
|
||||||
|
|
||||||
// player vel and pos are in px / 100
|
// player vel and pos are in UNITS_TO_PX_SCALE
|
||||||
let mut p_vel = player.vel + dir * UNITS_TO_PX_SCALE * MAX_SPEED;
|
|
||||||
if dir.length_squared() == 0 {
|
if dir.length_squared() == 0 {
|
||||||
p_vel = (p_vel * 8) / 10;
|
// very bare bones player friction
|
||||||
info!("Slowing! {p_vel}");
|
play_phys.vel *= 6;
|
||||||
|
play_phys.vel /= 10;
|
||||||
} else {
|
} else {
|
||||||
info!("Changing vel: {p_vel}")
|
play_phys.add_vel(dir * UNITS_TO_PX_SCALE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// want unit vector with int math
|
play_phys.tick();
|
||||||
// normally this would be x / len, y / len
|
|
||||||
|
|
||||||
// eg (2,2) -> length of 4 -> (0.5, 0.5)
|
|
||||||
// how to do with squares?
|
|
||||||
|
|
||||||
// (2, 2) -> sqr length of 8 -> sqr x / 8 -> 0.5... yes?
|
|
||||||
// but we can't get half units obvs so we need to work in centi-units
|
|
||||||
// 200, 200 -> should become 50, 50
|
|
||||||
|
|
||||||
// max speed is 3 px per update
|
|
||||||
if p_vel.length_squared() >= MAX_SPEED.pow(2) {
|
|
||||||
let l = p_vel.length_squared();
|
|
||||||
let x = p_vel.x * p_vel.x;
|
|
||||||
let y = p_vel.y * p_vel.y;
|
|
||||||
|
|
||||||
p_vel.x = match p_vel.x {
|
|
||||||
..0 => (x * -MAX_SPEED) / l,
|
|
||||||
_ => (x * MAX_SPEED) / l,
|
|
||||||
};
|
|
||||||
p_vel.y = match p_vel.y {
|
|
||||||
..0 => (y * -MAX_SPEED) / l,
|
|
||||||
_ => (y * MAX_SPEED) / l,
|
|
||||||
};
|
|
||||||
|
|
||||||
info!("Nerfed speed {p_vel}");
|
|
||||||
}
|
|
||||||
|
|
||||||
player.vel = p_vel;
|
|
||||||
player.pos += p_vel;
|
|
||||||
|
|
||||||
*player_sing.1 = Transform::from_xyz(
|
*player_sing.1 = Transform::from_xyz(
|
||||||
(player.pos.x as f32) / UNITS_TO_PX_SCALE as f32,
|
(play_phys.pos.x as f32) / UNITS_TO_PX_SCALE as f32,
|
||||||
(player.pos.y as f32) / UNITS_TO_PX_SCALE as f32,
|
(play_phys.pos.y as f32) / UNITS_TO_PX_SCALE as f32,
|
||||||
100.,
|
100.,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
use bevy::{ecs::component::Component, math::I64Vec2};
|
||||||
|
|
||||||
|
const MAX_SPEED: i64 = 256;
|
||||||
|
|
||||||
|
#[derive(Default, Debug, Component)]
|
||||||
|
pub struct PhysicsBody2D {
|
||||||
|
pub pos: I64Vec2,
|
||||||
|
pub vel: I64Vec2,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PhysicsBody2D {
|
||||||
|
pub fn tick(&mut self) {
|
||||||
|
self.pos += self.vel;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_vel(&mut self, force: I64Vec2) {
|
||||||
|
let mut vel = self.vel + force;
|
||||||
|
if vel.length_squared() >= MAX_SPEED.pow(2) {
|
||||||
|
let l = vel.length_squared();
|
||||||
|
let x = vel.x * vel.x;
|
||||||
|
let y = vel.y * vel.y;
|
||||||
|
|
||||||
|
vel.x = match vel.x {
|
||||||
|
..0 => (x * -MAX_SPEED) / l,
|
||||||
|
_ => (x * MAX_SPEED) / l,
|
||||||
|
};
|
||||||
|
vel.y = match vel.y {
|
||||||
|
..0 => (y * -MAX_SPEED) / l,
|
||||||
|
_ => (y * MAX_SPEED) / l,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
self.vel = vel;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,10 +1,4 @@
|
|||||||
use bevy::{
|
use bevy::ecs::component::Component;
|
||||||
ecs::component::Component,
|
|
||||||
math::{I64Vec2, IVec2},
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Default, Debug, Component)]
|
#[derive(Default, Debug, Component)]
|
||||||
pub struct Player {
|
pub struct Player {}
|
||||||
pub pos: I64Vec2,
|
|
||||||
pub vel: I64Vec2,
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user