jucraft/src/game.rs

89 lines
2.1 KiB
Rust
Raw Normal View History

2023-04-18 14:17:53 +02:00
use crate::{
tools::{WPos, Sides}, graphics::DrawType,
};
use self::{block::BlockType, world::World};
pub mod block;
pub mod chunk;
pub mod world;
pub struct Game {
worlds: Vec<World>,
types: Vec<Box<dyn BlockType>>,
type_names: Vec<String>,
}
impl Game {
pub fn new() -> Self {
let mut g = Game {
worlds: vec![],
types: vec![],
type_names: vec![],
};
g.add_type(Box::new(Air {
id: "air".to_string(),
}));
g
}
pub fn get_type(&self, id: usize) -> Option<&Box<dyn BlockType>> {
self.types.get(id)
}
pub fn find_type(&self, id: String) -> Option<(usize, &Box<dyn BlockType>)> {
let index = self.type_names.iter().position(|r| r.eq(&id));
if let Some(i) = index {
return Some((i,&self.types[i]));
}
None
}
pub fn do_tick(&mut self, instant: bool) -> usize {
let mut num = 0;
for w in 0..self.worlds.len() {
num += self.worlds[w].do_tick(instant, &self.types);
}
num
}
pub fn do_tex_tick(&mut self) {
for w in 0..self.worlds.len() {
self.worlds[w].do_tex_tick(&self.types);
}
}
pub fn add_type(&mut self, mut block_type: Box<dyn BlockType>) -> usize {
self.type_names.push(block_type.get_id().to_string());
let index = self.types.len();
block_type.set_index(index);
self.types.push(block_type);
index
}
pub fn add_world(&mut self) -> usize {
self.worlds.push(World::new());
self.worlds.len() - 1
}
pub fn get_world(&mut self, id: usize) -> Option<&mut World> {
self.worlds.get_mut(id)
}
pub fn get_world_im(&self, id: usize) -> Option<&World> {
self.worlds.get(id)
}
}
#[derive(Debug)]
struct Air {
id: String,
}
impl BlockType for Air {
fn get_id(&self) -> String {
self.id.to_string()
}
fn is_sided(&self) -> bool {
false
}
fn texture(&self) -> &DrawType {
&DrawType::None()
}
}