summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/audio.rs2
-rw-r--r--src/lib.rs1
-rw-r--r--src/state.rs22
-rw-r--r--src/ui.rs144
4 files changed, 162 insertions, 7 deletions
diff --git a/src/audio.rs b/src/audio.rs
index 2b190de..dd89a67 100644
--- a/src/audio.rs
+++ b/src/audio.rs
@@ -74,7 +74,9 @@ impl Assets {
}
pub fn play_sfx(&mut self, name: &str) {
+ log::info!("sfx: {}", name);
if let Some(a) = self.audio.get(name) {
+ log::info!("actually playing");
a.play(&self.ctx, None);
}
}
diff --git a/src/lib.rs b/src/lib.rs
index 4ad26a2..f0aa85d 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,6 +1,7 @@
use winit::platform::web::EventLoopExtWebSys;
pub mod utils;
+pub mod ui;
pub mod request;
pub mod context;
pub mod state;
diff --git a/src/state.rs b/src/state.rs
index bb479c6..90ad25c 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -55,7 +55,7 @@ pub struct State {
pub audio: Option<audio::Assets>,
pub projection: glam::Mat4,
- pub camera: glam::Mat4,
+ pub camera: (glam::Vec3, glam::Vec3, glam::Vec3),
pub lighting: (glam::Vec3, glam::Vec3),
pub log: Vec<(u64, String)>,
@@ -95,9 +95,9 @@ impl State {
0.1,
400.0,
),
- camera: glam::Mat4::IDENTITY,
+ camera: (glam::Vec3::new(0.0, 0.0, 0.0), glam::Vec3::new(0.0, 0.0, 1.0), glam::Vec3::new(0.0, 1.0, 0.0)),
lighting: (
- glam::Vec3::new(1.0, 0.0, 0.0),
+ glam::Vec3::new(1.0, 1.0, 1.0),
glam::Vec3::new(1.0, -1.0, 1.0),
),
@@ -106,6 +106,7 @@ impl State {
}
pub fn write_log(&mut self, e: &str) {
+ log::info!("log: {}", e.to_owned());
self.log.push((self.tick, e.to_owned()));
}
@@ -116,9 +117,11 @@ impl State {
pub fn move_camera(
&mut self,
_ctx: &context::Context,
- camera: &glam::Mat4,
+ pos: &glam::Vec3,
+ dir: &glam::Vec3,
+ up: &glam::Vec3,
) {
- self.camera = camera.clone();
+ self.camera = (pos.clone(), dir.clone(), up.clone());
}
pub fn set_lighting(
@@ -131,7 +134,11 @@ impl State {
}
pub fn view(&self) -> glam::Mat4 {
- self.camera.clone()
+ glam::Mat4::look_to_lh(
+ self.camera.0,
+ self.camera.1,
+ self.camera.2,
+ )
}
pub fn bind_3d(&mut self, ctx: &context::Context, shader: &shader::Shader) {
@@ -230,7 +237,8 @@ impl State {
pub fn run_update<G>(&mut self, ctx: &context::Context, game: &mut G) where G: Game {
let now = now(ctx);
- self.acc += now - self.last;
+ let diff = now - self.last;
+ self.acc += diff;
self.last = now;
// update, if enough time has accumulated since last update
diff --git a/src/ui.rs b/src/ui.rs
new file mode 100644
index 0000000..f855625
--- /dev/null
+++ b/src/ui.rs
@@ -0,0 +1,144 @@
+use crate::utils;
+
+fn compute_reverse(frames: u64, tick: u64, start: u64) -> u64 {
+ let leftover = frames - (tick - start)
+ .clamp(0, frames);
+ tick - leftover
+}
+
+pub enum ModeToggle {
+ Inactive { start: u64 },
+ Active { start: u64 },
+}
+
+pub struct Mode {
+ frames: u64,
+ toggle: ModeToggle,
+ locked: bool,
+}
+
+impl Mode {
+ pub fn new(frames: u64) -> Self {
+ Self {
+ frames,
+ toggle: ModeToggle::Inactive { start: 0 },
+ locked: false,
+ }
+ }
+
+ /// Is the current state active?
+ pub fn is_active(&self) -> bool {
+ match self.toggle {
+ ModeToggle::Inactive {..} => false,
+ ModeToggle::Active {..} => true,
+ }
+ }
+
+ /// Has the current transition finished?
+ pub fn is_ready(&self, tick: u64) -> bool {
+ let started = match self.toggle {
+ ModeToggle::Inactive { start } => start,
+ ModeToggle::Active { start } => start,
+ };
+ tick - started > self.frames
+ }
+
+ pub fn progress(&self, tick: u64) -> f32 {
+ match self.toggle {
+ ModeToggle::Inactive { start } => {
+ 1.0 - (((tick - start) as f32) / self.frames as f32)
+ .clamp(0.0, 1.0)
+ },
+ ModeToggle::Active { start } => {
+ (((tick - start) as f32) / self.frames as f32)
+ .clamp(0.0, 1.0)
+ }
+ }
+ }
+
+ pub fn reverse(&mut self, tick: u64) -> bool {
+ if !self.locked {
+ self.locked = true;
+ match self.toggle {
+ ModeToggle::Inactive { start } => {
+ self.toggle = ModeToggle::Active {
+ start: compute_reverse(self.frames, tick, start)
+ };
+ },
+ ModeToggle::Active { start } => {
+ self.toggle = ModeToggle::Inactive {
+ start: compute_reverse(self.frames, tick, start)
+ };
+ },
+ }
+ true
+ } else { false }
+ }
+
+ pub fn lock(&mut self) {
+ self.locked = true;
+ }
+
+ pub fn unlock(&mut self) {
+ self.locked = false;
+ }
+}
+
+pub struct Cursor {
+ pub index: i32,
+ pub prev_index: i32,
+ pub change_started: u64,
+ pub bound: i32,
+ pub frames: u64,
+ pub locked: bool,
+}
+
+impl Cursor {
+ pub fn new(bound: i32, frames: u64) -> Self {
+ Self {
+ index: 0,
+ prev_index: 0,
+ change_started: 0,
+ bound,
+ frames,
+ locked: false,
+ }
+ }
+
+ pub fn animation_index(&self, tick: u64) -> f32 {
+ let progress = ((tick - self.change_started) as f32)
+ / (self.frames as f32 / 2.0);
+ utils::lerp(
+ self.prev_index as f32,
+ self.index as f32,
+ progress
+ )
+ }
+
+ pub fn is_ready(&self, tick: u64) -> bool {
+ tick - self.change_started > self.frames
+ }
+
+ pub fn set(&mut self, val: i32, tick: u64) -> bool {
+ if self.is_ready(tick) || !self.locked {
+ self.change_started = tick;
+ self.prev_index = self.index;
+ self.index = val;
+ self.index %= self.bound;
+ self.locked = true;
+ true
+ } else { false }
+ }
+
+ pub fn increment(&mut self, tick: u64) -> bool {
+ self.set(self.index + 1, tick)
+ }
+
+ pub fn decrement(&mut self, tick: u64) -> bool {
+ self.set(self.index + self.bound - 1, tick)
+ }
+
+ pub fn unlock(&mut self) {
+ self.locked = false;
+ }
+}