do basic stuff like movement and walls

This commit is contained in:
Schrottkatze 2024-05-03 18:46:56 +02:00
parent 986bc287d0
commit af47eb6510
Signed by: schrottkatze
SSH key fingerprint: SHA256:hXb3t1vINBFCiDCmhRABHX5ocdbLiKyCdKI4HK2Rbbc
4 changed files with 84 additions and 32 deletions

3
.cargo/config.toml Normal file
View file

@ -0,0 +1,3 @@
[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=mold"]

21
Cargo.lock generated
View file

@ -293,7 +293,6 @@ version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65b9eadaacf8fe971331bc3f250f35c18bc9dace3f96b483062f38ac07e3a1b4" checksum = "65b9eadaacf8fe971331bc3f250f35c18bc9dace3f96b483062f38ac07e3a1b4"
dependencies = [ dependencies = [
"bevy_dylib",
"bevy_internal", "bevy_internal",
] ]
@ -470,15 +469,6 @@ dependencies = [
"sysinfo", "sysinfo",
] ]
[[package]]
name = "bevy_dylib"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "922826e3b8f37c19836b49e18ceca662260cce87ab8faa4db6df8433903660cc"
dependencies = [
"bevy_internal",
]
[[package]] [[package]]
name = "bevy_ecs" name = "bevy_ecs"
version = "0.13.2" version = "0.13.2"
@ -1065,6 +1055,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"bevy", "bevy",
"bevy_rapier2d", "bevy_rapier2d",
"some_bevy_tools",
] ]
[[package]] [[package]]
@ -2956,6 +2947,7 @@ dependencies = [
"parry2d", "parry2d",
"rustc-hash", "rustc-hash",
"simba", "simba",
"vec_map",
] ]
[[package]] [[package]]
@ -3242,6 +3234,15 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "some_bevy_tools"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85470658805b5915e9e84cddbdc582e2412e65df1f3299737142913430cd3209"
dependencies = [
"bevy",
]
[[package]] [[package]]
name = "spade" name = "spade"
version = "2.6.0" version = "2.6.0"

View file

@ -6,8 +6,10 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
bevy = { version = "0.13.0", features = ["dynamic_linking"] } # bevy = { version = "0.13.0", features = ["dynamic_linking"] }
bevy_rapier2d = "0.25.0" bevy = "0.13"
bevy_rapier2d = { version = "0.25.0", features = [ "simd-stable", "debug-render-2d" ] }
some_bevy_tools = "0.2.4"
# Enable a small amount of optimization in debug mode # Enable a small amount of optimization in debug mode
[profile.dev] [profile.dev]
@ -17,6 +19,6 @@ opt-level = 1
[profile.dev.package."*"] [profile.dev.package."*"]
opt-level = 3 opt-level = 3
[target.x86_64-unknown-linux-gnu] # Add this to your Cargo.toml
linker = "clang" [profile.dev.package.bevy_rapier2d]
rustflags = ["-C", "link-arg=-fuse-ld=/usr/bin/mold"] opt-level = 3

View file

@ -1,34 +1,69 @@
use bevy::prelude::*; use bevy::prelude::*;
use bevy_rapier2d::prelude::*; use bevy_rapier2d::prelude::*;
use player::{add_player, move_player}; use player::{add_player, move_player};
use some_bevy_tools::{camera_2d::Camera2DPlugin, controller_2d::SimpleTopDownControllerPlugin};
fn main() { fn main() {
App::new() App::new()
.insert_resource(RapierConfiguration {
gravity: Vec2::new(0.0, 0.0),
..Default::default()
})
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_plugins(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(100.0)) .add_plugins(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(64.0))
.add_plugins(RapierDebugRenderPlugin::default()) .add_plugins(RapierDebugRenderPlugin::default())
.add_systems(Startup, (setup_camera, add_player)) .add_plugins(Camera2DPlugin)
.add_plugins(SimpleTopDownControllerPlugin)
.add_systems(Startup, (scene::setup_scene, add_player))
.add_systems(Update, move_player) .add_systems(Update, move_player)
.run(); .run();
} }
fn setup_camera(mut commands: Commands) {
commands.spawn(Camera2dBundle {
transform: Transform::from_xyz(0., 0., 100.),
..default()
});
}
mod scene { mod scene {
use bevy::prelude::*; use bevy::{
prelude::*,
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
};
use bevy_rapier2d::prelude::*; use bevy_rapier2d::prelude::*;
const METER: f32 = 64.;
pub fn setup_scene(mut commands: Commands) {} pub fn setup_scene(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let scene_objs = [
// Arena walls
(Rectangle::new(16. * METER, METER), (0., 7.5 * METER)),
(Rectangle::new(16. * METER, METER), (0., -7.5 * METER)),
(Rectangle::new(METER, 16. * METER), (7.5 * METER, 0.)),
(Rectangle::new(METER, 16. * METER), (-7.5 * METER, 0.)),
(Rectangle::new(8. * METER, METER), (0., 2. * METER)),
(Rectangle::new(1. * METER, 2. * METER), (2. * METER, 0.)),
(Rectangle::new(8. * METER, METER), (0., -2. * METER)),
];
for (shape_, pos) in scene_objs {
let shape = Mesh2dHandle(meshes.add(shape_));
commands
.spawn(MaterialMesh2dBundle {
mesh: shape,
material: materials.add(Color::rgb(1., 0., 0.)),
transform: Transform::from_xyz(pos.0, pos.1, 1.),
..default()
})
.insert((
RigidBody::Fixed,
Collider::cuboid(shape_.half_size.x, shape_.half_size.y),
));
}
}
} }
mod player { mod player {
use bevy::prelude::*; use bevy::prelude::*;
use bevy_rapier2d::prelude::*; use bevy_rapier2d::prelude::*;
use some_bevy_tools::camera_2d::Camera2DController;
use std::f32::consts::PI; use std::f32::consts::PI;
const TURN_SPEED: f32 = PI / 100.; const TURN_SPEED: f32 = PI / 100.;
@ -42,7 +77,7 @@ mod player {
} }
pub fn add_player(mut commands: Commands, asset_server: Res<AssetServer>) { pub fn add_player(mut commands: Commands, asset_server: Res<AssetServer>) {
commands let player = commands
.spawn(( .spawn((
Player { Player {
move_cooldown: Timer::from_seconds(0.01, TimerMode::Repeating), move_cooldown: Timer::from_seconds(0.01, TimerMode::Repeating),
@ -53,15 +88,25 @@ mod player {
..default() ..default()
}, },
)) ))
.insert(Collider::cuboid(8., 8.)); .insert((Collider::cuboid(32., 32.),))
.insert(KinematicCharacterController { ..default() })
.id();
commands.spawn((
Camera2dBundle::default(),
Camera2DController::new_follow_with_speed(player, 300.),
));
} }
pub fn move_player( pub fn move_player(
kb_input: Res<ButtonInput<KeyCode>>, kb_input: Res<ButtonInput<KeyCode>>,
mut query: Query<(&mut Transform, &mut Player)>, mut query: Query<(
&mut Transform,
&mut Player,
&mut KinematicCharacterController,
)>,
time: Res<Time>, time: Res<Time>,
) { ) {
let (mut transform, mut player) = query.single_mut(); let (mut transform, mut player, mut controller) = query.single_mut();
if player.move_cooldown.tick(time.delta()).finished() { if player.move_cooldown.tick(time.delta()).finished() {
let mut moved = false; let mut moved = false;
let (mut mv_forward, mut mv_sideward, mut rot) = (0., 0., 0.); let (mut mv_forward, mut mv_sideward, mut rot) = (0., 0., 0.);
@ -92,11 +137,12 @@ mod player {
} }
if moved { if moved {
player.move_cooldown.reset(); // player.move_cooldown.reset();
transform.rotation *= Quat::from_rotation_z(rot); transform.rotation *= Quat::from_rotation_z(rot);
let fw = transform.up(); let fw = transform.up();
let right = transform.right(); let right = transform.right();
transform.translation += fw * mv_forward + right * mv_sideward; // transform.translation += fw * mv_forward + right * mv_sideward;
controller.translation = Some((fw * mv_forward + right * mv_sideward).xy());
} }
} }
} }