This commit is contained in:
parent
1273953723
commit
57db0aff22
5 changed files with 54 additions and 8 deletions
|
|
@ -2,4 +2,6 @@ use bevy::{prelude::*, render::renderer};
|
||||||
|
|
||||||
#[derive(Component, Default)]
|
#[derive(Component, Default)]
|
||||||
#[require(Transform)]
|
#[require(Transform)]
|
||||||
pub struct Collider;
|
pub struct BoxCollider {
|
||||||
|
pub dimensions: Vec2,
|
||||||
|
}
|
||||||
|
|
@ -5,7 +5,7 @@ use bevy::prelude::*;
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
#[require(Sprite, PhysicsBody, SphereCollider)]
|
#[require(Sprite, PhysicsBody, SphereCollider)]
|
||||||
pub struct Kirby {
|
pub struct Kirby {
|
||||||
move_speed: f32,
|
speed_force: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn kirby_spawn(mut commands: Commands, asset_server: Res<AssetServer>) {
|
pub fn kirby_spawn(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||||
|
|
@ -14,10 +14,10 @@ pub fn kirby_spawn(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||||
mass: 10.0,
|
mass: 10.0,
|
||||||
force: Vec2::ZERO,
|
force: Vec2::ZERO,
|
||||||
velocity: Vec2::ZERO,
|
velocity: Vec2::ZERO,
|
||||||
drag: 0.01,
|
drag: 0.05,
|
||||||
};
|
};
|
||||||
let transform = Transform::from_xyz(0.0, 0.0, 0.0).with_scale(Vec3::ONE * 0.25);
|
let transform = Transform::from_xyz(0.0, 0.0, 0.0).with_scale(Vec3::ONE * 0.25);
|
||||||
commands.spawn((Kirby { move_speed: 200.0 }, transform, sprite, body));
|
commands.spawn((Kirby { speed_force: 50.0 }, transform, sprite, body));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_dir(keys: Res<ButtonInput<KeyCode>>) -> Vec2 {
|
pub fn get_dir(keys: Res<ButtonInput<KeyCode>>) -> Vec2 {
|
||||||
|
|
@ -52,7 +52,7 @@ pub fn kirby_player_move(
|
||||||
}
|
}
|
||||||
let dir = get_dir(keys);
|
let dir = get_dir(keys);
|
||||||
for (mut body, kirby) in &mut query {
|
for (mut body, kirby) in &mut query {
|
||||||
body.velocity = dir * kirby.move_speed;
|
body.velocity += dir * kirby.speed_force;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,16 +2,22 @@ use bevy::prelude::*;
|
||||||
use camera::spawn_camera;
|
use camera::spawn_camera;
|
||||||
use kirby::kirby_player_move;
|
use kirby::kirby_player_move;
|
||||||
use kirby::kirby_spawn;
|
use kirby::kirby_spawn;
|
||||||
|
use map::MapBounds;
|
||||||
use physics_body::integrate;
|
use physics_body::integrate;
|
||||||
|
|
||||||
mod camera;
|
mod camera;
|
||||||
mod kirby;
|
mod kirby;
|
||||||
|
mod map;
|
||||||
mod physics_body;
|
mod physics_body;
|
||||||
mod sphere_collider;
|
mod sphere_collider;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
.add_plugins(DefaultPlugins)
|
.add_plugins(DefaultPlugins)
|
||||||
|
.insert_resource(MapBounds {
|
||||||
|
min: Vec2::ONE * -400.0,
|
||||||
|
max: Vec2::ONE * 400.0,
|
||||||
|
})
|
||||||
.add_systems(Startup, spawn_camera)
|
.add_systems(Startup, spawn_camera)
|
||||||
.add_systems(Startup, kirby_spawn)
|
.add_systems(Startup, kirby_spawn)
|
||||||
.add_systems(FixedUpdate, (kirby_player_move, integrate).chain())
|
.add_systems(FixedUpdate, (kirby_player_move, integrate).chain())
|
||||||
|
|
|
||||||
17
src/map.rs
Normal file
17
src/map.rs
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
use bevy::{prelude::*, render::renderer};
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct MapRoot;
|
||||||
|
|
||||||
|
#[derive(Resource)]
|
||||||
|
pub struct MapBounds {
|
||||||
|
pub min: Vec2,
|
||||||
|
pub max: Vec2,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn setup_map_bounds(mut commands: Commands) {
|
||||||
|
commands.insert_resource(MapBounds {
|
||||||
|
min: Vec2::new(-320.0, -240.0),
|
||||||
|
max: Vec2::new(320.0, 240.0),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
use bevy::{prelude::*, render::renderer};
|
use crate::map::MapBounds;
|
||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
#[require(Transform)]
|
#[require(Transform)]
|
||||||
|
|
@ -20,7 +21,11 @@ impl Default for PhysicsBody {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn integrate(mut query: Query<(&mut Transform, &mut PhysicsBody)>, time: Res<Time<Fixed>>) {
|
pub fn integrate(
|
||||||
|
bounds: Res<MapBounds>,
|
||||||
|
mut query: Query<(&mut Transform, &mut PhysicsBody)>,
|
||||||
|
time: Res<Time<Fixed>>,
|
||||||
|
) {
|
||||||
for (mut transform, mut physicsbody) in &mut query {
|
for (mut transform, mut physicsbody) in &mut query {
|
||||||
let force = physicsbody.force;
|
let force = physicsbody.force;
|
||||||
let mass = physicsbody.mass;
|
let mass = physicsbody.mass;
|
||||||
|
|
@ -28,11 +33,27 @@ pub fn integrate(mut query: Query<(&mut Transform, &mut PhysicsBody)>, time: Res
|
||||||
let delta_secs = time.delta_secs();
|
let delta_secs = time.delta_secs();
|
||||||
|
|
||||||
physicsbody.velocity += force * delta_secs / mass;
|
physicsbody.velocity += force * delta_secs / mass;
|
||||||
// Drag because stability is good you know
|
|
||||||
physicsbody.velocity *= 1.0 - drag;
|
physicsbody.velocity *= 1.0 - drag;
|
||||||
|
|
||||||
|
bounce(&mut physicsbody.velocity, &transform, &bounds);
|
||||||
|
|
||||||
transform.translation.x += physicsbody.velocity.x * delta_secs;
|
transform.translation.x += physicsbody.velocity.x * delta_secs;
|
||||||
transform.translation.y += physicsbody.velocity.y * delta_secs;
|
transform.translation.y += physicsbody.velocity.y * delta_secs;
|
||||||
|
|
||||||
physicsbody.force = Vec2::ZERO;
|
physicsbody.force = Vec2::ZERO;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn bounce(velocity: &mut Vec2, transform: &Transform, bounds: &MapBounds) {
|
||||||
|
if (transform.translation.x < bounds.min.x && velocity.x < 0.0)
|
||||||
|
|| (transform.translation.x > bounds.max.x && velocity.x > 0.0)
|
||||||
|
{
|
||||||
|
velocity.x *= -1.5;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (transform.translation.y < bounds.min.y && velocity.y < 0.0)
|
||||||
|
|| (transform.translation.y > bounds.max.y && velocity.y > 0.0)
|
||||||
|
{
|
||||||
|
velocity.y *= -1.5;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue