Skip to content
Snippets Groups Projects
Verified Commit 98132d8f authored by Louis's avatar Louis :fire:
Browse files

Add basic collision checks, recycle background sprites

parent d7df3eba
No related branches found
No related tags found
No related merge requests found
use crate::deref_as;
use bevy::prelude::{Color, Component, Rect, Vec2};
#[derive(Clone, Copy, Debug, Component)]
pub struct BoxSize(Vec2);
deref_as!(BoxSize => Vec2);
impl From<Vec2> for BoxSize {
fn from(value: Vec2) -> Self {
BoxSize(value)
}
}
pub fn check_box_collisions(first: Rect, second: Rect) -> bool {
first.min.x < second.max.x
&& first.max.x > second.min.x
&& first.min.y < second.max.y
&& first.max.y > second.min.y
}
#[derive(Clone, Copy, Debug, Component)]
pub enum CollisionGroup {
Player,
Pickup,
FriendlyProjectile,
HostileProjectile,
Enemy,
}
impl CollisionGroup {
pub fn color(&self) -> Color {
use CollisionGroup::*;
match self {
Player => Color::rgb_u8(0, 116, 217),
Pickup => Color::rgb_u8(177, 13, 201),
FriendlyProjectile => Color::rgb_u8(46, 204, 64),
HostileProjectile => Color::rgb_u8(255, 65, 54),
Enemy => Color::rgb_u8(133, 20, 75),
}
}
}
mod collision;
mod motion; mod motion;
mod player; mod player;
mod spawning; mod spawning;
...@@ -19,6 +20,7 @@ mod _plugin { ...@@ -19,6 +20,7 @@ mod _plugin {
} }
pub use _plugin::EntityPlugin; pub use _plugin::EntityPlugin;
pub use collision::{check_box_collisions, BoxSize, CollisionGroup};
pub use motion::Velocity; pub use motion::Velocity;
pub use player::Player; pub use player::Player;
pub use spawning::EntitySpawner; pub use spawning::EntitySpawner;
use crate::entities::motion::Velocity; use crate::entities::motion::Velocity;
use crate::entities::Player; use crate::entities::{BoxSize, Player};
use crate::system::Background; use crate::system::{Background, BACKGROUND_HEIGHT, BACKGROUND_WIDTH};
use bevy::ecs::system::SystemParam; use bevy::ecs::system::SystemParam;
use bevy::prelude::{AssetServer, Commands, Entity, Res, SpriteBundle, Transform, Vec2}; use bevy::prelude::{AssetServer, Commands, Entity, Res, SpriteBundle, Transform, Vec2};
...@@ -40,6 +40,7 @@ impl<'w, 's> EntitySpawner<'w, 's> { ...@@ -40,6 +40,7 @@ impl<'w, 's> EntitySpawner<'w, 's> {
}, },
Velocity::from(Vec2::new(-75.0, 0.0)), Velocity::from(Vec2::new(-75.0, 0.0)),
Background, Background,
BoxSize::from(Vec2::new(BACKGROUND_WIDTH, BACKGROUND_HEIGHT)),
)) ))
.id() .id()
} }
......
...@@ -4,19 +4,25 @@ mod window; ...@@ -4,19 +4,25 @@ mod window;
mod _plugin { mod _plugin {
use bevy::app::App; use bevy::app::App;
use bevy::prelude::Plugin; use bevy::prelude::{IntoSystemConfig, Plugin};
use bevy::time::common_conditions::on_fixed_timer;
use std::time::Duration;
pub struct SystemPlugin; pub struct SystemPlugin;
impl Plugin for SystemPlugin { impl Plugin for SystemPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_startup_system(super::camera::spawn_2d_camera) app.add_startup_system(super::camera::spawn_2d_camera)
.add_startup_system(super::parallax::spawn_backgrounds); .add_startup_system(super::parallax::spawn_backgrounds)
.add_system(
super::parallax::cycle_background_positions
.run_if(on_fixed_timer(Duration::from_millis(100))),
);
} }
} }
} }
pub use camera::default_projection; pub use camera::default_projection;
pub use parallax::Background; pub use parallax::{Background, BACKGROUND_HEIGHT, BACKGROUND_WIDTH};
pub use window::{window_bounds, VIEWPORT_HEIGHT, VIEWPORT_WIDTH}; pub use window::{window_bounds, VIEWPORT_HEIGHT, VIEWPORT_WIDTH};
pub use _plugin::SystemPlugin; pub use _plugin::SystemPlugin;
use crate::entities::EntitySpawner; use crate::entities::{BoxSize, EntitySpawner};
use crate::system::window_bounds; use crate::system::window_bounds;
use bevy::prelude::{Component, Vec2}; use bevy::prelude::{Component, Query, Transform, Vec2, With};
#[derive(Component)] #[derive(Component)]
pub struct Background; pub struct Background;
// TODO: No Magic Numbers! // TODO: No Magic Numbers!
const BACKGROUND_WIDTH: f32 = 2110.0; pub const BACKGROUND_WIDTH: f32 = 2110.0;
pub const BACKGROUND_HEIGHT: f32 = 300.0;
pub fn spawn_backgrounds(mut spawner: EntitySpawner) { pub fn spawn_backgrounds(mut spawner: EntitySpawner) {
let bounds = window_bounds(); let bounds = window_bounds();
...@@ -17,3 +18,13 @@ pub fn spawn_backgrounds(mut spawner: EntitySpawner) { ...@@ -17,3 +18,13 @@ pub fn spawn_backgrounds(mut spawner: EntitySpawner) {
bounds.height() / 2.0, bounds.height() / 2.0,
)); ));
} }
pub fn cycle_background_positions(
mut background_query: Query<(&mut Transform, &BoxSize), With<Background>>,
) {
for (mut transform, size) in &mut background_query {
if transform.translation.x <= -(size.x / 2.0) {
transform.translation.x = size.x + (size.x / 2.0);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment