fix game runner and start migrating to actual game data; fix game data loading errors

This commit is contained in:
Max Bradbury 2021-05-19 20:09:12 +01:00
parent d93f777b66
commit 98288a4426
3 changed files with 75 additions and 69 deletions

View File

@ -1,5 +1,8 @@
#[windows_subsystem = "windows"] #[windows_subsystem = "windows"]
use std::collections::HashMap;
use std::path::PathBuf;
use log::error; use log::error;
use pixels::{Error, SurfaceTexture, PixelsBuilder}; use pixels::{Error, SurfaceTexture, PixelsBuilder};
use pixels::wgpu::BackendBit; use pixels::wgpu::BackendBit;
@ -7,14 +10,16 @@ use winit::dpi::{LogicalPosition, LogicalSize, PhysicalSize};
use winit::event::{Event, VirtualKeyCode}; use winit::event::{Event, VirtualKeyCode};
use winit::event_loop::{ControlFlow, EventLoop}; use winit::event_loop::{ControlFlow, EventLoop};
use winit_input_helper::WinitInputHelper; use winit_input_helper::WinitInputHelper;
use std::collections::HashMap;
use peachy::Game;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
struct Image { struct Image {
pixels: [u8; 64] pixels: [u8; 64]
} }
struct Game { struct State {
game: Game,
width: usize, width: usize,
height: usize, height: usize,
player_position: (u8, u8), player_position: (u8, u8),
@ -24,7 +29,7 @@ struct Game {
music: HashMap<String, rodio::Sink>, music: HashMap<String, rodio::Sink>,
} }
impl Game { impl State {
fn draw(&self, screen: &mut [u8]) { fn draw(&self, screen: &mut [u8]) {
// clear screen // clear screen
for pixel in screen.chunks_exact_mut(4) { for pixel in screen.chunks_exact_mut(4) {
@ -68,7 +73,11 @@ impl Game {
fn main() -> Result<(), Error> { fn main() -> Result<(), Error> {
env_logger::init(); env_logger::init();
let mut game = Game { let path = PathBuf::from("src/test-resources/basic");
let game = peachy::Game::from_dir(path).unwrap();
let mut state = State {
game,
width: 16, width: 16,
height: 9, height: 9,
player_position: (8, 4), player_position: (8, 4),
@ -97,48 +106,50 @@ fn main() -> Result<(), Error> {
let (window, p_width, p_height, mut _hidpi_factor) = create_window( let (window, p_width, p_height, mut _hidpi_factor) = create_window(
"pixels test", "pixels test",
(game.width * 8) as f64, (state.game.config.width * 8) as f64,
(game.height * 8) as f64, (state.game.config.height * 8) as f64,
&event_loop &event_loop
); );
let surface_texture = SurfaceTexture::new(p_width, p_height, &window); let surface_texture = SurfaceTexture::new(p_width, p_height, &window);
let mut pixels = PixelsBuilder::new( let mut pixels = PixelsBuilder::new(
(game.width * 8) as u32, (game.height * 8) as u32, surface_texture (state.game.config.width * 8) as u32,
(state.game.config.height * 8) as u32,
surface_texture
) )
.wgpu_backend(BackendBit::GL | BackendBit::PRIMARY) .wgpu_backend(BackendBit::GL | BackendBit::PRIMARY)
.enable_vsync(false) .enable_vsync(false)
.build()?; .build()?;
let device = rodio::default_output_device().unwrap(); // let device = rodio::default_output_device().unwrap();
//
// let source = rodio_xm::XMSource::from_bytes(
// include_bytes!("../ninety degrees.xm")
// );
//
// let sink = rodio::Sink::new(&device);
// sink.append(source);
// sink.pause();
//
// game.music.insert(":ninety degrees".into(), sink);
//
// let source = rodio_xm::XMSource::from_bytes(
// include_bytes!("../orn_keygentheme2001.xm")
// );
//
// let sink = rodio::Sink::new(&device);
// sink.append(source);
// sink.pause();
//
// game.music.insert("orn_keygentheme2001".into(), sink);
let source = rodio_xm::XMSource::from_bytes( state.current_music = None;
include_bytes!("../ninety degrees.xm")
);
let sink = rodio::Sink::new(&device);
sink.append(source);
sink.pause();
game.music.insert(":ninety degrees".into(), sink);
let source = rodio_xm::XMSource::from_bytes(
include_bytes!("../orn_keygentheme2001.xm")
);
let sink = rodio::Sink::new(&device);
sink.append(source);
sink.pause();
game.music.insert("orn_keygentheme2001".into(), sink);
game.current_music = None;
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
// The one and only event that winit_input_helper doesn't have for us... // The one and only event that winit_input_helper doesn't have for us...
if let Event::RedrawRequested(_) = event { if let Event::RedrawRequested(_) = event {
game.draw(pixels.get_frame()); state.draw(pixels.get_frame());
if pixels if pixels
.render() .render()
@ -161,49 +172,49 @@ fn main() -> Result<(), Error> {
if input.key_pressed(VirtualKeyCode::M) { if input.key_pressed(VirtualKeyCode::M) {
// pause the current tune // pause the current tune
if game.current_music.is_some() { if state.current_music.is_some() {
game.music.get(game.current_music.as_ref().unwrap()).unwrap().pause(); state.music.get(state.current_music.as_ref().unwrap()).unwrap().pause();
} }
if game.current_music.is_none() || game.current_music.as_ref().unwrap() == "orn_keygentheme2001" { if state.current_music.is_none() || state.current_music.as_ref().unwrap() == "orn_keygentheme2001" {
// play the first tune // play the first tune
game.current_music = Some(":ninety degrees".into()); state.current_music = Some(":ninety degrees".into());
game.music.get(game.current_music.as_ref().unwrap()).unwrap().play(); state.music.get(state.current_music.as_ref().unwrap()).unwrap().play();
} else { } else {
// play the second tune // play the second tune
game.current_music = Some("orn_keygentheme2001".into()); state.current_music = Some("orn_keygentheme2001".into());
game.music.get(game.current_music.as_ref().unwrap()).unwrap().play(); state.music.get(state.current_music.as_ref().unwrap()).unwrap().play();
} }
} }
if input.key_pressed(VirtualKeyCode::Left) { if input.key_pressed(VirtualKeyCode::Left) {
let (x, y) = game.player_position; let (x, y) = state.player_position;
if x > 0 { if x > 0 {
game.player_position = (x - 1, y); state.player_position = (x - 1, y);
window.request_redraw(); window.request_redraw();
} }
} }
if input.key_pressed(VirtualKeyCode::Right) { if input.key_pressed(VirtualKeyCode::Right) {
let (x, y) = game.player_position; let (x, y) = state.player_position;
if x < game.width as u8 - 1 { if x < state.game.config.width as u8 - 1 {
game.player_position = (x + 1, y); state.player_position = (x + 1, y);
window.request_redraw(); window.request_redraw();
} }
} }
if input.key_pressed(VirtualKeyCode::Up) { if input.key_pressed(VirtualKeyCode::Up) {
let (x, y) = game.player_position; let (x, y) = state.player_position;
if y > 0 { if y > 0 {
game.player_position = (x, y - 1); state.player_position = (x, y - 1);
window.request_redraw(); window.request_redraw();
} }
} }
if input.key_pressed(VirtualKeyCode::Down) { if input.key_pressed(VirtualKeyCode::Down) {
let (x, y) = game.player_position; let (x, y) = state.player_position;
if y < game.height as u8 - 1 { if y < state.game.config.height as u8 - 1 {
game.player_position = (x, y + 1); state.player_position = (x, y + 1);
window.request_redraw(); window.request_redraw();
} }
} }

View File

@ -3,15 +3,15 @@ use serde_derive::{Serialize, Deserialize};
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] #[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Config { pub struct Config {
/// used in the window title bar /// used in the window title bar
name: Option<String>, pub name: Option<String>,
width: u8, pub width: u8,
height: u8, pub height: u8,
/// animation rate in milliseconds /// animation rate in milliseconds
tick: u64, pub tick: u64,
/// if this is not specified, the game will pick the first room it finds /// if this is not specified, the game will pick the first room it finds
starting_room: Option<String>, pub starting_room: Option<String>,
/// major / minor /// major / minor
version: (u8, u8), pub version: (u8, u8),
} }
#[cfg(test)] #[cfg(test)]

View File

@ -99,14 +99,14 @@ pub struct Position {
// } // }
pub struct Game { pub struct Game {
config: Config, pub config: Config,
palettes: Vec<Palette>, pub palettes: Vec<Palette>,
images: Vec<Image>, pub images: Vec<Image>,
tiles: Vec<Tile>, pub tiles: Vec<Tile>,
entities: Vec<Entity>, pub entities: Vec<Entity>,
// variables: Vec<Variable>, // pub variables: Vec<Variable>,
// triggers: HashMap<String, ScriptCollection>, // pub triggers: HashMap<String, ScriptCollection>,
music: Vec<Music>, pub music: Vec<Music>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -129,7 +129,6 @@ impl Game {
for file in music_files.unwrap() { for file in music_files.unwrap() {
let file = file.unwrap(); let file = file.unwrap();
music.push(Music::from_file(file.path())); music.push(Music::from_file(file.path()));
println!("music found: {:?}", file.file_name());
} }
} }
@ -142,11 +141,10 @@ impl Game {
for file in palette_files { for file in palette_files {
let file = file.unwrap(); let file = file.unwrap();
palettes.push(Palette::from_file(file.path())); palettes.push(Palette::from_file(file.path()));
println!("palette found: {:?}", file.file_name());
} }
let mut images_dir = path.clone(); let mut images_dir = path.clone();
images_dir.push("palettes"); images_dir.push("images");
let image_files = images_dir.read_dir() let image_files = images_dir.read_dir()
.expect("couldn't read image dir"); .expect("couldn't read image dir");
@ -154,11 +152,10 @@ impl Game {
for file in image_files { for file in image_files {
let file = file.unwrap(); let file = file.unwrap();
images.push(Image::from_file(file.path())); images.push(Image::from_file(file.path()));
println!("image found: {:?}", file.file_name());
} }
let mut tiles_dir = path.clone(); let mut tiles_dir = path.clone();
tiles_dir.push("palettes"); tiles_dir.push("tiles");
let tiles_files = tiles_dir.read_dir() let tiles_files = tiles_dir.read_dir()
.expect("couldn't read tile dir"); .expect("couldn't read tile dir");
@ -166,11 +163,10 @@ impl Game {
for file in tiles_files { for file in tiles_files {
let file = file.unwrap(); let file = file.unwrap();
tiles.push(Tile::from_file(file.path())); tiles.push(Tile::from_file(file.path()));
println!("tile found: {:?}", file.file_name());
} }
let mut entities_dir = path.clone(); let mut entities_dir = path.clone();
entities_dir.push("palettes"); entities_dir.push("entities");
let entities_files = entities_dir.read_dir() let entities_files = entities_dir.read_dir()
.expect("couldn't read tile dir"); .expect("couldn't read tile dir");
@ -178,7 +174,6 @@ impl Game {
for file in entities_files { for file in entities_files {
let file = file.unwrap(); let file = file.unwrap();
entities.push(Entity::from_file(file.path())); 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();