Moved `physics` to crate

master
Mark 2024-01-01 11:29:48 -08:00
parent 02220902ca
commit bd8651b02a
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
17 changed files with 106 additions and 70 deletions

20
Cargo.lock generated
View File

@ -582,6 +582,7 @@ dependencies = [
"crossbeam", "crossbeam",
"galactica-constants", "galactica-constants",
"galactica-content", "galactica-content",
"galactica-physics",
"galactica-render", "galactica-render",
"image", "image",
"nalgebra", "nalgebra",
@ -610,6 +611,25 @@ dependencies = [
"walkdir", "walkdir",
] ]
[[package]]
name = "galactica-physics"
version = "0.0.0"
dependencies = [
"anyhow",
"cgmath",
"crossbeam",
"galactica-content",
"galactica-render",
"image",
"nalgebra",
"pollster",
"rand",
"rapier2d",
"walkdir",
"wgpu",
"winit",
]
[[package]] [[package]]
name = "galactica-render" name = "galactica-render"
version = "0.0.0" version = "0.0.0"

View File

@ -28,7 +28,12 @@ rpath = false
[workspace] [workspace]
members = ["crates/content", "crates/render", "crates/constants"] members = [
"crates/content",
"crates/render",
"crates/constants",
"crates/physics",
]
[dependencies] [dependencies]
@ -36,6 +41,7 @@ members = ["crates/content", "crates/render", "crates/constants"]
galactica-content = { path = "crates/content" } galactica-content = { path = "crates/content" }
galactica-render = { path = "crates/render" } galactica-render = { path = "crates/render" }
galactica-constants = { path = "crates/constants" } galactica-constants = { path = "crates/constants" }
galactica-physics = { path = "crates/physics" }
# Files # Files
image = { version = "0.24", features = ["png"] } image = { version = "0.24", features = ["png"] }

27
crates/physics/Cargo.toml Normal file
View File

@ -0,0 +1,27 @@
[package]
name = "galactica-physics"
version = "0.0.0"
edition = "2021"
[dependencies]
# Internal crates
galactica-render = { path = "../render" }
galactica-content = { path = "../content" }
# Files
image = { version = "0.24", features = ["png"] }
# Graphics
winit = "0.28"
wgpu = "0.18"
# Physics
rapier2d = { version = "0.17.2" }
nalgebra = "0.32.3"
crossbeam = "0.8.3"
# Misc helpers
pollster = "0.3"
anyhow = "1.0"
# TODO: migrate to nalgebra
cgmath = "0.18.0"
rand = "0.8.5"
walkdir = "2.4.0"

View File

@ -1,3 +1,4 @@
pub mod objects;
mod physics; mod physics;
pub mod util; pub mod util;
mod wrapper; mod wrapper;
@ -7,4 +8,4 @@ pub use physics::Physics;
use rapier2d::{dynamics::RigidBodyHandle, geometry::ColliderHandle}; use rapier2d::{dynamics::RigidBodyHandle, geometry::ColliderHandle};
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct ShipHandle(pub(super) RigidBodyHandle, ColliderHandle); pub struct ShipHandle(pub RigidBodyHandle, ColliderHandle);

View File

@ -1,7 +1,9 @@
//! This module contains game objects that may interact with the physics engine. //! This module contains game objects that may interact with the physics engine.
mod outfits;
mod projectile; mod projectile;
mod ship; mod ship;
pub use outfits::{ShipGun, ShipOutfits};
pub use projectile::{Projectile, ProjectileBuilder}; pub use projectile::{Projectile, ProjectileBuilder};
pub use ship::Ship; pub use ship::Ship;

View File

@ -1,8 +1,7 @@
use cgmath::{Deg, Point3}; use cgmath::{Deg, Point3};
use galactica_content as content;
use galactica_render::ObjectSubSprite; use galactica_render::ObjectSubSprite;
use crate::content;
/// Represents a gun attached to a specific ship at a certain gunpoint. /// Represents a gun attached to a specific ship at a certain gunpoint.
#[derive(Debug)] #[derive(Debug)]
pub struct ShipGun { pub struct ShipGun {

View File

@ -1,11 +1,12 @@
use cgmath::{Deg, InnerSpace, Point3, Vector2}; use cgmath::{Deg, InnerSpace, Point3, Vector2};
use galactica_render::ObjectSprite;
use rapier2d::{ use rapier2d::{
dynamics::{RigidBody, RigidBodyBuilder, RigidBodyHandle}, dynamics::{RigidBody, RigidBodyBuilder, RigidBodyHandle},
geometry::{ColliderBuilder, ColliderHandle}, geometry::{ColliderBuilder, ColliderHandle},
}; };
use crate::{content, physics::util}; use crate::util;
use galactica_content as content;
use galactica_render::ObjectSprite;
pub struct ProjectileBuilder { pub struct ProjectileBuilder {
pub rigid_body: RigidBodyBuilder, pub rigid_body: RigidBodyBuilder,

View File

@ -1,6 +1,5 @@
use cgmath::{Deg, EuclideanSpace, InnerSpace, Matrix2, Rad, Vector2}; use cgmath::{Deg, EuclideanSpace, InnerSpace, Matrix2, Rad, Vector2};
use content::{FactionHandle, TextureHandle}; use content::{FactionHandle, TextureHandle};
use galactica_render::ObjectSprite;
use nalgebra::vector; use nalgebra::vector;
use rand::Rng; use rand::Rng;
use rapier2d::{ use rapier2d::{
@ -9,12 +8,10 @@ use rapier2d::{
pipeline::ActiveEvents, pipeline::ActiveEvents,
}; };
use super::ProjectileBuilder; use super::{ProjectileBuilder, ShipOutfits};
use crate::{ use crate::{util, ShipHandle};
content, use galactica_content as content;
game::outfits, use galactica_render::ObjectSprite;
physics::{util, ShipHandle},
};
pub struct ShipTickResult { pub struct ShipTickResult {
pub projectiles: Vec<ProjectileBuilder>, pub projectiles: Vec<ProjectileBuilder>,
@ -44,7 +41,7 @@ pub struct Ship {
pub hull: f32, pub hull: f32,
pub controls: ShipControls, pub controls: ShipControls,
outfits: outfits::ShipOutfits, outfits: ShipOutfits,
sprite_texture: TextureHandle, sprite_texture: TextureHandle,
size: f32, size: f32,
} }
@ -52,7 +49,7 @@ pub struct Ship {
impl Ship { impl Ship {
pub fn new( pub fn new(
c: &content::Ship, c: &content::Ship,
outfits: outfits::ShipOutfits, outfits: ShipOutfits,
physics_handle: ShipHandle, physics_handle: ShipHandle,
faction: FactionHandle, faction: FactionHandle,
) -> Self { ) -> Self {

View File

@ -1,6 +1,5 @@
use cgmath::Point2; use cgmath::Point2;
use crossbeam::channel::Receiver; use crossbeam::channel::Receiver;
use galactica_render::ObjectSprite;
use nalgebra::vector; use nalgebra::vector;
use rapier2d::{ use rapier2d::{
dynamics::{RigidBody, RigidBodyBuilder, RigidBodyHandle}, dynamics::{RigidBody, RigidBodyBuilder, RigidBodyHandle},
@ -9,8 +8,9 @@ use rapier2d::{
}; };
use std::{collections::HashMap, f32::consts::PI}; use std::{collections::HashMap, f32::consts::PI};
use super::{wrapper::Wrapper, ShipHandle}; use crate::{objects, objects::ShipOutfits, wrapper::Wrapper, ShipHandle};
use crate::{content, game::outfits, objects}; use galactica_content as content;
use galactica_render::ObjectSprite;
/// Keeps track of all objects in the world that we can interact with. /// Keeps track of all objects in the world that we can interact with.
/// Also wraps our physics engine /// Also wraps our physics engine
@ -82,7 +82,7 @@ impl Physics {
pub fn add_ship( pub fn add_ship(
&mut self, &mut self,
ct: &content::Ship, ct: &content::Ship,
outfits: outfits::ShipOutfits, outfits: ShipOutfits,
position: Point2<f32>, position: Point2<f32>,
faction: content::FactionHandle, faction: content::FactionHandle,
) -> ShipHandle { ) -> ShipHandle {

View File

@ -8,7 +8,7 @@ use rapier2d::{
pipeline::{EventHandler, PhysicsPipeline}, pipeline::{EventHandler, PhysicsPipeline},
}; };
pub(super) struct Wrapper { pub(crate) struct Wrapper {
pub rigid_body_set: RigidBodySet, pub rigid_body_set: RigidBodySet,
pub collider_set: ColliderSet, pub collider_set: ColliderSet,

View File

@ -1,16 +1,16 @@
use cgmath::{Deg, InnerSpace, Point2}; use cgmath::{Deg, InnerSpace, Point2};
use galactica_constants;
use galactica_render::{AnchoredUiPosition, ObjectSprite, UiSprite};
use std::time::Instant; use std::time::Instant;
use winit::event::{ElementState, MouseButton, MouseScrollDelta, TouchPhase, VirtualKeyCode}; use winit::event::{ElementState, MouseButton, MouseScrollDelta, TouchPhase, VirtualKeyCode};
use super::{camera::Camera, outfits, system::System}; use super::{camera::Camera, system::System};
use crate::{ use crate::{
content, content,
inputstatus::InputStatus, inputstatus::InputStatus,
physics::{util, Physics, ShipHandle},
shipbehavior::{self, ShipBehavior}, shipbehavior::{self, ShipBehavior},
}; };
use galactica_constants;
use galactica_physics::{objects::ShipOutfits, util, Physics, ShipHandle};
use galactica_render::{AnchoredUiPosition, ObjectSprite, UiSprite};
struct Ui {} struct Ui {}
@ -87,7 +87,7 @@ impl Game {
pub fn new(ct: content::Content) -> Self { pub fn new(ct: content::Content) -> Self {
let mut physics = Physics::new(); let mut physics = Physics::new();
let mut o1 = outfits::ShipOutfits::new(&ct.ships[0]); let mut o1 = ShipOutfits::new(&ct.ships[0]);
o1.add(ct.outfits[0].clone()); o1.add(ct.outfits[0].clone());
o1.add_gun(ct.guns[0].clone()); o1.add_gun(ct.guns[0].clone());
o1.add_gun(ct.guns[0].clone()); o1.add_gun(ct.guns[0].clone());
@ -101,12 +101,12 @@ impl Game {
let h2 = physics.add_ship( let h2 = physics.add_ship(
&ct.ships[0], &ct.ships[0],
outfits::ShipOutfits::new(&ct.ships[0]), ShipOutfits::new(&ct.ships[0]),
Point2 { x: 300.0, y: 300.0 }, Point2 { x: 300.0, y: 300.0 },
content::FactionHandle { index: 0 }, content::FactionHandle { index: 0 },
); );
let mut o2 = outfits::ShipOutfits::new(&ct.ships[0]); let mut o2 = ShipOutfits::new(&ct.ships[0]);
o2.add(ct.outfits[0].clone()); o2.add(ct.outfits[0].clone());
o2.add_gun(ct.guns[0].clone()); o2.add_gun(ct.guns[0].clone());
let h3 = physics.add_ship( let h3 = physics.add_ship(
@ -172,7 +172,7 @@ impl Game {
if self.physics.get_ship_mut(&b.get_handle()).is_none() { if self.physics.get_ship_mut(&b.get_handle()).is_none() {
false false
} else { } else {
b.update_controls(&mut self.physics, &self.input, &self.content); b.update_controls(&mut self.physics, &self.content);
true true
} }
}); });

View File

@ -1,8 +1,7 @@
//! This module contains high-level game control routines. //! This module contains high-level game control logic.
mod camera; mod camera;
mod game; mod game;
pub mod outfits;
mod system; mod system;
mod systemobject; mod systemobject;

View File

@ -1,7 +1,6 @@
use galactica_render::ObjectSprite;
use super::SystemObject; use super::SystemObject;
use crate::content; use crate::content;
use galactica_render::ObjectSprite;
pub struct System { pub struct System {
pub name: String, pub name: String,

View File

@ -1,5 +1,6 @@
use crate::content;
use cgmath::{Deg, Point3}; use cgmath::{Deg, Point3};
use crate::content;
use galactica_render::ObjectSprite; use galactica_render::ObjectSprite;
pub struct SystemObject { pub struct SystemObject {

View File

@ -1,7 +1,5 @@
mod game; mod game;
mod inputstatus; mod inputstatus;
mod objects;
mod physics;
mod shipbehavior; mod shipbehavior;
pub use galactica_content as content; pub use galactica_content as content;

View File

@ -1,21 +1,13 @@
use cgmath::{Deg, InnerSpace}; use cgmath::{Deg, InnerSpace};
use crate::{ use crate::content;
content, use galactica_physics::{util, Physics, ShipHandle};
inputstatus::InputStatus,
physics::{util, Physics, ShipHandle},
};
pub trait ShipBehavior pub trait ShipBehavior
where where
Self: Send, Self: Send,
{ {
fn update_controls( fn update_controls(&mut self, physics: &mut Physics, content: &content::Content);
&mut self,
physics: &mut Physics,
input: &InputStatus,
content: &content::Content,
);
fn get_handle(&self) -> ShipHandle; fn get_handle(&self) -> ShipHandle;
} }
@ -30,13 +22,7 @@ impl Dummy {
} }
impl ShipBehavior for Dummy { impl ShipBehavior for Dummy {
fn update_controls( fn update_controls(&mut self, _physics: &mut Physics, _content: &content::Content) {}
&mut self,
_physics: &mut Physics,
_input: &InputStatus,
_content: &content::Content,
) {
}
fn get_handle(&self) -> ShipHandle { fn get_handle(&self) -> ShipHandle {
return self._handle; return self._handle;
} }
@ -44,26 +30,31 @@ impl ShipBehavior for Dummy {
pub struct Player { pub struct Player {
handle: ShipHandle, handle: ShipHandle,
key_left: bool,
key_right: bool,
key_guns: bool,
key_thrust: bool,
} }
impl Player { impl Player {
pub fn new(handle: ShipHandle) -> Box<Self> { pub fn new(handle: ShipHandle) -> Box<Self> {
Box::new(Self { handle }) Box::new(Self {
handle,
key_left: false,
key_right: false,
key_guns: false,
key_thrust: false,
})
} }
} }
impl ShipBehavior for Player { impl ShipBehavior for Player {
fn update_controls( fn update_controls(&mut self, physics: &mut Physics, _content: &content::Content) {
&mut self,
physics: &mut Physics,
input: &InputStatus,
_content: &content::Content,
) {
let s = physics.get_ship_mut(&self.handle).unwrap(); let s = physics.get_ship_mut(&self.handle).unwrap();
s.controls.left = input.key_left; s.controls.left = self.key_left;
s.controls.right = input.key_right; s.controls.right = self.key_right;
s.controls.guns = input.key_guns; s.controls.guns = self.key_guns;
s.controls.thrust = input.key_thrust; s.controls.thrust = self.key_thrust;
} }
fn get_handle(&self) -> ShipHandle { fn get_handle(&self) -> ShipHandle {
@ -82,12 +73,7 @@ impl Point {
} }
impl ShipBehavior for Point { impl ShipBehavior for Point {
fn update_controls( fn update_controls(&mut self, physics: &mut Physics, content: &content::Content) {
&mut self,
physics: &mut Physics,
_input: &InputStatus,
content: &content::Content,
) {
// Turn off all controls // Turn off all controls
let s = physics.get_ship_mut(&self.handle).unwrap(); let s = physics.get_ship_mut(&self.handle).unwrap();
s.controls.left = false; s.controls.left = false;