basic pipe displacement implemented

main
Elijah Voigt 3 months ago
parent 8566a46347
commit aac5430f9e

@ -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::<Rand>();
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);

@ -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::<PipeComponents>()
.init_resource::<GroundComponents>()
.init_resource::<PipeAssets>()
.init_resource::<GroundAssets>()
.init_resource::<Score>()
.init_resource::<RewindFrames>()
.init_resource::<Flaps>()
@ -250,7 +251,7 @@ fn update_batch_position(
fn populate_ground(
trigger: Trigger<OnAdd, Ground>,
grounds: Query<&Ground>,
ground_assets: Res<GroundComponents>,
ground_assets: Res<GroundAssets>,
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<OnAdd, Pipe>,
pipes: Query<&Pipe>,
pipe_assets: Res<PipeComponents>,
pipes: Query<(&Pipe, &Batch)>,
pipe_assets: Res<PipeAssets>,
mut commands: Commands,
rand: Res<Rand>
) {
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<OnAdd, Hitbox>, mut commands: Commands) {
}
#[derive(Resource, Default)]
struct GroundComponents {
struct GroundAssets {
material: MeshMaterial2d<ColorMaterial>,
mesh: Mesh2d,
}
#[derive(Resource, Default)]
struct PipeComponents {
struct PipeAssets {
material: MeshMaterial2d<ColorMaterial>,
mesh: Mesh2d,
}
@ -316,8 +335,8 @@ struct PipeComponents {
fn init_obstacle_assets(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
mut pipe_assets: ResMut<PipeComponents>,
mut ground_assets: ResMut<GroundComponents>,
mut pipe_assets: ResMut<PipeAssets>,
mut ground_assets: ResMut<GroundAssets>,
server: Res<AssetServer>,
) {
pipe_assets.material = MeshMaterial2d(materials.add(ColorMaterial {

Loading…
Cancel
Save