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",
"galactica-constants",
"galactica-content",
"galactica-physics",
"galactica-render",
"image",
"nalgebra",
@ -610,6 +611,25 @@ dependencies = [
"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]]
name = "galactica-render"
version = "0.0.0"

View File

@ -28,7 +28,12 @@ rpath = false
[workspace]
members = ["crates/content", "crates/render", "crates/constants"]
members = [
"crates/content",
"crates/render",
"crates/constants",
"crates/physics",
]
[dependencies]
@ -36,6 +41,7 @@ members = ["crates/content", "crates/render", "crates/constants"]
galactica-content = { path = "crates/content" }
galactica-render = { path = "crates/render" }
galactica-constants = { path = "crates/constants" }
galactica-physics = { path = "crates/physics" }
# Files
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;
pub mod util;
mod wrapper;
@ -7,4 +8,4 @@ pub use physics::Physics;
use rapier2d::{dynamics::RigidBodyHandle, geometry::ColliderHandle};
#[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.
mod outfits;
mod projectile;
mod ship;
pub use outfits::{ShipGun, ShipOutfits};
pub use projectile::{Projectile, ProjectileBuilder};
pub use ship::Ship;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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