minor refactor

This commit is contained in:
griffi-gh 2023-01-29 01:21:28 +01:00
parent 155e3b1e36
commit 17dbdc2757
5 changed files with 43 additions and 23 deletions

12
src/delta_time.rs Normal file
View file

@ -0,0 +1,12 @@
use std::time::Duration;
use shipyard::{Unique, AllStoragesView};
#[derive(Unique, Default)]
pub(crate) struct DeltaTime(pub Duration);
pub fn init_delta_time(
storages: AllStoragesView
) {
storages.add_unique(DeltaTime::default())
}

View file

@ -1,7 +1,7 @@
use glam::{Vec3, Mat4, Quat, EulerRot, Vec2};
use shipyard::{Component, View, ViewMut, IntoIter, UniqueView, Workload, IntoWorkload};
use std::f32::consts::PI;
use crate::{transform::Transform, input::Inputs, settings::GameSettings, DeltaTime};
use crate::{transform::Transform, input::Inputs, settings::GameSettings, delta_time::DeltaTime};
#[derive(Component)]
pub struct FlyController;

View file

@ -1,7 +1,7 @@
use shipyard::{
World, Workload, IntoWorkload,
UniqueView, UniqueViewMut,
NonSendSync, Unique
NonSendSync
};
use glium::{
glutin::{
@ -10,7 +10,7 @@ use glium::{
}
};
use glam::vec3;
use std::time::{Instant, Duration};
use std::time::Instant;
mod logging;
@ -25,6 +25,7 @@ pub(crate) mod events;
pub(crate) mod input;
pub(crate) mod fly_controller;
pub(crate) mod block_placement;
pub(crate) mod delta_time;
use rendering::{
Renderer,
@ -45,17 +46,17 @@ use events::{clear_events, process_glutin_events};
use input::{init_input, process_inputs};
use fly_controller::update_controllers;
use rendering::{
selection_box::render_selection_box,
selection_box::{render_selection_box, init_selection_box_buffers},
world::draw_world,
};
use block_placement::block_placement_system;
#[derive(Unique)]
pub(crate) struct DeltaTime(Duration);
use delta_time::{DeltaTime, init_delta_time};
fn startup() -> Workload {
(
init_delta_time,
load_prefabs,
init_selection_box_buffers,
init_input,
init_game_world,
spawn_player,
@ -96,7 +97,6 @@ fn main() {
//Add systems and uniques, Init and load things
world.add_unique_non_send_sync(Renderer::init(&event_loop));
world.add_unique(BackgroundColor(vec3(0.5, 0.5, 1.)));
world.add_unique(DeltaTime(Duration::default()));
world.add_unique(GameSettings::default());
//Register workloads

View file

View file

@ -1,4 +1,4 @@
use shipyard::{View, IntoIter, NonSendSync, UniqueViewMut, UniqueView};
use shipyard::{View, IntoIter, NonSendSync, UniqueViewMut, UniqueView, AllStoragesView, Unique};
use glium::{
Surface,
implement_vertex,
@ -43,6 +43,9 @@ const fn box_vertices() -> [SelBoxVertex; CUBE_VERTICES.len() / 3] {
}
const BOX_VERTICES: &[SelBoxVertex] = &box_vertices();
#[derive(Unique)]
pub struct SelectionBoxBuffers(VertexBuffer<SelBoxVertex>, IndexBuffer<u16>);
//wip
pub fn render_selection_box(
lookat: View<LookingAtBlock>,
@ -50,27 +53,16 @@ pub fn render_selection_box(
mut target: NonSendSync<UniqueViewMut<RenderTarget>>,
display: NonSendSync<UniqueView<Renderer>>,
program: NonSendSync<UniqueView<SelBoxShaderPrefab>>,
buffers: NonSendSync<UniqueView<SelectionBoxBuffers>>,
) {
let camera = camera.iter().next().unwrap();
let Some(lookat) = lookat.iter().next() else { return };
let Some(lookat) = lookat.0 else { return };
//this may be slow but the amount of vertices is very low
let vert = VertexBuffer::new(
&display.display,
BOX_VERTICES
).unwrap();
let index = IndexBuffer::new(
&display.display,
PrimitiveType::TrianglesList,
CUBE_INDICES
).unwrap();
//Darken block
target.0.draw(
&vert,
&index,
&buffers.0,
&buffers.1,
&program.0,
&uniform! {
u_color: [0., 0., 0., 0.5_f32],
@ -89,3 +81,19 @@ pub fn render_selection_box(
}
).unwrap();
}
pub fn init_selection_box_buffers(
storages: AllStoragesView,
display: NonSendSync<UniqueView<Renderer>>
) {
let vert = VertexBuffer::new(
&display.display,
BOX_VERTICES
).unwrap();
let index = IndexBuffer::new(
&display.display,
PrimitiveType::TrianglesList,
CUBE_INDICES
).unwrap();
storages.add_unique_non_send_sync(SelectionBoxBuffers(vert, index));
}