From fefbc6f629dacd8cb8cab7e6d244d38e12cbb183 Mon Sep 17 00:00:00 2001 From: Able Date: Sat, 26 Mar 2022 01:28:01 -0500 Subject: [PATCH] Hex Coordinates --- src/coordinates.rs | 121 +++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 24 ++++++--- src/voxel.rs | 8 --- 3 files changed, 139 insertions(+), 14 deletions(-) create mode 100644 src/coordinates.rs diff --git a/src/coordinates.rs b/src/coordinates.rs new file mode 100644 index 0000000..93c0ff4 --- /dev/null +++ b/src/coordinates.rs @@ -0,0 +1,121 @@ +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] +#![allow(unused_mut)] + +#[derive(Debug, Copy, Clone)] +pub struct Hex { + pub s: f64, + pub r: f64, + pub q: f64, + pub y: f64, +} + +pub enum HexDirection { + SPos, + SNeg, + RPos, + RNeg, + QPos, + QNeg, + YPos, + YNeg, +} + +impl Hex { + pub fn distance_to(&self, other: Hex) -> f64 { + let ret = *self - other; + ret.length() + } + pub fn length(&self) -> f64 { + self.q.abs() + self.r.abs() + self.s.abs() / 2.0 + } + + pub fn round(mut self) -> Hex { + let q = self.q.round(); + let r = self.r.round(); + let s = self.s.round(); + let y = self.y.round(); + Hex { q, r, s, y } + } + pub fn scale(self, k: f64) -> Hex { + Hex { + q: self.q * k, + r: self.r * k, + s: self.s * k, + y: self.y * k, + } + } + + pub fn rotate_left(&mut self) { + self.s = -self.s; + self.q = -self.q; + self.r = -self.r; + } + pub fn rotate_right(&mut self) { + self.s = -self.s; + self.q = -self.q; + self.r = -self.r; + } + pub fn lerp(&self, other: Hex, time: f64) -> Hex { + return Hex { + q: self.q * (1.0 - time) + other.q * time, + r: self.r * (1.0 - time) + other.r * time, + s: self.s * (1.0 - time) + other.s * time, + y: self.y * (1.0 - time) + other.y * time, + }; + } +} + +impl Add for Hex { + type Output = Self; + + fn add(self, other: Self) -> Self { + Self { + s: self.s + other.s, + r: self.r + other.r, + q: self.q + other.q, + y: self.y + other.y, + } + } +} + +impl Sub for Hex { + type Output = Self; + + fn sub(self, other: Self) -> Self { + Self { + s: self.s - other.s, + r: self.r - other.r, + q: self.q - other.q, + y: self.y - other.y, + } + } +} + +impl Div for Hex { + type Output = Self; + + fn div(self, other: Self) -> Self { + Self { + s: self.s / other.s, + r: self.r / other.r, + q: self.q / other.q, + y: self.y / other.y, + } + } +} + +impl Mul for Hex { + type Output = Self; + + fn mul(self, other: Self) -> Self { + Self { + s: self.s * other.s, + r: self.r * other.r, + q: self.q * other.q, + y: self.y * other.y, + } + } +} + +use std::ops::{Add, Div, Mul, Sub}; diff --git a/src/main.rs b/src/main.rs index fa2ab06..40ac648 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,8 @@ pub mod chunk; +pub mod coordinates; pub mod platform; pub mod voxel; -pub struct Position3D { - pub x: f64, - pub y: f64, - pub z: f64, -} - use bgfx::*; use bgfx_rs::bgfx; use glfw::{Action, Key, Window}; @@ -17,6 +12,23 @@ const WIDTH: usize = 1280; const HEIGHT: usize = 720; fn main() { + let hex_coord_1 = coordinates::Hex { + q: 0.0, + r: 0.0, + s: 0.0, + y: 0.0, + }; + + let hex_coord_2 = coordinates::Hex { + q: 1.0, + r: 1.0, + s: 1.1, + y: 0.0, + }; + hex_coord_2.round(); + + println!("{}", hex_coord_1.distance_to(hex_coord_2)); + let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap(); glfw.window_hint(glfw::WindowHint::ClientApi(glfw::ClientApiHint::NoApi)); diff --git a/src/voxel.rs b/src/voxel.rs index cce7fea..a3fe994 100644 --- a/src/voxel.rs +++ b/src/voxel.rs @@ -1,11 +1,3 @@ -use crate::{chunk::Chunk, Position3D}; - pub struct VoxelData { pub id: u32, } - -pub struct VoxelGrid { - pub name: String, - pub position: Position3D, - pub chunks: Vec, -}