Hex Coordinates
This commit is contained in:
parent
bbe069880c
commit
fefbc6f629
121
src/coordinates.rs
Normal file
121
src/coordinates.rs
Normal file
|
@ -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};
|
24
src/main.rs
24
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));
|
||||
|
||||
|
|
|
@ -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<Chunk>,
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue