Change inner hex table code
This commit is contained in:
+48
-19
@@ -1,12 +1,23 @@
|
|||||||
pub struct HexTable {
|
pub struct HexTable<T: Clone> {
|
||||||
len: usize,
|
default: T,
|
||||||
scale: f32,
|
pub(crate) data: Vec<T>,
|
||||||
|
pub(crate) len: usize,
|
||||||
|
pub(crate) scale: f32,
|
||||||
|
pub(crate) dimensions: (usize, usize),
|
||||||
}
|
}
|
||||||
|
|
||||||
const VERTICAL_OFFSET: f32 = 0.866025;
|
const VERTICAL_OFFSET: f32 = 0.866025;
|
||||||
impl HexTable {
|
impl<T: Clone> HexTable<T> {
|
||||||
pub fn new(len: usize, scale: f32) -> Self {
|
pub fn new(len: usize, default: T, scale: f32) -> Self {
|
||||||
Self { len, scale }
|
let rows = len - 1;
|
||||||
|
let columns = (len as f32 / VERTICAL_OFFSET) as usize;
|
||||||
|
Self {
|
||||||
|
default: default.clone(),
|
||||||
|
len,
|
||||||
|
scale,
|
||||||
|
data: vec![default; rows * columns],
|
||||||
|
dimensions: (rows, columns),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn calculate(&self) -> Vec<(f32, f32)> {
|
pub fn calculate(&self) -> Vec<(f32, f32)> {
|
||||||
@@ -30,25 +41,43 @@ impl HexTable {
|
|||||||
vec
|
vec
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn around(&self, x: f32, y: f32) -> Vec<(f32, f32)> {
|
pub fn around(&self, x: usize, y: usize) -> Vec<&T> {
|
||||||
let around = vec![
|
let x = x as isize;
|
||||||
(x + self.scale, y),
|
let y = y as isize;
|
||||||
(x - self.scale, y),
|
let around : Vec<(isize, isize)> = vec![
|
||||||
(x + 0.5 * self.scale, y + VERTICAL_OFFSET * self.scale),
|
(x, y + 1),
|
||||||
(x - 0.5 * self.scale, y + VERTICAL_OFFSET * self.scale),
|
(x + 1, y + 1),
|
||||||
(x + 0.5 * self.scale, y - VERTICAL_OFFSET * self.scale),
|
(x, y - 1),
|
||||||
(x - 0.5 * self.scale, y - VERTICAL_OFFSET * self.scale),
|
(x + 1, y - 1),
|
||||||
|
(x - 1, y),
|
||||||
|
(x + 1, y),
|
||||||
];
|
];
|
||||||
|
|
||||||
around
|
around
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|(x, y)| {
|
.filter(|(x, y)| {
|
||||||
*x >= 0.0
|
*x >= 0
|
||||||
&& *x < self.scale * self.len as f32
|
&& *x <= self.dimensions.0 as isize
|
||||||
&& *y >= 0.0
|
&& *y >= 0
|
||||||
&& *y < self.scale * self.len as f32
|
&& *y < self.dimensions.1 as isize
|
||||||
})
|
})
|
||||||
.map(|(x, y)| (*x, *y))
|
.map(|(x, y)| self.get_dim(*x as usize, *y as usize))
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get(&self, index: usize) -> &T {
|
||||||
|
&self.data[index]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_dim(&self, x: usize, y: usize) -> &T {
|
||||||
|
self.get(x + y * self.dimensions.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set(&mut self, index: usize, value: T) {
|
||||||
|
self.data[index] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_dim(&mut self, x: usize, y: usize, value: T) {
|
||||||
|
self.set(x + y * self.dimensions.0, value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -1,10 +1,10 @@
|
|||||||
mod basic;
|
mod table;
|
||||||
mod seed_automation;
|
mod seed_automation;
|
||||||
mod plate_automation;
|
mod plate_automation;
|
||||||
mod subplate_automation;
|
mod subplate_automation;
|
||||||
mod hex_table;
|
mod hex_table;
|
||||||
|
|
||||||
use crate::basic::{IntoImage, Table};
|
use crate::table::{IntoImage, Table};
|
||||||
use bevy::asset::io::embedded::GetAssetServer;
|
use bevy::asset::io::embedded::GetAssetServer;
|
||||||
use bevy::image::TextureFormatPixelInfo;
|
use bevy::image::TextureFormatPixelInfo;
|
||||||
use bevy::input::keyboard::keyboard_input_system;
|
use bevy::input::keyboard::keyboard_input_system;
|
||||||
@@ -84,7 +84,7 @@ struct SeededRng(ChaCha8Rng);
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::basic::Table;
|
use crate::table::Table;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn around_1(){
|
fn around_1(){
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use rayon::iter::IndexedParallelIterator;
|
use rayon::iter::IndexedParallelIterator;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use crate::SeededRng;
|
use crate::SeededRng;
|
||||||
use crate::basic::{IntoImage, Table};
|
use crate::table::{IntoImage, Table};
|
||||||
use bevy::asset::{Assets, RenderAssetUsages};
|
use bevy::asset::{Assets, RenderAssetUsages};
|
||||||
use bevy::image::{BevyDefault, Image};
|
use bevy::image::{BevyDefault, Image};
|
||||||
use bevy::input::ButtonInput;
|
use bevy::input::ButtonInput;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use crate::SeededRng;
|
use crate::SeededRng;
|
||||||
use crate::basic::{IntoImage, Table};
|
use crate::table::{IntoImage, Table};
|
||||||
use bevy::asset::{Assets, RenderAssetUsages};
|
use bevy::asset::{Assets, RenderAssetUsages};
|
||||||
use bevy::image::{BevyDefault, Image};
|
use bevy::image::{BevyDefault, Image};
|
||||||
use bevy::input::ButtonInput;
|
use bevy::input::ButtonInput;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::basic::{IntoImage, Table};
|
use crate::table::{IntoImage, Table};
|
||||||
use crate::hex_table::HexTable;
|
use crate::hex_table::HexTable;
|
||||||
use crate::SeededRng;
|
use crate::SeededRng;
|
||||||
use bevy::asset::{Assets, RenderAssetUsages};
|
use bevy::asset::{Assets, RenderAssetUsages};
|
||||||
@@ -196,7 +196,7 @@ pub fn setup_hex_matrix(
|
|||||||
|
|
||||||
let resolution = automation.world.side / 4;
|
let resolution = automation.world.side / 4;
|
||||||
|
|
||||||
let generator = HexTable::new(resolution, 4.0);
|
let generator = HexTable::new(resolution, true, 4.0);
|
||||||
let mut table = Table::new(false, automation.world.side);
|
let mut table = Table::new(false, automation.world.side);
|
||||||
generator.calculate().iter().for_each(|(x, y)| {
|
generator.calculate().iter().for_each(|(x, y)| {
|
||||||
let x = *x as usize;
|
let x = *x as usize;
|
||||||
@@ -281,5 +281,5 @@ pub struct HexMatrixView;
|
|||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct HexMatrixBuild{
|
pub struct HexMatrixBuild{
|
||||||
points: Table<bool>,
|
points: Table<bool>,
|
||||||
hex_matrix: HexTable
|
hex_matrix: HexTable<bool>
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user