peachy/src/lib.rs

157 lines
3.5 KiB
Rust

use std::fs;
use std::path::PathBuf;
mod colour;
mod config;
mod image;
mod mock;
mod music;
mod palette;
mod scene;
pub use colour::Colour;
pub use config::Config;
pub use music::Music;
pub use palette::Palette;
pub use scene::Scene;
#[derive(Debug, Eq, Hash, PartialEq)]
pub struct Position {
x: u8,
y: u8,
}
// #[derive(Serialize, Deserialize)]
// pub struct Thing {
// name: Option<String>,
// /// image name
// image: String,
// }
//
// #[derive(Serialize, Deserialize)]
// pub enum DataType {
// Image,
// Integer,
// Palette,
// Room,
// Script,
// String,
// Thing,
// Variable,
// }
//
// /// todo refactor, this is stupid
// #[derive(Serialize, Deserialize)]
// pub struct Value {
// data_type: DataType,
// image: Option<String>,
// integer: Option<u64>,
// palette: Option<String>,
// room: Option<String>,
// script: Option<String>,
// string: Option<String>,
// thing: Option<String>,
// variable: Option<String>,
// }
//
// #[derive(Serialize, Deserialize)]
// pub struct Variable {
// name: String,
// data_type: DataType,
// default: Value,
// }
//
// #[derive(Serialize, Deserialize)]
// pub struct Parameter {
// name: String,
// data_type: DataType,
// default: Value,
// }
//
// #[derive(Serialize, Deserialize)]
// pub struct Script {
// name: Option<String>,
// params: Vec<Parameter>,
// script: String,
// }
//
// #[derive(Serialize, Deserialize)]
// pub struct ScriptInstance {
// script: String,
// params: HashMap<String, Value>,
// }
//
// #[derive(Serialize, Deserialize)]
// pub struct ScriptCollection {
// scripts: Vec<ScriptInstance>,
// /// as well as many named scripts, a trigger can have one anonymous script
// anonymous: Option<String>,
// }
//
// #[derive(Serialize, Deserialize)]
// pub struct Version {
// major: u8,
// minor: u8,
// }
//
// impl Version {
// pub fn default() -> Version {
// Version { major: 0, minor: 1 }
// }
// }
pub struct Game {
config: Config,
palettes: Vec<Palette>,
// variables: Vec<Variable>,
// triggers: HashMap<String, ScriptCollection>,
music: Vec<Music>,
}
#[derive(Debug)]
pub struct GameParseError;
impl Game {
pub fn from(path: String) -> Result<Game, GameParseError> {
let path = PathBuf::from(path);
let mut music = Vec::new();
let mut palettes = Vec::new();
let mut music_dir = path.clone();
music_dir.push("music");
let music_files = music_dir.read_dir();
if music_files.is_ok() {
for file in music_files.unwrap() {
let file = file.unwrap();
music.push(Music::from_file(file.path()));
println!("music found: {:?}", file.file_name());
}
}
let mut palettes_dir = path.clone();
palettes_dir.push("palettes");
let palette_files = palettes_dir.read_dir()
.expect("couldn't read palettes dir");
for file in palette_files {
let file = file.unwrap();
palettes.push(Palette::from_file(file.path()));
println!("palette found: {:?}", file.file_name());
}
let mut game_config = path.clone();
game_config.push("game.toml");
let config = fs::read_to_string(game_config)
.expect("Couldn't load game config");
let config: Config = toml::from_str(&config)
.expect("Couldn't parse game config");
Ok(Game { config, palettes, music })
}
}