Commit b3a967db authored by Florian Blasius's avatar Florian Blasius 🤘
Browse files

Implemet game builder. Adjust main and adventure example.

parent c449e296
......@@ -2,9 +2,10 @@ extern crate orbgame;
use std::env;
use orbgame::Game;
use orbgame::GameBuilder;
fn main() {
Game::from_toml("examples/adventure/game.toml").exec();
let args: Vec<String> = env::args().collect();
let game_builder = GameBuilder::new("examples/adventure/game.ron");
game_builder.build().expect("Could not load game").exec();
}
\ No newline at end of file
......@@ -3,4 +3,5 @@ Game (
width: 800,
height: 600,
target_fps: 60,
theme: "ui.css",
)
\ No newline at end of file
......@@ -6,13 +6,12 @@
use std::fs::File;
// use ron::value::Value;
use ron;
use ron::value::Value;
use ron::de::from_reader;
use orbtk::{Rect, Window};
// use orbtk::{Place, Rect, Window};
// use super::{ScriptEngine, Stage};
use super::{ScriptEngine};
#[derive(Clone, Debug, Deserialize, Default)]
#[serde(rename = "Game")]
......@@ -20,17 +19,14 @@ pub struct Config {
pub title: String,
pub width: u32,
pub height: u32,
pub target_fps: u32
pub target_fps: u32,
pub theme: String,
}
impl Config {
pub fn from_ron(path: &str) -> ron::de::Result<Config> {
from_reader(File::open(&path).expect("Failed opening file"))
}
}
pub struct Game {
window: Window,
target_fps: u32,
script_engine: ScriptEngine,
}
impl Game {
......@@ -38,16 +34,7 @@ impl Game {
Game {
window: Window::new(Rect::new(0, 0, config.width, config.height), &config.title),
target_fps: config.target_fps,
}
}
pub fn from_ron(path: &str) -> Result<Game, String> {
let config = Config::from_ron(path);
println!("{:?}", config);
if let Ok(config) = Config::from_ron(path) {
return Ok(Game::from_config(&config))
} else {
Err(String::from("Cloud not load game"))
script_engine: ScriptEngine::new(),
}
}
......
use std::fs::File;
use ron;
use ron::de::from_reader;
use game::{Config, Game};
pub struct GameBuilder {
path: String,
}
impl GameBuilder {
pub fn new(path: &str) -> Self {
GameBuilder {
path: String::from(path)
}
}
pub fn build(&self) -> Result<Game, String> {
if let Ok(file) = File::open(&self.path) {
let config: ron::de::Result<Config> = from_reader(file);
if let Ok(config) = config {
return Ok(Game::from_config(&config))
} else {
return Err(String::from("Could not parse game file."))
}
}
Err(String::from("Could not load game file."))
}
}
\ No newline at end of file
......@@ -49,17 +49,19 @@ static HEIGHT_KEY: &str = "height";
// pub use self::camera::*;
// pub use self::entity::*;
pub use self::game_builder::*;
pub use self::game::*;
// pub use self::tile_map::*;
// pub use self::script_engine::ScriptEngine;
pub use self::script_engine::ScriptEngine;
// pub use self::sprite::Sprite;
// pub use self::stage::*;
// mod camera;
// mod entity;
mod game_builder;
mod game;
// mod tile_map;
// mod script_engine;
mod script_engine;
// mod sprite;
// mod stage;
......
......@@ -2,9 +2,10 @@ extern crate orbgame;
use std::env;
use orbgame::Game;
use orbgame::GameBuilder;
fn main() {
let args: Vec<String> = env::args().collect();
Game::from_ron(&args[1][..]).expect("Could not load game").exec();
let game_builder = GameBuilder::new(&args[1][..]);
game_builder.build().expect("Could not load game").exec();
}
use rhai::{Engine, RegisterFn, Scope};
use sprite::Sprite;
use entity::Entity;
use stage::Stage;
// use sprite::Sprite;
// use entity::Entity;
// use stage::Stage;
pub struct ScriptEngine {
inner_engine: Engine,
......@@ -15,9 +15,9 @@ impl ScriptEngine {
let mut inner_engine = Engine::new();
let scope = Scope::new();
inner_engine.register_type::<Sprite>();
inner_engine.register_type::<Entity>();
inner_engine.register_type::<Stage>();
// inner_engine.register_type::<Sprite>();
// inner_engine.register_type::<Entity>();
// inner_engine.register_type::<Stage>();
ScriptEngine {
inner_engine,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment