From d61c1c6a7781b514837a548a08e80a62d7f9eea2 Mon Sep 17 00:00:00 2001 From: Serge Bazanski Date: Thu, 8 Apr 2021 17:56:11 +0000 Subject: [PATCH] engine, ecs: ticking --- engine/BUILD | 1 + engine/init.lua | 6 ++ engine/scene.lua | 22 ++-- engine/src/globals.rs | 26 +++++ engine/src/main.rs | 26 +---- engine/src/render/renderable.rs | 2 +- engine/src/scripting.rs | 173 +++++++++++++++++++++++--------- lib/ecs/src/componentmap.rs | 43 ++++++++ lib/ecs/src/world.rs | 28 ++++++ 9 files changed, 245 insertions(+), 82 deletions(-) create mode 100644 engine/src/globals.rs diff --git a/engine/BUILD b/engine/BUILD index 61f4621..3aa8f43 100644 --- a/engine/BUILD +++ b/engine/BUILD @@ -15,6 +15,7 @@ rust_binary( srcs = [ "src/main.rs", + "src/globals.rs", "src/input.rs", "src/scripting.rs", diff --git a/engine/init.lua b/engine/init.lua index 987d9f1..cb4ebd5 100644 --- a/engine/init.lua +++ b/engine/init.lua @@ -1,4 +1,10 @@ sent = {} +sent.newindex = function(t, k, v) + return __sent_components_newindex(t, k, v) +end +sent.index = function(t, k) + return __sent_components_index(t, k) +end sent.register = function (cfg) if cfg.name == nil then error("sent.register: needs name") diff --git a/engine/scene.lua b/engine/scene.lua index 9751b23..3ac3e33 100644 --- a/engine/scene.lua +++ b/engine/scene.lua @@ -5,10 +5,19 @@ local cube_material = rm.get_material("test-128px") local Cube = {} function Cube:init(x, y, z) + self.name = string.format("%d %d %d", x, y, z) self.components.Transform = components.Transform.new(x, y, z) end function Cube:tick() + local xyzw = self.components.Transform:xyzw(); + local x = xyzw[1]; + local y = xyzw[2]; + local dist = math.sqrt(x*x + y*y) + local z = math.sin(time*2+dist)*math.max(10-dist, 0)/10 + local new = components.Transform.new(x, y, z); + --print(self.name, x, y, z, new:xyzw()[1], new:xyzw()[2], new:xyzw()[3]) + self.components.Transform = new end sent.register({ @@ -20,14 +29,9 @@ sent.register({ }, }) -for x=-2,2 do - for y=-2,2 do - for z=-2,2 do - --if z > -2 and z < 2 and x > -2 and x < 2 and y > 0 then - if x <= 0 and y <= 0 and z <= 0 then - else - Cube.new(x, y, z) - end - end +local z = 0 +for x=-8,8 do + for y=-8,8 do + Cube.new(x, y, z) end end diff --git a/engine/src/globals.rs b/engine/src/globals.rs new file mode 100644 index 0000000..042d7d2 --- /dev/null +++ b/engine/src/globals.rs @@ -0,0 +1,26 @@ +use std::time; +use std::time::Instant; + +pub struct Time { + start: time::Instant, + now: time::Instant, +} +impl ecs::Global for Time {} + +impl Time { + pub fn instant(&self) -> f32 { + let instant_ns = self.now.duration_since(self.start).as_nanos() as u64; + let instant = ((instant_ns/1000) as f32) / 1_000_000.0; + instant + } + pub fn update(&mut self) { + self.now = time::Instant::now(); + } + pub fn new() -> Self { + let now = time::Instant::now(); + Self { + start: now, + now: now, + } + } +} diff --git a/engine/src/main.rs b/engine/src/main.rs index c6840e1..68b616f 100644 --- a/engine/src/main.rs +++ b/engine/src/main.rs @@ -17,10 +17,10 @@ use log; use env_logger; use std::sync::Arc; -use std::time; use cgmath as cgm; +pub mod globals; pub mod input; mod render; mod util; @@ -34,20 +34,6 @@ use render::material::{Texture, PBRMaterialBuilder}; use render::{Light, Material, Mesh, Transform, Renderable}; use physics::color; -struct Time { - start: time::Instant, - now: time::Instant, -} -impl ecs::Global for Time {} - -impl Time { - pub fn instant(&self) -> f32 { - let instant_ns = self.now.duration_since(self.start).as_nanos() as u64; - let instant = ((instant_ns/1000) as f32) / 1_000_000.0; - instant - } -} - struct Main { light1: ecs::EntityID, @@ -156,7 +142,7 @@ impl Main { #[derive(Access)] struct MainData<'a> { scene_info: ecs::ReadWriteGlobal<'a, render::SceneInfo>, - time: ecs::ReadGlobal<'a, Time>, + time: ecs::ReadGlobal<'a, globals::Time>, input: ecs::ReadGlobal<'a, input::Input>, transforms: ecs::ReadWriteComponent<'a, Transform>, } @@ -220,15 +206,11 @@ fn main() { p.add_system(context); p.add_system(renderer); - let start = time::Instant::now(); - world.set_global(Time { - start, - now: start, - }); + world.set_global(globals::Time::new()); world.set_global(input::Input::new()); loop { world.queue_drain(); - world.global_mut::