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 config;
mod entity;
mod image;
mod mock;
mod music;
@ -12,10 +13,12 @@ mod tile;
pub use colour::Colour;
pub use config::Config;
pub use entity::Entity;
pub use crate::image::Image;
pub use music::Music;
pub use palette::Palette;
pub use scene::Scene;
pub use tile::Tile;
#[derive(Debug, Eq, Hash, PartialEq)]
pub struct Position {
@ -107,8 +110,8 @@ pub struct Game {
config: Config,
palettes: Vec<Palette>,
images: Vec<Image>,
// todo tiles
// todo things
tiles: Vec<Tile>,
entities: Vec<Entity>,
// variables: Vec<Variable>,
// triggers: HashMap<String, ScriptCollection>,
music: Vec<Music>,
@ -122,6 +125,8 @@ impl Game {
let path = PathBuf::from(path);
let mut images = Vec::new();
let mut tiles = Vec::new();
let mut entities = Vec::new();
let mut music = Vec::new();
let mut palettes = Vec::new();
@ -154,7 +159,7 @@ impl Game {
images_dir.push("palettes");
let image_files = images_dir.read_dir()
.expect("couldn't read images dir");
.expect("couldn't read image dir");
for file in image_files {
let file = file.unwrap();
@ -162,6 +167,30 @@ impl Game {
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();
game_config.push("game.toml");
let config = fs::read_to_string(game_config)
@ -169,6 +198,6 @@ impl Game {
let config: Config = toml::from_str(&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 do we need a "current frame" property or leave that up to the player implementation?
pub images: Vec<String>,
pub wall: bool,
}
impl Tile {
@ -19,13 +20,14 @@ impl Tile {
&read_to_string(path).unwrap()
).unwrap();
Tile { name, images: intermediate.images }
Tile { name, images: intermediate.images, wall: intermediate.wall }
}
}
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
struct IntermediateTile {
images: Vec<String>,
wall: bool,
}
#[cfg(test)]
@ -35,9 +37,15 @@ mod test {
#[test]
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 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);
}
}