Compare commits

..

3 Commits

Author SHA1 Message Date
Max Bradbury b72e8f5ac8 tiles and entities 2021-05-19 11:42:49 +01:00
Max Bradbury 6637d87d45 add Entity 2021-05-19 11:07:49 +01:00
Max Bradbury 0abc64946e add wall property to tile; why did I make "avatar" a tile? smh 2021-05-19 10:26:37 +01:00
6 changed files with 90 additions and 8 deletions

42
src/entity.rs Normal file
View File

@ -0,0 +1,42 @@
use std::fs::read_to_string;
use std::path::PathBuf;
use serde_derive::{Serialize, Deserialize};
#[derive(Debug, Eq, PartialEq)]
pub struct Entity {
name: String,
image: String,
tags: Vec<String>,
}
impl Entity {
pub fn from_file(path: PathBuf) -> Self {
let name = path.file_stem().unwrap().to_str().unwrap().into();
let intermediate: IntermediateEntity = toml::from_str(
&read_to_string(path).unwrap()
).unwrap();
Self { name, image: intermediate.image, tags: intermediate.tags }
}
}
#[derive(Debug, Serialize, Deserialize)]
pub(crate) struct IntermediateEntity {
image: String,
tags: Vec<String>,
}
#[cfg(test)]
mod test {
use std::path::PathBuf;
use crate::entity::Entity;
#[test]
fn from_file() {
let path = PathBuf::from("src/test-resources/basic/entities/avatar.toml");
let output = Entity::from_file(path);
let expected = Entity { name: "avatar".into(), image: "avatar".into(), tags: vec![] };
assert_eq!(output, expected);
}
}

View File

@ -3,6 +3,7 @@ use std::path::PathBuf;
mod colour; mod colour;
mod config; mod config;
mod entity;
mod image; mod image;
mod mock; mod mock;
mod music; mod music;
@ -12,10 +13,12 @@ mod tile;
pub use colour::Colour; pub use colour::Colour;
pub use config::Config; pub use config::Config;
pub use entity::Entity;
pub use crate::image::Image; pub use crate::image::Image;
pub use music::Music; pub use music::Music;
pub use palette::Palette; pub use palette::Palette;
pub use scene::Scene; pub use scene::Scene;
pub use tile::Tile;
#[derive(Debug, Eq, Hash, PartialEq)] #[derive(Debug, Eq, Hash, PartialEq)]
pub struct Position { pub struct Position {
@ -107,8 +110,8 @@ pub struct Game {
config: Config, config: Config,
palettes: Vec<Palette>, palettes: Vec<Palette>,
images: Vec<Image>, images: Vec<Image>,
// todo tiles tiles: Vec<Tile>,
// todo things entities: Vec<Entity>,
// variables: Vec<Variable>, // variables: Vec<Variable>,
// triggers: HashMap<String, ScriptCollection>, // triggers: HashMap<String, ScriptCollection>,
music: Vec<Music>, music: Vec<Music>,
@ -122,6 +125,8 @@ impl Game {
let path = PathBuf::from(path); let path = PathBuf::from(path);
let mut images = Vec::new(); let mut images = Vec::new();
let mut tiles = Vec::new();
let mut entities = Vec::new();
let mut music = Vec::new(); let mut music = Vec::new();
let mut palettes = Vec::new(); let mut palettes = Vec::new();
@ -154,7 +159,7 @@ impl Game {
images_dir.push("palettes"); images_dir.push("palettes");
let image_files = images_dir.read_dir() let image_files = images_dir.read_dir()
.expect("couldn't read images dir"); .expect("couldn't read image dir");
for file in image_files { for file in image_files {
let file = file.unwrap(); let file = file.unwrap();
@ -162,6 +167,30 @@ impl Game {
println!("image found: {:?}", file.file_name()); println!("image found: {:?}", file.file_name());
} }
let mut tiles_dir = path.clone();
tiles_dir.push("palettes");
let tiles_files = tiles_dir.read_dir()
.expect("couldn't read tile dir");
for file in tiles_files {
let file = file.unwrap();
tiles.push(Tile::from_file(file.path()));
println!("tile found: {:?}", file.file_name());
}
let mut entities_dir = path.clone();
entities_dir.push("palettes");
let entities_files = entities_dir.read_dir()
.expect("couldn't read tile dir");
for file in entities_files {
let file = file.unwrap();
entities.push(Entity::from_file(file.path()));
println!("entity found: {:?}", file.file_name());
}
let mut game_config = path.clone(); let mut game_config = path.clone();
game_config.push("game.toml"); game_config.push("game.toml");
let config = fs::read_to_string(game_config) let config = fs::read_to_string(game_config)
@ -169,6 +198,6 @@ impl Game {
let config: Config = toml::from_str(&config) let config: Config = toml::from_str(&config)
.expect("Couldn't parse game config"); .expect("Couldn't parse game config");
Ok(Game { config, images, palettes, music }) Ok(Game { config, images, tiles, palettes, music, entities })
} }
} }

View File

@ -0,0 +1,2 @@
image = "avatar"
tags = []

View File

@ -1 +0,0 @@
images = ["avatar"]

View File

@ -0,0 +1,2 @@
images = ["block"]
wall = false

View File

@ -9,6 +9,7 @@ pub struct Tile {
/// todo should there be animation options? reverse, random, etc? /// todo should there be animation options? reverse, random, etc?
/// todo do we need a "current frame" property or leave that up to the player implementation? /// todo do we need a "current frame" property or leave that up to the player implementation?
pub images: Vec<String>, pub images: Vec<String>,
pub wall: bool,
} }
impl Tile { impl Tile {
@ -19,13 +20,14 @@ impl Tile {
&read_to_string(path).unwrap() &read_to_string(path).unwrap()
).unwrap(); ).unwrap();
Tile { name, images: intermediate.images } Tile { name, images: intermediate.images, wall: intermediate.wall }
} }
} }
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] #[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
struct IntermediateTile { struct IntermediateTile {
images: Vec<String>, images: Vec<String>,
wall: bool,
} }
#[cfg(test)] #[cfg(test)]
@ -35,9 +37,15 @@ mod test {
#[test] #[test]
fn from_file() { fn from_file() {
let path = PathBuf::from("src/test-resources/basic/tiles/avatar.toml"); let path = PathBuf::from("src/test-resources/basic/tiles/block.toml");
let output = Tile::from_file(path); let output = Tile::from_file(path);
let expected = Tile { name: "avatar".into(), images: vec!["avatar".to_string()] };
let expected = Tile {
name: "block".into(),
images: vec!["block".to_string()],
wall: false
};
assert_eq!(output, expected); assert_eq!(output, expected);
} }
} }