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

View File

@ -3,15 +3,15 @@ use serde_derive::{Serialize, Deserialize};
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Config {
/// used in the window title bar
name: Option<String>,
width: u8,
height: u8,
pub name: Option<String>,
pub width: u8,
pub height: u8,
/// animation rate in milliseconds
tick: u64,
pub tick: u64,
/// 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
version: (u8, u8),
pub version: (u8, u8),
}
#[cfg(test)]

View File

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