jucraft/src/game.rs

92 lines
2.1 KiB
Rust
Raw Normal View History

2023-04-19 09:05:48 +02:00
use std::vec;
2023-04-18 14:17:53 +02:00
use crate::{
2023-04-19 09:05:48 +02:00
tools::{WPos, Sides}, graphics::DrawType, systems::{WorldGenerator, BlockType},
2023-04-18 14:17:53 +02:00
};
2023-04-19 09:05:48 +02:00
use self::{world::World};
2023-04-18 14:17:53 +02:00
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 {
2023-04-19 09:05:48 +02:00
return Some((i, &self.types[i]));
2023-04-18 14:17:53 +02:00
}
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
}
2023-04-19 09:05:48 +02:00
2023-04-18 14:17:53 +02:00
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()
}
}