From 1eecbff4042ccae6a8c41c30a23ffcaff40c3a4c Mon Sep 17 00:00:00 2001 From: Mitrofanov Mikhail Date: Thu, 17 Jul 2025 13:22:44 +0300 Subject: [PATCH] Seed Landscape works --- src/basic.rs | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 src/basic.rs diff --git a/src/basic.rs b/src/basic.rs new file mode 100644 index 0000000..1535e1a --- /dev/null +++ b/src/basic.rs @@ -0,0 +1,110 @@ +use std::ops::{Index, IndexMut}; +use ggez::graphics::Color; + +pub struct Table { + default: T, + pub(crate) data: Vec, + pub(crate) side: usize, +} + +impl Clone for Table{ + fn clone(&self) -> Self { + Table{ + default: self.default.clone(), + data: self.data.clone(), + side: self.side + } + } +} + +impl Table { + pub fn new(fill: T, side: usize) -> Self { + Table { + default: fill.clone(), + data: vec![fill; side * side], + side, + } + } + + pub fn get(&self, index: usize) -> &T { + &self.data[index] + } + + fn get_dim(&mut self, x: usize, y: usize) -> &T { + self.get(x + y * self.side) + } + + 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.side, value); + } + + pub fn grow(&mut self) { + let new_side = self.side * 2; + let mut temp_table = Table::new(self.default.clone(), new_side); + for x in 0..self.side { + for y in 0..self.side { + let new_x = x * 2; + let new_y = y * 2; + let val = self.get_dim(x, y); + temp_table.set_dim(new_x, new_y, val.clone()); + temp_table.set_dim(new_x + 1, new_y, val.clone()); + temp_table.set_dim(new_x, new_y + 1, val.clone()); + temp_table.set_dim(new_x + 1, new_y + 1, val.clone()); + } + } + self.data = temp_table.data; + self.side = new_side; + } + + pub fn iter(&self) -> impl Iterator { + self.data.iter() + } + + pub fn convert_copy(&self, table: &mut Table, f: impl Fn(T) -> X) { + for i in 0..self.data.len() { + table.data[i] = f(self[i].clone()) + } + } +} + +impl Into> for Table { + fn into(self) -> Vec { + self.data + } +} + +impl Into> for Table { + fn into(self) -> Vec { + let mut data: Vec = vec![0; self.side * self.side * 4]; + self.data + .iter() + .zip((0..self.data.len()).collect::>()) + .for_each(|(color, idx)| { + let (r, g, b, a) = color.to_rgba(); + let idx = idx * 4; + data[idx] = r; + data[idx + 1] = g; + data[idx + 2] = b; + data[idx + 3] = a; + }); + data + } +} + +impl Index for Table{ + type Output = T; + + fn index(&self, index: usize) -> &Self::Output { + self.get(index) + } +} + +impl IndexMut for Table{ + fn index_mut(&mut self, index: usize) -> &mut Self::Output { + self.data.index_mut(index) + } +} \ No newline at end of file