Compare commits
3 Commits
3f76ee4f03
...
b72e8f5ac8
Author | SHA1 | Date |
---|---|---|
Max Bradbury | b72e8f5ac8 | |
Max Bradbury | 6637d87d45 | |
Max Bradbury | 0abc64946e |
|
@ -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);
|
||||
}
|
||||
}
|
37
src/lib.rs
37
src/lib.rs
|
@ -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 })
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
image = "avatar"
|
||||
tags = []
|
|
@ -1 +0,0 @@
|
|||
images = ["avatar"]
|
|
@ -0,0 +1,2 @@
|
|||
images = ["block"]
|
||||
wall = false
|
14
src/tile.rs
14
src/tile.rs
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue