extract modules to files(tm)

This commit is contained in:
Schrottkatze 2024-05-03 22:15:35 +02:00
parent af47eb6510
commit 23f88b41a7
Signed by: schrottkatze
SSH key fingerprint: SHA256:hXb3t1vINBFCiDCmhRABHX5ocdbLiKyCdKI4HK2Rbbc
3 changed files with 126 additions and 129 deletions

View file

@ -3,6 +3,9 @@ use bevy_rapier2d::prelude::*;
use player::{add_player, move_player};
use some_bevy_tools::{camera_2d::Camera2DPlugin, controller_2d::SimpleTopDownControllerPlugin};
mod player;
mod scene;
fn main() {
App::new()
.insert_resource(RapierConfiguration {
@ -18,132 +21,3 @@ fn main() {
.add_systems(Update, move_player)
.run();
}
mod scene {
use bevy::{
prelude::*,
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
};
use bevy_rapier2d::prelude::*;
const METER: f32 = 64.;
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 {
use bevy::prelude::*;
use bevy_rapier2d::prelude::*;
use some_bevy_tools::camera_2d::Camera2DController;
use std::f32::consts::PI;
const TURN_SPEED: f32 = PI / 100.;
const FW_SPEED: f32 = 5.;
const BW_SPEED: f32 = 3.;
const SIDEWARD_SPEED: f32 = 3.5;
#[derive(Component)]
pub struct Player {
move_cooldown: Timer,
}
pub fn add_player(mut commands: Commands, asset_server: Res<AssetServer>) {
let player = commands
.spawn((
Player {
move_cooldown: Timer::from_seconds(0.01, TimerMode::Repeating),
},
SpriteBundle {
texture: asset_server.load("sprites/player.png"),
transform: Transform::from_xyz(0., 0., 0.),
..default()
},
))
.insert((Collider::cuboid(32., 32.),))
.insert(KinematicCharacterController { ..default() })
.id();
commands.spawn((
Camera2dBundle::default(),
Camera2DController::new_follow_with_speed(player, 300.),
));
}
pub fn move_player(
kb_input: Res<ButtonInput<KeyCode>>,
mut query: Query<(
&mut Transform,
&mut Player,
&mut KinematicCharacterController,
)>,
time: Res<Time>,
) {
let (mut transform, mut player, mut controller) = query.single_mut();
if player.move_cooldown.tick(time.delta()).finished() {
let mut moved = false;
let (mut mv_forward, mut mv_sideward, mut rot) = (0., 0., 0.);
if kb_input.pressed(KeyCode::KeyA) {
moved = true;
rot += TURN_SPEED;
}
if kb_input.pressed(KeyCode::KeyD) {
moved = true;
rot -= TURN_SPEED;
}
if kb_input.pressed(KeyCode::KeyW) {
moved = true;
mv_forward += FW_SPEED;
}
if kb_input.pressed(KeyCode::KeyE) {
moved = true;
mv_sideward += SIDEWARD_SPEED;
}
if kb_input.pressed(KeyCode::KeyQ) {
moved = true;
mv_sideward -= SIDEWARD_SPEED;
}
if kb_input.pressed(KeyCode::KeyS) {
moved = true;
mv_forward -= BW_SPEED;
}
if moved {
// player.move_cooldown.reset();
transform.rotation *= Quat::from_rotation_z(rot);
let fw = transform.up();
let right = transform.right();
// transform.translation += fw * mv_forward + right * mv_sideward;
controller.translation = Some((fw * mv_forward + right * mv_sideward).xy());
}
}
}
}

85
src/player.rs Normal file
View file

@ -0,0 +1,85 @@
use bevy::prelude::*;
use bevy_rapier2d::prelude::*;
use some_bevy_tools::camera_2d::Camera2DController;
use std::f32::consts::PI;
const TURN_SPEED: f32 = PI / 100.;
const FW_SPEED: f32 = 5.;
const BW_SPEED: f32 = 3.;
const SIDEWARD_SPEED: f32 = 3.5;
#[derive(Component)]
pub struct Player {
move_cooldown: Timer,
}
pub fn add_player(mut commands: Commands, asset_server: Res<AssetServer>) {
let player = commands
.spawn((
Player {
move_cooldown: Timer::from_seconds(0.01, TimerMode::Repeating),
},
SpriteBundle {
texture: asset_server.load("sprites/player.png"),
transform: Transform::from_xyz(0., 0., 0.),
..default()
},
))
.insert((Collider::cuboid(32., 32.),))
.insert(KinematicCharacterController { ..default() })
.id();
commands.spawn((
Camera2dBundle::default(),
Camera2DController::new_follow_with_speed(player, 300.),
));
}
pub fn move_player(
kb_input: Res<ButtonInput<KeyCode>>,
mut query: Query<(
&mut Transform,
&mut Player,
&mut KinematicCharacterController,
)>,
time: Res<Time>,
) {
let (mut transform, mut player, mut controller) = query.single_mut();
if player.move_cooldown.tick(time.delta()).finished() {
let mut moved = false;
let (mut mv_forward, mut mv_sideward, mut rot) = (0., 0., 0.);
if kb_input.pressed(KeyCode::KeyA) {
moved = true;
rot += TURN_SPEED;
}
if kb_input.pressed(KeyCode::KeyD) {
moved = true;
rot -= TURN_SPEED;
}
if kb_input.pressed(KeyCode::KeyW) {
moved = true;
mv_forward += FW_SPEED;
}
if kb_input.pressed(KeyCode::KeyE) {
moved = true;
mv_sideward += SIDEWARD_SPEED;
}
if kb_input.pressed(KeyCode::KeyQ) {
moved = true;
mv_sideward -= SIDEWARD_SPEED;
}
if kb_input.pressed(KeyCode::KeyS) {
moved = true;
mv_forward -= BW_SPEED;
}
if moved {
// player.move_cooldown.reset();
transform.rotation *= Quat::from_rotation_z(rot);
let fw = transform.up();
let right = transform.right();
// transform.translation += fw * mv_forward + right * mv_sideward;
controller.translation = Some((fw * mv_forward + right * mv_sideward).xy());
}
}
}

38
src/scene.rs Normal file
View file

@ -0,0 +1,38 @@
use bevy::{
prelude::*,
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
};
use bevy_rapier2d::prelude::*;
const METER: f32 = 64.;
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),
));
}
}