init
This commit is contained in:
commit
68702e358f
9 changed files with 4654 additions and 0 deletions
103
src/main.rs
Normal file
103
src/main.rs
Normal file
|
@ -0,0 +1,103 @@
|
|||
use bevy::{
|
||||
prelude::*,
|
||||
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
|
||||
};
|
||||
use bevy_rapier2d::prelude::*;
|
||||
const METER: f32 = 48.;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_plugins(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(METER))
|
||||
.add_plugins(RapierDebugRenderPlugin::default())
|
||||
.add_systems(Startup, (scene::setup_scene, setup_cam, player::add_player))
|
||||
.add_systems(
|
||||
Update,
|
||||
(player::move_player, player::player_ground_collision),
|
||||
)
|
||||
.run()
|
||||
}
|
||||
|
||||
fn setup_cam(mut commands: Commands) {
|
||||
commands.spawn(Camera2dBundle {
|
||||
transform: Transform::from_xyz(0., 4. * METER, 0.),
|
||||
..default()
|
||||
});
|
||||
}
|
||||
|
||||
mod scene;
|
||||
mod player {
|
||||
use bevy::{
|
||||
prelude::*,
|
||||
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
|
||||
};
|
||||
use bevy_rapier2d::prelude::*;
|
||||
|
||||
use crate::METER;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Player {
|
||||
move_cooldown: Timer,
|
||||
grounded: bool,
|
||||
}
|
||||
|
||||
pub fn add_player(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<ColorMaterial>>,
|
||||
) {
|
||||
let shape = Mesh2dHandle(meshes.add(Rectangle::new(0.8 * METER, 1.8 * METER)));
|
||||
commands
|
||||
.spawn(Player {
|
||||
move_cooldown: Timer::from_seconds(0.01, TimerMode::Repeating),
|
||||
grounded: false,
|
||||
})
|
||||
.insert(MaterialMesh2dBundle {
|
||||
mesh: shape,
|
||||
material: materials.add(Color::rgb(0.2, 0.9, 0.2)),
|
||||
transform: Transform::from_xyz(0., 0., 0.),
|
||||
..default()
|
||||
})
|
||||
.insert((
|
||||
RigidBody::Dynamic,
|
||||
Collider::cuboid(0.4 * METER, 0.9 * METER),
|
||||
))
|
||||
.insert(KinematicCharacterController { ..default() });
|
||||
}
|
||||
|
||||
pub fn player_ground_collision(
|
||||
player: Query<Entity, With<Player>>,
|
||||
mut collision_events: EventReader<CollisionEvent>,
|
||||
) {
|
||||
let p = player.single();
|
||||
for collision_event in collision_events.read() {
|
||||
dbg!(p);
|
||||
dbg!(collision_event);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn move_player(
|
||||
kb_input: Res<ButtonInput<KeyCode>>,
|
||||
mut query: Query<(&mut Player, &mut KinematicCharacterController)>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
let (mut player, mut controller) = query.single_mut();
|
||||
if player.move_cooldown.tick(time.delta()).finished() {
|
||||
let mut moved = false;
|
||||
let mut mv = 0;
|
||||
|
||||
if kb_input.pressed(KeyCode::KeyA) {
|
||||
moved = true;
|
||||
mv += 1;
|
||||
}
|
||||
if kb_input.pressed(KeyCode::KeyD) {
|
||||
moved = true;
|
||||
mv -= 1;
|
||||
}
|
||||
|
||||
if moved {
|
||||
controller.translation = Some(Vec2::new(mv as f32 * 6., 0.));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
44
src/scene.rs
Normal file
44
src/scene.rs
Normal file
|
@ -0,0 +1,44 @@
|
|||
use bevy::{
|
||||
prelude::*,
|
||||
sprite::{MaterialMesh2dBundle, Mesh2dHandle},
|
||||
};
|
||||
use bevy_rapier2d::prelude::*;
|
||||
|
||||
use crate::METER;
|
||||
|
||||
#[derive(Component)]
|
||||
struct SceneObj;
|
||||
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., -2. * METER)),
|
||||
(
|
||||
Rectangle::new(1. * METER, 6. * METER),
|
||||
(7.5 * METER, 1.5 * METER),
|
||||
),
|
||||
(
|
||||
Rectangle::new(1. * METER, 6. * METER),
|
||||
(-7.5 * METER, 1.5 * METER),
|
||||
),
|
||||
];
|
||||
|
||||
for (shape_, pos) in scene_objs {
|
||||
let shape = Mesh2dHandle(meshes.add(shape_));
|
||||
commands
|
||||
.spawn(SceneObj)
|
||||
.insert(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),
|
||||
));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue