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

This commit is contained in:
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();
}
}