Skip to content

Commit

Permalink
Basic movement of yups, collision detection
Browse files Browse the repository at this point in the history
  • Loading branch information
bas-ie committed Jan 2, 2025
1 parent 3988aee commit 0c6a2cb
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 12 deletions.
5 changes: 4 additions & 1 deletion src/game.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
pub mod movement;
pub mod yup;

use bevy::prelude::*;

use crate::screens::Screen;
Expand All @@ -21,7 +24,7 @@ pub enum Game {
pub fn plugin(app: &mut App) {
app.init_state::<Game>();
app.enable_state_scoped_entities::<Game>();

app.add_plugins((movement::plugin, yup::plugin));
app.add_systems(OnEnter(Game::Intro), init);
}

Expand Down
14 changes: 14 additions & 0 deletions src/game/movement.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use avian2d::prelude::*;
use bevy::prelude::*;

use crate::screens::ingame::playing::MovementSpeed;

pub fn plugin(app: &mut App) {
app.add_systems(FixedUpdate, movement);
}

fn movement(mut moving_objects: Query<(&mut LinearVelocity, &MovementSpeed)>) {
for (mut lv, speed) in &mut moving_objects {
lv.x = speed.0;
}
}
29 changes: 29 additions & 0 deletions src/game/yup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use avian2d::prelude::*;
use bevy::prelude::*;

use crate::screens::ingame::playing::{MovementSpeed, Obstacle};

#[derive(Component, Debug)]
pub struct Yup;

pub fn plugin(app: &mut App) {
app.add_systems(PostProcessCollisions, collision_handler);
}

fn collision_handler(
mut collisions: ResMut<Collisions>,
obstacles: Query<Entity, With<Obstacle>>,
mut yups: Query<(Entity, &mut MovementSpeed), With<Yup>>,
) {
// For now, we'll ignore all collisions between yups.
// TODO: sometimes blockers will need to be collided with!
collisions.retain(|c| yups.get(c.entity1).is_err() || yups.get(c.entity2).is_err());

for (yup, mut speed) in &mut yups {
for obstacle in &obstacles {
if collisions.contains(yup, obstacle) {
*speed = MovementSpeed(-speed.0);
}
}
}
}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
mod assets;
#[cfg(feature = "dev")]
mod dev_tools;
mod game;
mod screens;
pub mod game;
pub mod screens;
mod ui;

use avian2d::PhysicsPlugins;
Expand Down
2 changes: 1 addition & 1 deletion src/screens.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod ingame;
pub mod ingame;
mod loading;
mod splash;
mod title;
Expand Down
4 changes: 2 additions & 2 deletions src/screens/ingame.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod pause;
mod playing;
pub mod pause;
pub mod playing;

use crate::game::Game;
use bevy::prelude::*;
Expand Down
60 changes: 54 additions & 6 deletions src/screens/ingame/playing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ use avian2d::{
};
use bevy::prelude::*;

use crate::screens::Screen;
use crate::{game::yup::Yup, screens::Screen};

pub fn plugin(app: &mut App) {
app.add_systems(OnEnter(Screen::InGame), init);
app.insert_resource(Gravity(Vector::NEG_Y * 1000.0));
}

#[derive(Component, Debug)]
struct Player;
pub struct Platform;

#[derive(Component, Debug)]
pub struct Obstacle;

#[derive(Component)]
struct MovementSpeed(Scalar);
pub struct MovementSpeed(pub Scalar);

fn init(
mut commands: Commands,
Expand All @@ -30,6 +33,7 @@ fn init(
};
commands.spawn((
Name::new("Platform"),
Platform,
platform.clone(),
Collider::rectangle(50., 50.),
RigidBody::Static,
Expand All @@ -38,17 +42,61 @@ fn init(
));

commands.spawn((
Name::new("Player"),
Player,
Name::new("Platform"),
Platform,
platform.clone(),
Collider::rectangle(50., 50.),
RigidBody::Static,
StateScoped(Screen::InGame),
Transform::from_xyz(400., -150., 0.).with_scale(Vec3::new(10., 0.5, 10.)),
));

commands.spawn((
Name::new("Obstacle"),
Obstacle,
platform.clone(),
Collider::rectangle(50., 50.),
RigidBody::Static,
StateScoped(Screen::InGame),
Transform::from_xyz(500., -112., 0.),
));

commands.spawn((
Name::new("Obstacle"),
Obstacle,
platform.clone(),
Collider::rectangle(50., 50.),
RigidBody::Static,
StateScoped(Screen::InGame),
Transform::from_xyz(200., -112., 0.),
));

commands.spawn((
Name::new("Yup"),
Yup,
Collider::circle(20.),
LockedAxes::ROTATION_LOCKED,
Mesh2d(meshes.add(Circle::new(20.))),
MeshMaterial2d(materials.add(Color::srgb(0.2, 0.7, 0.9))),
MovementSpeed(250.),
MovementSpeed(100.),
Restitution::ZERO.with_combine_rule(CoefficientCombine::Min),
RigidBody::Dynamic,
StateScoped(Screen::InGame),
Transform::from_xyz(0., 200., 0.),
));

commands.spawn((
Name::new("Yup"),
Yup,
Collider::circle(20.),
LockedAxes::ROTATION_LOCKED,
Mesh2d(meshes.add(Circle::new(20.))),
MeshMaterial2d(materials.add(Color::srgb(0.1, 0.6, 0.9))),
MovementSpeed(100.),
Restitution::ZERO.with_combine_rule(CoefficientCombine::Min),
RigidBody::Dynamic,
StateScoped(Screen::InGame),
Transform::from_xyz(-100., 500., 0.),
));
time.unpause();
}

0 comments on commit 0c6a2cb

Please sign in to comment.