rpg_base/map/src/surface.rs
2025-02-28 08:43:35 +01:00

113 lines
3 KiB
Rust

use anyhow::Result;
use engine::prelude::*;
use std::collections::HashMap;
pub struct Surface {
point_cloud: Vec<Vec<cgmath::Vector3<f32>>>,
texture_ids: Vec<Vec<u32>>,
}
impl Surface {
pub fn new(
width: u32,
height: u32,
point_cloud: Vec<Vec<cgmath::Vector3<f32>>>,
tiles: HashMap<u32, u32>,
) -> Result<Surface> {
let texture_ids = {
let mut texture_ids = Vec::with_capacity(width as usize);
for x in 0..width {
let mut tmp_y_ids = Vec::with_capacity(height as usize);
for y in 0..height {
let index = (x + 1) + (y * width);
// index - 1 since sql starts indices at 1
let texture_id = match tiles.get(&index) {
Some(tex_id) => tex_id - 1,
None => {
return Err(anyhow::Error::msg(format!(
"Index ({}) in tiles_map (Surface) not found",
index
)))
}
};
tmp_y_ids.push(texture_id);
}
texture_ids.push(tmp_y_ids);
}
texture_ids
};
Ok(Surface {
point_cloud,
texture_ids,
})
}
pub fn texture_id(&self, x: u32, y: u32) -> u32 {
self.texture_ids[x as usize][y as usize]
}
pub fn set_texture_id(&mut self, x: u32, y: u32, texture_id: u32) {
self.texture_ids[x as usize][y as usize] = texture_id;
}
pub fn point(&self, x: u32, y: u32) -> cgmath::Vector3<f32> {
self.point_cloud[x as usize][y as usize]
}
pub fn change_height(&mut self, x: u32, y: u32, height: f32) -> f32 {
self.point_cloud[x as usize][y as usize].z += height;
self.point_cloud[x as usize][y as usize].z
}
/*
pub fn check_plain_2_fixed(
&mut self,
x1_fixed: u32,
y1_fixed: u32,
x2_fixed: u32,
y2_fixed: u32,
) {
let ux1 = x1_fixed as usize;
let uy1 = y1_fixed as usize;
let ux2 = x2_fixed as usize;
let uy2 = y2_fixed as usize;
// fixed points
let bp1 = self.point_cloud[ux1][uy1].try_borrow()?;
let p1 = bp1.deref();
let bp2 = self.point_cloud[ux2][uy2].try_borrow()?;
let p2 = bp2.deref();
// points that need to be checked
let bp3 = self.point_cloud[ux1][uy2].try_borrow()?;
let p3 = bp3.deref();
let bp4 = self.point_cloud[ux2][uy1].try_borrow()?;
let p4 = bp4.deref();
let p12 = p1 - p2;
let p13 = p1 - p3;
let p14 = p1 - p4;
let cross = p12.cross(p13);
let dot = cgmath::dot(cross, p14);
if dot == 0.0 {
// everything is alright
return;
}
// TODO: elevate p3 and p4 to correct height
}
*/
}