mirror of
https://github.com/Noratrieb/game-wip-dontplay.git
synced 2026-01-16 20:35:02 +01:00
Add torches
This commit is contained in:
parent
704819a988
commit
77099ab8a6
13 changed files with 427 additions and 133 deletions
106
src/tiles.rs
Normal file
106
src/tiles.rs
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
pub mod tiledb_edit_ui;
|
||||
|
||||
use std::ops::Index;
|
||||
|
||||
use egui_inspect::derive::Inspect;
|
||||
use log::warn;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sfml::graphics::IntRect;
|
||||
|
||||
use crate::{math::TILE_SIZE, world::TileId};
|
||||
|
||||
#[derive(Serialize, Deserialize, Default, Debug, Inspect)]
|
||||
pub struct TileDef {
|
||||
pub solid: bool,
|
||||
pub emits_light: bool,
|
||||
pub atlas_offset: AtlasOffset,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Inspect)]
|
||||
pub struct TileDb {
|
||||
db: Vec<TileDef>,
|
||||
}
|
||||
|
||||
impl Default for TileDb {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
// Add empty/air tile
|
||||
db: vec![EMPTY],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const EMPTY: TileDef = TileDef {
|
||||
solid: false,
|
||||
emits_light: false,
|
||||
// Rendering empty tile is actually special cased, and no rendering is done.
|
||||
// But just in case, put the offset to UNKNOWN
|
||||
atlas_offset: UNKNOWN_ATLAS_OFF,
|
||||
};
|
||||
|
||||
impl Index<TileId> for TileDb {
|
||||
type Output = TileDef;
|
||||
|
||||
fn index(&self, index: TileId) -> &Self::Output {
|
||||
self.db.get(index as usize).unwrap_or(&UNKNOWN_TILE)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Inspect, Serialize, Deserialize)]
|
||||
pub struct AtlasOffset {
|
||||
pub x: u16,
|
||||
pub y: u16,
|
||||
}
|
||||
impl AtlasOffset {
|
||||
pub(crate) fn to_sf_rect(&self) -> IntRect {
|
||||
IntRect {
|
||||
left: self.x as i32,
|
||||
top: self.y as i32,
|
||||
width: TILE_SIZE as i32,
|
||||
height: TILE_SIZE as i32,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for AtlasOffset {
|
||||
fn default() -> Self {
|
||||
UNKNOWN_ATLAS_OFF
|
||||
}
|
||||
}
|
||||
|
||||
const UNKNOWN_ATLAS_OFF: AtlasOffset = AtlasOffset { x: 320, y: 0 };
|
||||
|
||||
static UNKNOWN_TILE: TileDef = TileDef {
|
||||
solid: true,
|
||||
emits_light: true,
|
||||
atlas_offset: UNKNOWN_ATLAS_OFF,
|
||||
};
|
||||
|
||||
const PATH: &str = "tiles.dat";
|
||||
|
||||
impl TileDb {
|
||||
pub fn load_or_default() -> Self {
|
||||
match std::fs::read(PATH) {
|
||||
Ok(data) => match rmp_serde::from_slice(&data) {
|
||||
Ok(db) => db,
|
||||
Err(e) => {
|
||||
warn!("Failed to load tile database: {e}\nCreating default.");
|
||||
Default::default()
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
warn!("Failed to load tile database: {e}\nCreating default.");
|
||||
Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn try_save(&self) {
|
||||
match rmp_serde::to_vec(self) {
|
||||
Ok(vec) => match std::fs::write(PATH, vec) {
|
||||
Ok(()) => {}
|
||||
Err(e) => warn!("Failed to save tile db: {e}"),
|
||||
},
|
||||
Err(e) => warn!("Failed to save tile db: {e}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue