Adapted game logic for Directives

master
Mark 2024-02-07 15:58:38 -08:00
parent b170f3f53f
commit c8f2001426
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
2 changed files with 90 additions and 83 deletions

View File

@ -2,6 +2,7 @@ use galactica_content::{Content, ContentIndex};
use galactica_playeragent::PlayerAgent; use galactica_playeragent::PlayerAgent;
use galactica_system::data::ShipPersonality; use galactica_system::data::ShipPersonality;
use galactica_system::phys::{PhysImage, PhysSim, PhysSimShipHandle, PhysStepResources}; use galactica_system::phys::{PhysImage, PhysSim, PhysSimShipHandle, PhysStepResources};
use galactica_system::PlayerDirective;
use galactica_util::timing::Timing; use galactica_util::timing::Timing;
use nalgebra::Point2; use nalgebra::Point2;
use std::sync::Arc; use std::sync::Arc;
@ -130,8 +131,10 @@ impl<'a> Game {
} }
} }
pub fn update_player_controls(&mut self, player: &mut PlayerAgent) { pub fn apply_directive(&mut self, directive: PlayerDirective, player: &PlayerAgent) {
self.phys_sim.update_player_controls(player) match directive {
_ => self.phys_sim.apply_directive(directive, player),
}
} }
pub fn step(&mut self, phys_img: &PhysImage) { pub fn step(&mut self, phys_img: &PhysImage) {

View File

@ -3,12 +3,9 @@ mod game;
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use clap::Parser; use clap::Parser;
use galactica_content::Content; use galactica_content::Content;
use galactica_playeragent::{PlayerAgent, PlayerStatus}; use galactica_playeragent::PlayerAgent;
use galactica_render::RenderInput; use galactica_render::{InputEvent, RenderInput};
use galactica_system::{ use galactica_system::phys::PhysImage;
data::ShipState,
phys::{PhysImage, PhysSimShipHandle},
};
use galactica_util::constants::ASSET_CACHE; use galactica_util::constants::ASSET_CACHE;
use log::LevelFilter; use log::LevelFilter;
use log4rs::{ use log4rs::{
@ -17,15 +14,13 @@ use log4rs::{
encode::pattern::PatternEncoder, encode::pattern::PatternEncoder,
Config, Config,
}; };
use nalgebra::Vector2;
use std::{ use std::{
fs, fs,
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::Arc, sync::Arc,
time::Instant,
}; };
use winit::{ use winit::{
event::{Event, KeyboardInput, WindowEvent}, event::{ElementState, Event, KeyboardInput, MouseButton, MouseScrollDelta, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::{ControlFlow, EventLoop},
window::WindowBuilder, window::WindowBuilder,
}; };
@ -128,32 +123,21 @@ fn try_main() -> Result<()> {
let mut game = game::Game::new(content.clone()); let mut game = game::Game::new(content.clone());
let p = game.make_player(); let p = game.make_player();
let mut player = Arc::new(PlayerAgent::new(&content, p.0)); let player = Arc::new(PlayerAgent::new(&content, p.0));
Arc::get_mut(&mut player).unwrap().set_camera_aspect(
gpu.window().inner_size().width as f32 / gpu.window().inner_size().height as f32,
);
let mut phys_img = Arc::new(PhysImage::new()); let mut phys_img = Arc::new(PhysImage::new());
let mut last_run = Instant::now();
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
match event { match event {
Event::RedrawRequested(window_id) if window_id == gpu.window().id() => { Event::RedrawRequested(window_id) if window_id == gpu.window().id() => {
let render_input = RenderInput { match gpu.render(RenderInput {
camera_pos: player.camera.pos,
camera_zoom: player.camera.zoom,
current_time: game.get_current_time(), current_time: game.get_current_time(),
ct: content.clone(), ct: content.clone(),
phys_img: phys_img.clone(), phys_img: phys_img.clone(),
player: player.clone(), player: player.clone(),
time_since_last_run: last_run.elapsed().as_secs_f32(),
// TODO: this is a hack for testing. // TODO: this is a hack for testing.
current_system: content.systems.values().next().unwrap().clone(), current_system: content.systems.values().next().unwrap().clone(),
timing: game.get_timing().clone(), timing: game.get_timing().clone(),
}; }) {
last_run = Instant::now();
match gpu.render(render_input) {
Ok(_) => {} Ok(_) => {}
Err(wgpu::SurfaceError::Lost) => gpu.resize(&content), Err(wgpu::SurfaceError::Lost) => gpu.resize(&content),
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit, Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
@ -163,40 +147,8 @@ fn try_main() -> Result<()> {
} }
Event::MainEventsCleared => { Event::MainEventsCleared => {
game.update_player_controls(Arc::get_mut(&mut player).unwrap());
game.step(&phys_img); game.step(&phys_img);
game.update_image(Arc::get_mut(&mut phys_img).unwrap()); game.update_image(Arc::get_mut(&mut phys_img).unwrap());
// TODO: clean up
let player_status = {
let pos = {
let o = phys_img.get_ship(&PhysSimShipHandle(player.ship.unwrap()));
if let Some(o) = o {
match o.ship.get_data().get_state() {
ShipState::Landing { .. }
| ShipState::UnLanding { .. }
| ShipState::Collapsing { .. }
| ShipState::Flying { .. } => Some(*o.rigidbody.translation()),
ShipState::Landed { target } => {
Some(Vector2::new(target.pos.x, target.pos.y))
}
ShipState::Dead => None,
}
} else {
None
}
};
PlayerStatus { pos }
};
// This must be updated BEFORE rendering!
Arc::get_mut(&mut player)
.unwrap()
.step(&content, player_status);
Arc::get_mut(&mut player).unwrap().input.clear_inputs();
gpu.window().request_redraw(); gpu.window().request_redraw();
} }
@ -205,7 +157,7 @@ fn try_main() -> Result<()> {
window_id, window_id,
} if window_id == gpu.window().id() => match event { } if window_id == gpu.window().id() => match event {
WindowEvent::Focused(_state) => { WindowEvent::Focused(_state) => {
//game.set_paused(!state); // TODO: handle focus loss
} }
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::KeyboardInput { WindowEvent::KeyboardInput {
@ -217,42 +169,94 @@ fn try_main() -> Result<()> {
}, },
.. ..
} => { } => {
Arc::get_mut(&mut player) let directive = gpu
.unwrap() .process_input(
.input RenderInput {
.process_key(state, key); current_time: game.get_current_time(),
ct: content.clone(),
phys_img: phys_img.clone(),
player: player.clone(),
current_system: content.systems.values().next().unwrap().clone(),
timing: game.get_timing().clone(),
},
InputEvent::Keyboard {
down: state == &ElementState::Pressed,
key: *key,
},
)
.unwrap();
game.apply_directive(directive, &player);
} }
WindowEvent::CursorMoved { position, .. } => { WindowEvent::CursorMoved { position, .. } => {
Arc::get_mut(&mut player) let directive = gpu
.unwrap() .process_input(
.input RenderInput {
.process_mouse(position); current_time: game.get_current_time(),
ct: content.clone(),
phys_img: phys_img.clone(),
player: player.clone(),
current_system: content.systems.values().next().unwrap().clone(),
timing: game.get_timing().clone(),
},
InputEvent::MouseMove(position.cast()),
)
.unwrap();
game.apply_directive(directive, &player);
} }
WindowEvent::MouseInput { state, button, .. } => { WindowEvent::MouseInput { state, button, .. } => {
Arc::get_mut(&mut player) let down = state == &ElementState::Pressed;
let event = match button {
MouseButton::Left => Some(InputEvent::MouseLeftClick(down)),
MouseButton::Right => Some(InputEvent::MouseRightClick(down)),
_ => None,
};
if let Some(event) = event {
let directive = gpu
.process_input(
RenderInput {
current_time: game.get_current_time(),
ct: content.clone(),
phys_img: phys_img.clone(),
player: player.clone(),
current_system: content
.systems
.values()
.next()
.unwrap() .unwrap()
.input .clone(),
.process_click(state, button); timing: game.get_timing().clone(),
},
event,
)
.unwrap();
game.apply_directive(directive, &player);
} }
WindowEvent::MouseWheel { delta, phase, .. } => { }
Arc::get_mut(&mut player) WindowEvent::MouseWheel { delta, .. } => {
.unwrap() let directive = gpu
.input .process_input(
.process_scroll(delta, phase); RenderInput {
current_time: game.get_current_time(),
ct: content.clone(),
phys_img: phys_img.clone(),
player: player.clone(),
current_system: content.systems.values().next().unwrap().clone(),
timing: game.get_timing().clone(),
},
InputEvent::Scroll(match delta {
MouseScrollDelta::LineDelta(_h, v) => *v,
MouseScrollDelta::PixelDelta(v) => v.x as f32,
}),
)
.unwrap();
game.apply_directive(directive, &player);
} }
WindowEvent::Resized(_) => { WindowEvent::Resized(_) => {
gpu.resize(&content); gpu.resize(&content);
Arc::get_mut(&mut player).unwrap().set_camera_aspect(
gpu.window().inner_size().width as f32
/ gpu.window().inner_size().height as f32,
);
} }
WindowEvent::ScaleFactorChanged { .. } => { WindowEvent::ScaleFactorChanged { .. } => {
gpu.resize(&content); gpu.resize(&content);
Arc::get_mut(&mut player).unwrap().set_camera_aspect( gpu.window().request_redraw();
gpu.window().inner_size().width as f32
/ gpu.window().inner_size().height as f32,
);
} }
_ => {} _ => {}
}, },