Skip to content

Commit

Permalink
Merge pull request #28 from Froggy618157725/routes_again
Browse files Browse the repository at this point in the history
Routes again
  • Loading branch information
cohaereo authored Jul 8, 2024
2 parents cf08683 + b21cb22 commit c5bb009
Show file tree
Hide file tree
Showing 22 changed files with 1,231 additions and 301 deletions.
22 changes: 18 additions & 4 deletions crates/alkahest-renderer/src/camera/fps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,19 @@ impl CameraController for FpsCamera {
}

// Cancel tween if the user moves the camera
if tween.is_some() && direction.length() > 0.0 {
*tween = None;
if direction.length() > 0.0 {
if let Some(t) = tween {
t.abort();
}
}

if let Some(tween) = tween {
self.target_position = tween.update_pos().unwrap_or(self.target_position);
self.orientation = tween.update_angle().unwrap_or(self.orientation);
if tween.is_aborted() {
self.target_position += direction * speed;
} else {
self.target_position = tween.update_pos().unwrap_or(self.target_position);
self.orientation = tween.update_angle().unwrap_or(self.orientation);
}
} else {
self.target_position += direction * speed;
}
Expand Down Expand Up @@ -194,6 +200,14 @@ impl CameraController for FpsCamera {
Mat4::look_at_rh(self.position, self.position + self.forward, Vec3::Z)
}

fn view_angle(&self) -> Vec2 {
self.orientation
}

fn get_look_angle(&self, pos: Vec3) -> Vec2 {
super::get_look_angle(self.orientation, self.position, pos)
}

fn set_position(&mut self, position: Vec3) {
self.position = position;
self.target_position = position;
Expand Down
27 changes: 27 additions & 0 deletions crates/alkahest-renderer/src/camera/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,32 @@ pub trait CameraController {
fn up(&self) -> Vec3;

fn view_matrix(&self) -> Mat4;
fn view_angle(&self) -> Vec2;
fn get_look_angle(&self, pos: Vec3) -> Vec2;

fn set_position(&mut self, position: Vec3);
fn set_orientation(&mut self, orientation: Vec2);
// fn set_rotation(&mut self, rotation: Quat);
// fn look_at(&mut self, target: Vec3);
}

pub fn get_look_angle(start_angle: Vec2, pos1: Vec3, pos2: Vec3) -> Vec2 {
let dir = pos2 - pos1;
let inv_r = dir.length_recip();
if inv_r.is_infinite() {
start_angle
} else {
let theta = dir.x.atan2(dir.y).to_degrees();
let mut diff = (theta - start_angle.y).rem_euclid(360.0);
if diff > 180.0 {
diff -= 360.0;
}
Vec2::new(
(dir.z * inv_r).acos().to_degrees() - 90.0,
start_angle.y + diff,
)
}
}
pub struct Camera {
controller: Box<dyn CameraController>,
viewport: Viewport,
Expand Down Expand Up @@ -198,6 +217,14 @@ impl Camera {
self.controller.set_orientation(orientation);
}

pub fn view_angle(&self) -> Vec2 {
self.controller.view_angle()
}

pub fn get_look_angle(&self, pos: Vec3) -> Vec2 {
self.controller.get_look_angle(pos)
}

// pub fn set_rotation(&mut self, rotation: Quat) {
// self.controller.set_rotation(rotation);
// }
Expand Down
26 changes: 26 additions & 0 deletions crates/alkahest-renderer/src/camera/tween.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,22 @@ impl Tween {
new_angle
}

pub fn reset(&mut self) {
self.start_time = Instant::now();
}

pub fn abort(&mut self) {
self.angle_movement = None;
self.pos_movement = None;
}

pub fn is_finished(&self) -> bool {
self.start_time.elapsed().as_secs_f32() >= self.duration
}

pub fn is_aborted(&self) -> bool {
self.angle_movement.is_none() && self.pos_movement.is_none()
}
}

// https://easings.net/#easeOutExpo
Expand All @@ -64,3 +77,16 @@ pub fn ease_out_exponential(x: f32) -> f32 {
1.0 - 2f32.powf(-10. * x)
}
}

// https://easings.net/#easeInOutExpo
pub fn ease_in_out_exponential(x: f32) -> f32 {
if x == 0.0 {
0.0
} else if x == 1.0 {
1.0
} else if x < 0.5 {
2f32.powf(20. * x - 10.) / 2.
} else {
(2. - 2f32.powf(-20. * x + 10.)) / 2.
}
}
36 changes: 31 additions & 5 deletions crates/alkahest-renderer/src/ecs/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ impl Icon {
Icon::Colored(_, c) => *c,
}
}

pub fn char(&self) -> char {
match self {
Icon::Unicode(c) => *c,
Icon::Colored(c, _) => *c,
}
}
}

impl Display for Icon {
Expand All @@ -43,29 +50,48 @@ impl Display for Icon {
}
}

pub struct Label(pub String);
pub struct Label {
pub label: String,
pub default: bool,
}

impl Display for Label {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
write!(f, "{}", self.label)
}
}

impl Label {
pub fn new_default(str: impl AsRef<str>) -> Self {
let s = str.as_ref();
Self {
label: s.to_string(),
default: true,
}
}
}

impl From<&str> for Label {
fn from(s: &str) -> Self {
Self(s.to_string())
Self {
label: s.to_string(),
default: false,
}
}
}

impl From<String> for Label {
fn from(s: String) -> Self {
Self(s)
Self {
label: s,
default: false,
}
}
}

impl AsRef<str> for Label {
fn as_ref(&self) -> &str {
&self.0
&self.label
}
}

Expand Down
41 changes: 41 additions & 0 deletions crates/alkahest-renderer/src/ecs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use destiny_pkg::TagHash;

pub mod audio;
pub mod common;
pub mod hierarchy;
Expand All @@ -10,3 +12,42 @@ pub mod transform;
pub mod utility;

pub type Scene = hecs::World;
pub struct MapInfo {
pub activity_hash: Option<TagHash>,
pub map_hash: TagHash,
}

pub trait SceneInfo {
fn new_with_info(activity_hash: Option<TagHash>, map_hash: TagHash) -> Self;
fn add_map_info(&mut self, activity_hash: Option<TagHash>, map_hash: TagHash);
fn get_map_hash(&self) -> Option<TagHash>;
fn get_activity_hash(&self) -> Option<TagHash>;
}
impl SceneInfo for Scene {
fn new_with_info(activity_hash: Option<TagHash>, map_hash: TagHash) -> Self {
let mut scene = Scene::new();

scene.add_map_info(activity_hash, map_hash);
scene
}
fn add_map_info(&mut self, activity_hash: Option<TagHash>, map_hash: TagHash) {
if self.query::<&MapInfo>().iter().next().is_none() {
self.spawn((MapInfo {
activity_hash,
map_hash,
},));
}
}
fn get_map_hash(&self) -> Option<TagHash> {
self.query::<&MapInfo>()
.iter()
.next()
.map(|(_, i)| i.map_hash)
}
fn get_activity_hash(&self) -> Option<TagHash> {
self.query::<&MapInfo>()
.iter()
.next()
.and_then(|(_, i)| i.activity_hash)
}
}
12 changes: 7 additions & 5 deletions crates/alkahest-renderer/src/ecs/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::time::Instant;
use hecs::Entity;

use crate::{
camera::tween::ease_out_exponential,
camera::tween::ease_in_out_exponential,
util::color::{Color, ColorExt},
};

Expand Down Expand Up @@ -43,13 +43,15 @@ impl SelectedEntity {
}

pub fn select_fade_color(&self, base_color: Color, entity: Option<Entity>) -> Color {
let select_color = Color::from_rgb(1.0, 0.6, 0.2);
let select_color = Color::from_rgb(0.6, 0.36, 0.12);
let elapsed =
ease_out_exponential((self.time_selected.elapsed().as_secs_f32() / 1.4).min(1.0));
ease_in_out_exponential((self.time_selected.elapsed().as_secs_f32() / 0.4).min(1.0));

if self.selected() == entity && elapsed < 1.0 {
let c = select_color.to_vec4().lerp(base_color.to_vec4(), elapsed);
Color::from_rgb(c.x, c.y, c.z)
let c = base_color
.to_vec4()
.lerp(select_color.to_vec4(), 1.0 - elapsed);
Color::from_rgba_premultiplied(c.x, c.y, c.z, c.w)
} else {
base_color
}
Expand Down
Loading

0 comments on commit c5bb009

Please sign in to comment.