From aac5430f9e77f943736820990594bf9aa67c631b Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Fri, 8 Aug 2025 07:43:10 -0700 Subject: [PATCH] basic pipe displacement implemented --- src/base_game.rs | 8 +++++++- src/bin/flappy/main.rs | 43 ++++++++++++++++++++++++++++++------------ 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/base_game.rs b/src/base_game.rs index e2cf794..c302b24 100644 --- a/src/base_game.rs +++ b/src/base_game.rs @@ -1,3 +1,5 @@ +use bevy::platform::hash::RandomState; + use super::*; /// A good starting place for creating a game building on top of the base Bevy app @@ -37,7 +39,8 @@ impl Plugin for BaseGamePlugin { .add_plugins(DebuggingPlugin) .add_plugins(MeshPickingPlugin) .add_plugins(LoadingPlugin) - .add_plugins(BaseUiPlugin); + .add_plugins(BaseUiPlugin) + .init_resource::(); match self.game_type { GameType::Two => app.add_systems(Startup, create_camera_2d), @@ -76,3 +79,6 @@ pub fn create_camera_2d(mut commands: Commands) { info!("Spawning 2d camera"); commands.spawn((Camera2d, Camera { ..default() })); } + +#[derive(Default, Resource)] +pub struct Rand(pub RandomState); diff --git a/src/bin/flappy/main.rs b/src/bin/flappy/main.rs index e58aaaa..ad3087c 100644 --- a/src/bin/flappy/main.rs +++ b/src/bin/flappy/main.rs @@ -1,6 +1,7 @@ // Bevy basically forces "complex types" with Querys #![allow(clippy::type_complexity)] +use std::hash::BuildHasher; use games::physics2d::*; use games::*; @@ -14,8 +15,8 @@ fn main() { Physics2dPlugin, )) .insert_resource(Gravity(Vec2::NEG_Y * 9.8 * 100.0)) - .init_resource::() - .init_resource::() + .init_resource::() + .init_resource::() .init_resource::() .init_resource::() .init_resource::() @@ -250,7 +251,7 @@ fn update_batch_position( fn populate_ground( trigger: Trigger, grounds: Query<&Ground>, - ground_assets: Res, + ground_assets: Res, mut commands: Commands, ) { let Ground(idx) = grounds.get(trigger.target()).unwrap(); @@ -269,13 +270,31 @@ fn populate_ground( /// Otherwise this just spawns in the center of the batch. fn populate_pipe( trigger: Trigger, - pipes: Query<&Pipe>, - pipe_assets: Res, + pipes: Query<(&Pipe, &Batch)>, + pipe_assets: Res, mut commands: Commands, + rand: Res ) { - let pipe_t = match pipes.get(trigger.target()).unwrap() { - Pipe::Top => Transform::from_xyz(0.0, 200.0, -1.0).with_scale(Vec3::splat(100.0)), - Pipe::Bottom => Transform::from_xyz(0.0, -200.0, -1.0).with_scale(Vec3::splat(100.0)), + let pipe_t = { + let (pipe, Batch(id)) = pipes.get(trigger.target()).unwrap(); + + let offset = { + let val = rand.0.hash_one(id); + + let option = val % 3; + + match option { + 0 => 100.0, + 1 => 0.0, + 2 => -100.0, + _ => panic!("Can only pick 1 of 3 pipe offsets"), + } + }; + + match pipe { + Pipe::Top => Transform::from_xyz(0.0, 200.0 + offset, -1.0).with_scale(Vec3::splat(100.0)), + Pipe::Bottom => Transform::from_xyz(0.0, -100.0 + offset, -1.0).with_scale(Vec3::splat(100.0)), + } }; commands.entity(trigger.target()).insert(( pipe_t, @@ -301,13 +320,13 @@ fn populate_hitbox(trigger: Trigger, mut commands: Commands) { } #[derive(Resource, Default)] -struct GroundComponents { +struct GroundAssets { material: MeshMaterial2d, mesh: Mesh2d, } #[derive(Resource, Default)] -struct PipeComponents { +struct PipeAssets { material: MeshMaterial2d, mesh: Mesh2d, } @@ -316,8 +335,8 @@ struct PipeComponents { fn init_obstacle_assets( mut meshes: ResMut>, mut materials: ResMut>, - mut pipe_assets: ResMut, - mut ground_assets: ResMut, + mut pipe_assets: ResMut, + mut ground_assets: ResMut, server: Res, ) { pipe_assets.material = MeshMaterial2d(materials.add(ColorMaterial {