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

View File

@ -3,12 +3,9 @@ mod game;
use anyhow::{bail, Result};
use clap::Parser;
use galactica_content::Content;
use galactica_playeragent::{PlayerAgent, PlayerStatus};
use galactica_render::RenderInput;
use galactica_system::{
data::ShipState,
phys::{PhysImage, PhysSimShipHandle},
};
use galactica_playeragent::PlayerAgent;
use galactica_render::{InputEvent, RenderInput};
use galactica_system::phys::PhysImage;
use galactica_util::constants::ASSET_CACHE;
use log::LevelFilter;
use log4rs::{
@ -17,15 +14,13 @@ use log4rs::{
encode::pattern::PatternEncoder,
Config,
};
use nalgebra::Vector2;
use std::{
fs,
path::{Path, PathBuf},
sync::Arc,
time::Instant,
};
use winit::{
event::{Event, KeyboardInput, WindowEvent},
event::{ElementState, Event, KeyboardInput, MouseButton, MouseScrollDelta, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
@ -128,32 +123,21 @@ fn try_main() -> Result<()> {
let mut game = game::Game::new(content.clone());
let p = game.make_player();
let mut 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 player = Arc::new(PlayerAgent::new(&content, p.0));
let mut phys_img = Arc::new(PhysImage::new());
let mut last_run = Instant::now();
event_loop.run(move |event, _, control_flow| {
match event {
Event::RedrawRequested(window_id) if window_id == gpu.window().id() => {
let render_input = RenderInput {
camera_pos: player.camera.pos,
camera_zoom: player.camera.zoom,
match gpu.render(RenderInput {
current_time: game.get_current_time(),
ct: content.clone(),
phys_img: phys_img.clone(),
player: player.clone(),
time_since_last_run: last_run.elapsed().as_secs_f32(),
// TODO: this is a hack for testing.
current_system: content.systems.values().next().unwrap().clone(),
timing: game.get_timing().clone(),
};
last_run = Instant::now();
match gpu.render(render_input) {
}) {
Ok(_) => {}
Err(wgpu::SurfaceError::Lost) => gpu.resize(&content),
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
@ -163,40 +147,8 @@ fn try_main() -> Result<()> {
}
Event::MainEventsCleared => {
game.update_player_controls(Arc::get_mut(&mut player).unwrap());
game.step(&phys_img);
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();
}
@ -205,7 +157,7 @@ fn try_main() -> Result<()> {
window_id,
} if window_id == gpu.window().id() => match event {
WindowEvent::Focused(_state) => {
//game.set_paused(!state);
// TODO: handle focus loss
}
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::KeyboardInput {
@ -217,42 +169,94 @@ fn try_main() -> Result<()> {
},
..
} => {
Arc::get_mut(&mut player)
.unwrap()
.input
.process_key(state, key);
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().clone(),
timing: game.get_timing().clone(),
},
InputEvent::Keyboard {
down: state == &ElementState::Pressed,
key: *key,
},
)
.unwrap();
game.apply_directive(directive, &player);
}
WindowEvent::CursorMoved { position, .. } => {
Arc::get_mut(&mut player)
.unwrap()
.input
.process_mouse(position);
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().clone(),
timing: game.get_timing().clone(),
},
InputEvent::MouseMove(position.cast()),
)
.unwrap();
game.apply_directive(directive, &player);
}
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()
.input
.process_click(state, button);
.clone(),
timing: game.get_timing().clone(),
},
event,
)
.unwrap();
game.apply_directive(directive, &player);
}
WindowEvent::MouseWheel { delta, phase, .. } => {
Arc::get_mut(&mut player)
.unwrap()
.input
.process_scroll(delta, phase);
}
WindowEvent::MouseWheel { delta, .. } => {
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().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(_) => {
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 { .. } => {
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,
);
gpu.window().request_redraw();
}
_ => {}
},