Skip to content

Commit

Permalink
Remove unused commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Wilgenbus committed Oct 9, 2023
1 parent 07dc7cb commit 9629fe9
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 134 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ rand = { version = "0.8.3" }
winit = { version = "0.28", default-features = false }
image = { version = "0.24", default-features = false }

bytemuck = "1.14.0"

[build-dependencies]
embed-resource = "2.3.0"

Expand Down
47 changes: 1 addition & 46 deletions src/actions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
use bevy::math::Vec3Swizzles;
use bevy::prelude::*;

use crate::actions::game_control::{get_movement, GameControl};
use crate::player::Player;
use crate::GameState;

mod game_control;

pub const FOLLOW_EPSILON: f32 = 5.;
Expand All @@ -14,47 +9,7 @@ pub struct ActionsPlugin;
// This plugin listens for keyboard input and converts the input into Actions
// Actions can then be used as a resource in other systems to act on the player input.
impl Plugin for ActionsPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<Actions>().add_systems(
Update,
set_movement_actions.run_if(in_state(GameState::Playing)),
);
}
}

#[derive(Default, Resource)]
pub struct Actions {
pub player_movement: Option<Vec2>,
}

pub fn set_movement_actions(
mut actions: ResMut<Actions>,
keyboard_input: Res<Input<KeyCode>>,
touch_input: Res<Touches>,
player: Query<&Transform, With<Player>>,
camera: Query<(&Camera, &GlobalTransform), With<Camera2d>>,
) {
let mut player_movement = Vec2::new(
get_movement(GameControl::Right, &keyboard_input)
- get_movement(GameControl::Left, &keyboard_input),
get_movement(GameControl::Up, &keyboard_input)
- get_movement(GameControl::Down, &keyboard_input),
);

if let Some(touch_position) = touch_input.first_pressed_position() {
let (camera, camera_transform) = camera.single();
if let Some(touch_position) = camera.viewport_to_world_2d(camera_transform, touch_position)
{
let diff = touch_position - player.single().translation.xy();
if diff.length() > FOLLOW_EPSILON {
player_movement = diff.normalize();
}
}
}
fn build(&self, _app: &mut App) {

if player_movement != Vec2::ZERO {
actions.player_movement = Some(player_movement.normalize());
} else {
actions.player_movement = None;
}
}
31 changes: 1 addition & 30 deletions src/audio.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::actions::{set_movement_actions, Actions};
use crate::loading::AudioAssets;
use crate::GameState;
use bevy::prelude::*;
Expand All @@ -11,13 +10,7 @@ pub struct InternalAudioPlugin;
impl Plugin for InternalAudioPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(AudioPlugin)
.add_systems(OnEnter(GameState::Playing), start_audio)
.add_systems(
Update,
control_flying_sound
.after(set_movement_actions)
.run_if(in_state(GameState::Playing)),
);
.add_systems(OnEnter(GameState::Playing), start_audio);
}
}

Expand All @@ -33,25 +26,3 @@ fn start_audio(mut commands: Commands, audio_assets: Res<AudioAssets>, audio: Re
.handle();
commands.insert_resource(FlyingAudio(handle));
}

fn control_flying_sound(
actions: Res<Actions>,
audio: Res<FlyingAudio>,
mut audio_instances: ResMut<Assets<AudioInstance>>,
) {
if let Some(instance) = audio_instances.get_mut(&audio.0) {
match instance.state() {
PlaybackState::Paused { .. } => {
if actions.player_movement.is_some() {
instance.resume(AudioTween::default());
}
}
PlaybackState::Playing { .. } => {
if actions.player_movement.is_none() {
instance.pause(AudioTween::default());
}
}
_ => {}
}
}
}
96 changes: 96 additions & 0 deletions src/engine/buffers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use bevy::{
prelude::{Res, Resource},
render::{
extract_resource::ExtractResource,
render_resource::{Buffer, BufferInitDescriptor, BufferUsages},
renderer::RenderDevice,
},
};

use super::{SIM_SIZE, SIM_VOXELS};

#[derive(Resource, Clone, ExtractResource, Debug)]
pub struct VoxelPipelineBuffers {
/// uniform, used for constants
pub dimentions: Buffer,
/// storage
pub in_out_double_buffers: Vec<Buffer>,
pub(in crate::engine) double_indices: DoubleBufferIndices,
}

impl VoxelPipelineBuffers {
pub fn new(device: Res<RenderDevice>) -> Self {
// Initialize constants (configuration, parameters... etc)
let dimentions = create_uniform_buffer(
&device,
&[SIM_SIZE.0, SIM_SIZE.1],
Some("Voxels image dimensions Uniform"),
);

// Initialize double buffers for the simulation data
let initial_life_data = vec![0u32; 2 * SIM_VOXELS];
let in_out_double_buffers = (0..2)
.map(|i| {
create_storage_buffer_with_data(
&device,
&initial_life_data,
Some(&format!("Voxel Engine Storage Buffer {i}")),
)
})
.collect::<Vec<_>>();

VoxelPipelineBuffers {
dimentions,
in_out_double_buffers,
double_indices: DoubleBufferIndices::default(),
}
}
}

/// To help us maintain the current and next buffer indices
#[derive(Clone, Debug)]
pub(in crate::engine) struct DoubleBufferIndices {
pub current: usize,
pub next: usize,
}

impl Default for DoubleBufferIndices {
fn default() -> Self {
Self {
current: 0,
next: 1,
}
}
}

impl DoubleBufferIndices {
// Sould only ever be called once per rendering tick
pub fn swap(&mut self) {
self.current = self.next;
self.next = (self.next + 1) % 2;
}
}

pub(in crate::engine) fn create_uniform_buffer<T: bytemuck::Pod + bytemuck::Zeroable>(
device: &RenderDevice,
data: &[T],
label: Option<&str>,
) -> Buffer {
device.create_buffer_with_data(&BufferInitDescriptor {
label,
contents: bytemuck::cast_slice(data),
usage: BufferUsages::UNIFORM | BufferUsages::COPY_DST,
})
}

pub(in crate::engine) fn create_storage_buffer_with_data<T: bytemuck::Pod + bytemuck::Zeroable>(
device: &RenderDevice,
data: &[T],
label: Option<&str>,
) -> Buffer {
device.create_buffer_with_data(&BufferInitDescriptor {
label,
contents: bytemuck::cast_slice(data),
usage: BufferUsages::STORAGE | BufferUsages::COPY_DST,
})
}
2 changes: 1 addition & 1 deletion src/engine/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bevy::{
render::{extract_resource::*, render_resource::*},
};

#[derive(Resource, Clone, Deref, ExtractResource)]
#[derive(Resource, Clone, Deref, ExtractResource, Debug)]
pub struct VoxelsRenderImage(pub Handle<Image>);

impl VoxelsRenderImage {
Expand Down
21 changes: 17 additions & 4 deletions src/engine/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
mod buffers;
pub mod image;
mod pipeline;

use bevy::{
diagnostic::*,
prelude::*,
render::{extract_resource::ExtractResourcePlugin, Render, RenderApp, RenderSet, render_graph::RenderGraph},
render::{extract_resource::ExtractResourcePlugin, Render, RenderApp, RenderSet, render_graph::RenderGraph, renderer::RenderDevice},
window::*,
};

use crate::{GameState, engine::buffers::VoxelPipelineBuffers};

use self::{image::VoxelsRenderImage, pipeline::{VoxelShadersPipeline, VoxelSandboxNode}};

const SIM_SIZE: (u32, u32) = (1280, 720);
const SIM_SIZE: (u32, u32) = (800, 600);
const SIM_VOXELS: usize = 800 * 600;
const WORKGROUP_SIZE: u32 = 8;

pub struct EnginePlugin;
Expand Down Expand Up @@ -48,7 +52,11 @@ fn window_fps(
}
}

fn setup(mut commands: Commands, images: ResMut<Assets<Image>>) {
fn setup(
mut commands: Commands,
images: ResMut<Assets<Image>>,
device: Res<RenderDevice>,
) {
let render_image_res = VoxelsRenderImage::new(SIM_SIZE.0, SIM_SIZE.1, images);

commands.spawn(SpriteBundle {
Expand All @@ -60,5 +68,10 @@ fn setup(mut commands: Commands, images: ResMut<Assets<Image>>) {
..default()
});

let buffers = VoxelPipelineBuffers::new(device);

debug!("[Resource inserted]{:?}", render_image_res);
commands.insert_resource(render_image_res);
}
debug!("[Resource inserted]{:?}", buffers);
commands.insert_resource(buffers);
}
5 changes: 1 addition & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ mod audio;
mod engine;
mod loading;
mod menu;
mod player;

use crate::actions::ActionsPlugin;
use crate::audio::InternalAudioPlugin;
use crate::loading::LoadingPlugin;
use crate::menu::MenuPlugin;
use crate::player::PlayerPlugin;

#[cfg(debug_assertions)]
use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin};
Expand Down Expand Up @@ -55,7 +53,6 @@ impl Plugin for GamePlugin {
MenuPlugin,
ActionsPlugin,
InternalAudioPlugin,
PlayerPlugin,
EnginePlugin,
ExtractResourcePlugin::<GameStateRes>::default(),
))
Expand All @@ -72,7 +69,7 @@ impl Plugin for GamePlugin {
{
app.add_plugins(LogPlugin {
level: bevy::log::Level::DEBUG,
filter: "debug,wgpu_core=warn,wgpu_hal=warn,sandboxed=debug".into(),
filter: "debug,wgpu_core=warn,wgpu_hal=warn,naga=warn,sandboxed=debug".into(),
});
app.add_plugins((FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin::default()));
}
Expand Down
47 changes: 0 additions & 47 deletions src/player.rs

This file was deleted.

0 comments on commit 9629fe9

Please sign in to comment.