forked from StrafesNET/strafe-project
Compare commits
2 Commits
game-mecha
...
thread-tex
Author | SHA1 | Date | |
---|---|---|---|
17b0d12d4d | |||
1f9bdd9a34 |
25
Cargo.lock
generated
25
Cargo.lock
generated
@ -834,29 +834,6 @@ dependencies = [
|
||||
"pkg-config",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy-regex"
|
||||
version = "3.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e723bd417b2df60a0f6a2b6825f297ea04b245d4ba52b5a22cb679bdf58b05fa"
|
||||
dependencies = [
|
||||
"lazy-regex-proc_macros",
|
||||
"once_cell",
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy-regex-proc_macros"
|
||||
version = "3.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0f0a1d9139f0ee2e862e08a9c5d0ba0470f2aa21cd1e1aa1b1562f83116c725f"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"regex",
|
||||
"syn 2.0.29",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
@ -1689,7 +1666,6 @@ dependencies = [
|
||||
"ddsfile",
|
||||
"env_logger",
|
||||
"glam",
|
||||
"lazy-regex",
|
||||
"log",
|
||||
"obj",
|
||||
"pollster",
|
||||
@ -1697,6 +1673,7 @@ dependencies = [
|
||||
"rbx_dom_weak",
|
||||
"rbx_reflection_database",
|
||||
"rbx_xml",
|
||||
"regex",
|
||||
"wgpu",
|
||||
"winit",
|
||||
]
|
||||
|
@ -11,7 +11,6 @@ bytemuck = { version = "1.13.1", features = ["derive"] }
|
||||
ddsfile = "0.5.1"
|
||||
env_logger = "0.10.0"
|
||||
glam = "0.24.1"
|
||||
lazy-regex = "3.0.2"
|
||||
log = "0.4.20"
|
||||
obj = "0.10.2"
|
||||
pollster = "0.3.0"
|
||||
@ -19,6 +18,7 @@ rbx_binary = "0.7.1"
|
||||
rbx_dom_weak = "2.5.0"
|
||||
rbx_reflection_database = "0.2.7"
|
||||
rbx_xml = "0.13.1"
|
||||
regex = "1.9.5"
|
||||
wgpu = "0.17.0"
|
||||
winit = "0.28.6"
|
||||
|
||||
|
174
src/body.rs
174
src/body.rs
@ -97,8 +97,6 @@ enum MouseInterpolation {
|
||||
First,//just checks the last value
|
||||
Lerp,//lerps between
|
||||
}
|
||||
|
||||
//hey dumbass just use a delta
|
||||
pub struct MouseInterpolationState {
|
||||
interpolation: MouseInterpolation,
|
||||
time0: TIME,
|
||||
@ -257,70 +255,28 @@ fn get_control_dir(controls: u32) -> glam::Vec3{
|
||||
return control_dir
|
||||
}
|
||||
|
||||
pub struct GameMechanicsState{
|
||||
pub spawn_id:u32,
|
||||
//jump_counts:HashMap<u32,u32>,
|
||||
}
|
||||
impl std::default::Default for GameMechanicsState{
|
||||
fn default() -> Self {
|
||||
Self{
|
||||
spawn_id:0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WorldState{}
|
||||
|
||||
pub struct StyleModifiers{
|
||||
pub constrols_mask:u32,//constrols which are unable to be activated
|
||||
pub constrols_held:u32,//constrols which must be active to be able to strafe
|
||||
pub mv:f32,
|
||||
pub walkspeed:f32,
|
||||
pub friction:f32,
|
||||
pub walk_accel:f32,
|
||||
pub gravity:glam::Vec3,
|
||||
pub strafe_tick_num:TIME,
|
||||
pub strafe_tick_den:TIME,
|
||||
pub hitbox_halfsize:glam::Vec3,
|
||||
}
|
||||
impl std::default::Default for StyleModifiers{
|
||||
fn default() -> Self {
|
||||
Self{
|
||||
constrols_mask: !0&!(CONTROL_MOVEUP|CONTROL_MOVEDOWN),
|
||||
constrols_held: 0,
|
||||
strafe_tick_num: 100,//100t
|
||||
strafe_tick_den: 1_000_000_000,
|
||||
gravity: glam::vec3(0.0,-100.0,0.0),
|
||||
friction: 1.2,
|
||||
walk_accel: 90.0,
|
||||
mv: 2.7,
|
||||
walkspeed: 18.0,
|
||||
hitbox_halfsize: glam::vec3(1.0,2.5,1.0),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PhysicsState{
|
||||
pub time:TIME,
|
||||
pub body:Body,
|
||||
pub world:WorldState,//currently there is only one state the world can be in
|
||||
pub game:GameMechanicsState,
|
||||
pub style:StyleModifiers,
|
||||
pub contacts:std::collections::HashSet::<RelativeCollision>,
|
||||
pub struct PhysicsState {
|
||||
pub body: Body,
|
||||
pub hitbox_halfsize: glam::Vec3,
|
||||
pub contacts: std::collections::HashSet::<RelativeCollision>,
|
||||
//pub intersections: Vec<ModelId>,
|
||||
pub models: Vec<ModelPhysics>,
|
||||
//camera must exist in state because wormholes modify the camera, also camera punch
|
||||
pub camera:Camera,
|
||||
pub mouse_interpolation:MouseInterpolationState,
|
||||
pub controls:u32,
|
||||
pub walk:WalkState,
|
||||
pub grounded:bool,
|
||||
//all models
|
||||
pub models:Vec<ModelPhysics>,
|
||||
|
||||
pub stages:Vec<crate::model::StageDescription>,
|
||||
//the spawn point is where you spawn when you load into the map.
|
||||
//This is not the same as Reset which teleports you to Spawn0
|
||||
pub spawn_point:glam::Vec3,
|
||||
pub camera: Camera,
|
||||
pub mouse_interpolation: MouseInterpolationState,
|
||||
pub controls: u32,
|
||||
pub time: TIME,
|
||||
pub strafe_tick_num: TIME,
|
||||
pub strafe_tick_den: TIME,
|
||||
pub tick: u32,
|
||||
pub mv: f32,
|
||||
pub walk: WalkState,
|
||||
pub walkspeed: f32,
|
||||
pub friction: f32,
|
||||
pub walk_accel: f32,
|
||||
pub gravity: glam::Vec3,
|
||||
pub grounded: bool,
|
||||
pub spawn_point: glam::Vec3,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone,Copy,Hash,Eq,PartialEq)]
|
||||
@ -432,40 +388,20 @@ impl Aabb {
|
||||
type TreyMeshFace = AabbFace;
|
||||
type TreyMesh = Aabb;
|
||||
|
||||
enum PhysicsCollisionAttributes{
|
||||
Contact{//track whether you are contacting the object
|
||||
contacting:crate::model::ContactingAttributes,
|
||||
general:crate::model::GameMechanicAttributes,
|
||||
},
|
||||
Intersect{//track whether you are intersecting the object
|
||||
intersecting:crate::model::IntersectingAttributes,
|
||||
general:crate::model::GameMechanicAttributes,
|
||||
},
|
||||
}
|
||||
|
||||
pub struct ModelPhysics {
|
||||
//A model is a thing that has a hitbox. can be represented by a list of TreyMesh-es
|
||||
//in this iteration, all it needs is extents.
|
||||
mesh: TreyMesh,
|
||||
attributes:PhysicsCollisionAttributes,
|
||||
}
|
||||
|
||||
impl ModelPhysics {
|
||||
fn from_model_transform_attributes(model:&crate::model::IndexedModel,transform:&glam::Affine3A,attributes:PhysicsCollisionAttributes)->Self{
|
||||
pub fn from_model(model:&crate::model::IndexedModel,model_transform:glam::Affine3A) -> Self {
|
||||
let mut aabb=Aabb::new();
|
||||
for indexed_vertex in &model.unique_vertices {
|
||||
aabb.grow(transform.transform_point3(glam::Vec3::from_array(model.unique_pos[indexed_vertex.pos as usize])));
|
||||
aabb.grow(model_transform.transform_point3(glam::Vec3::from_array(model.unique_pos[indexed_vertex.pos as usize])));
|
||||
}
|
||||
Self{
|
||||
mesh:aabb,
|
||||
attributes,
|
||||
}
|
||||
}
|
||||
pub fn from_model(model:&crate::model::IndexedModel,instance:&crate::model::ModelInstance) -> Option<Self> {
|
||||
match &instance.attributes{
|
||||
crate::model::CollisionAttributes::Decoration=>None,
|
||||
crate::model::CollisionAttributes::Contact{contacting,general}=>Some(ModelPhysics::from_model_transform_attributes(model,&instance.transform,PhysicsCollisionAttributes::Contact{contacting:contacting.clone(),general:general.clone()})),
|
||||
crate::model::CollisionAttributes::Intersect{intersecting,general}=>None,//Some(ModelPhysics::from_model_transform_attributes(model,&instance.transform,PhysicsCollisionAttributes::Intersecting{intersecting,general})),
|
||||
}
|
||||
}
|
||||
pub fn unit_vertices(&self) -> [glam::Vec3;8] {
|
||||
@ -584,7 +520,7 @@ impl PhysicsState {
|
||||
}
|
||||
fn next_strafe_instruction(&self) -> Option<TimedInstruction<PhysicsInstruction>> {
|
||||
return Some(TimedInstruction{
|
||||
time:(self.time*self.style.strafe_tick_num/self.style.strafe_tick_den+1)*self.style.strafe_tick_den/self.style.strafe_tick_num,
|
||||
time:(self.time*self.strafe_tick_num/self.strafe_tick_den+1)*self.strafe_tick_den/self.strafe_tick_num,
|
||||
//only poll the physics if there is a before and after mouse event
|
||||
instruction:PhysicsInstruction::StrafeTick
|
||||
});
|
||||
@ -634,7 +570,7 @@ impl PhysicsState {
|
||||
self.body.acceleration=a;
|
||||
self.walk.state=WalkEnum::Reached;
|
||||
}else{
|
||||
let accel=self.style.walk_accel.min(self.style.gravity.length()*self.style.friction);
|
||||
let accel=self.walk_accel.min(self.gravity.length()*self.friction);
|
||||
let time_delta=target_diff.length()/accel;
|
||||
let mut a=target_diff/time_delta;
|
||||
self.contact_constrain_acceleration(&mut a);
|
||||
@ -663,7 +599,7 @@ impl PhysicsState {
|
||||
fn mesh(&self) -> TreyMesh {
|
||||
let mut aabb=Aabb::new();
|
||||
for vertex in Aabb::unit_vertices(){
|
||||
aabb.grow(self.body.position+self.style.hitbox_halfsize*vertex);
|
||||
aabb.grow(self.body.position+self.hitbox_halfsize*vertex);
|
||||
}
|
||||
aabb
|
||||
}
|
||||
@ -1007,7 +943,7 @@ impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsStat
|
||||
},
|
||||
PhysicsInstruction::CollisionEnd(c) => {
|
||||
self.contacts.remove(&c);//remove contact before calling contact_constrain_acceleration
|
||||
let mut a=self.style.gravity;
|
||||
let mut a=self.gravity;
|
||||
self.contact_constrain_acceleration(&mut a);
|
||||
self.body.acceleration=a;
|
||||
//check ground
|
||||
@ -1023,8 +959,8 @@ impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsStat
|
||||
let camera_mat=self.camera.simulate_move_rotation_y(self.mouse_interpolation.interpolated_position(self.time).x-self.mouse_interpolation.mouse0.x);
|
||||
let control_dir=camera_mat*get_control_dir(self.controls);
|
||||
let d=self.body.velocity.dot(control_dir);
|
||||
if d<self.style.mv {
|
||||
let mut v=self.body.velocity+(self.style.mv-d)*control_dir;
|
||||
if d<self.mv {
|
||||
let mut v=self.body.velocity+(self.mv-d)*control_dir;
|
||||
self.contact_constrain_velocity(&mut v);
|
||||
self.body.velocity=v;
|
||||
}
|
||||
@ -1040,50 +976,64 @@ impl crate::instruction::InstructionConsumer<PhysicsInstruction> for PhysicsStat
|
||||
self.walk.state=WalkEnum::Reached;
|
||||
},
|
||||
PhysicsInstruction::Input(input_instruction) => {
|
||||
let mut refresh_walk_target=true;
|
||||
let mut refresh_walk_target_velocity=true;
|
||||
let mut refresh_walk_target=false;
|
||||
match input_instruction{
|
||||
InputInstruction::MoveMouse(m) => {
|
||||
self.camera.angles=self.camera.simulate_move_angles(self.mouse_interpolation.mouse1-self.mouse_interpolation.mouse0);
|
||||
self.mouse_interpolation.move_mouse(self.time,m);
|
||||
refresh_walk_target=true;
|
||||
},
|
||||
InputInstruction::MoveForward(s) => {
|
||||
self.set_control(CONTROL_MOVEFORWARD,s);
|
||||
refresh_walk_target=true;
|
||||
},
|
||||
InputInstruction::MoveLeft(s) => {
|
||||
self.set_control(CONTROL_MOVELEFT,s);
|
||||
refresh_walk_target=true;
|
||||
},
|
||||
InputInstruction::MoveBack(s) => {
|
||||
self.set_control(CONTROL_MOVEBACK,s);
|
||||
refresh_walk_target=true;
|
||||
},
|
||||
InputInstruction::MoveRight(s) => {
|
||||
self.set_control(CONTROL_MOVERIGHT,s);
|
||||
refresh_walk_target=true;
|
||||
},
|
||||
InputInstruction::MoveUp(s) => {
|
||||
self.set_control(CONTROL_MOVEUP,s);
|
||||
refresh_walk_target=true;
|
||||
},
|
||||
InputInstruction::MoveDown(s) => {
|
||||
self.set_control(CONTROL_MOVEDOWN,s);
|
||||
refresh_walk_target=true;
|
||||
},
|
||||
InputInstruction::MoveForward(s) => self.set_control(CONTROL_MOVEFORWARD,s),
|
||||
InputInstruction::MoveLeft(s) => self.set_control(CONTROL_MOVELEFT,s),
|
||||
InputInstruction::MoveBack(s) => self.set_control(CONTROL_MOVEBACK,s),
|
||||
InputInstruction::MoveRight(s) => self.set_control(CONTROL_MOVERIGHT,s),
|
||||
InputInstruction::MoveUp(s) => self.set_control(CONTROL_MOVEUP,s),
|
||||
InputInstruction::MoveDown(s) => self.set_control(CONTROL_MOVEDOWN,s),
|
||||
InputInstruction::Jump(s) => {
|
||||
self.set_control(CONTROL_JUMP,s);
|
||||
refresh_walk_target=true;
|
||||
if self.grounded{
|
||||
self.jump();
|
||||
}
|
||||
refresh_walk_target_velocity=false;
|
||||
},
|
||||
InputInstruction::Zoom(s) => {
|
||||
self.set_control(CONTROL_ZOOM,s);
|
||||
refresh_walk_target=false;
|
||||
},
|
||||
InputInstruction::Reset => {
|
||||
//temp
|
||||
self.body.position=self.spawn_point;
|
||||
self.body.velocity=glam::Vec3::ZERO;
|
||||
//manual clear //for c in self.contacts{process_instruction(CollisionEnd(c))}
|
||||
self.contacts.clear();
|
||||
self.body.acceleration=self.style.gravity;
|
||||
self.body.acceleration=self.gravity;
|
||||
self.walk.state=WalkEnum::Reached;
|
||||
self.grounded=false;
|
||||
refresh_walk_target=false;
|
||||
},
|
||||
InputInstruction::Idle => {refresh_walk_target=false;},//literally idle!
|
||||
InputInstruction::Idle => (),//literally idle!
|
||||
}
|
||||
//calculate control dir
|
||||
let camera_mat=self.camera.simulate_move_rotation_y(self.mouse_interpolation.interpolated_position(self.time).x-self.mouse_interpolation.mouse0.x);
|
||||
let control_dir=camera_mat*get_control_dir(self.controls);
|
||||
//calculate walk target velocity
|
||||
if refresh_walk_target{
|
||||
//calculate walk target velocity
|
||||
if refresh_walk_target_velocity{
|
||||
let camera_mat=self.camera.simulate_move_rotation_y(self.mouse_interpolation.interpolated_position(self.time).x-self.mouse_interpolation.mouse0.x);
|
||||
let control_dir=camera_mat*get_control_dir(self.controls);
|
||||
self.walk.target_velocity=self.style.walkspeed*control_dir;
|
||||
}
|
||||
self.walk.target_velocity=self.walkspeed*control_dir;
|
||||
self.refresh_walk_target();
|
||||
}
|
||||
},
|
||||
|
@ -51,9 +51,8 @@ pub trait Example: 'static + Sized {
|
||||
device: &wgpu::Device,
|
||||
queue: &wgpu::Queue,
|
||||
);
|
||||
fn update(&mut self, window: &winit::window::Window, device: &wgpu::Device, queue: &wgpu::Queue, event: WindowEvent);
|
||||
fn device_event(&mut self, window: &winit::window::Window, event: DeviceEvent);
|
||||
fn load_file(&mut self, path:std::path::PathBuf, device: &wgpu::Device, queue: &wgpu::Queue);
|
||||
fn update(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, event: WindowEvent);
|
||||
fn device_event(&mut self, event: DeviceEvent);
|
||||
fn render(
|
||||
&mut self,
|
||||
view: &wgpu::TextureView,
|
||||
@ -368,14 +367,14 @@ fn start<E: Example>(
|
||||
println!("{:#?}", instance.generate_report());
|
||||
}
|
||||
_ => {
|
||||
example.update(&window,&device,&queue,event);
|
||||
example.update(&device,&queue,event);
|
||||
}
|
||||
},
|
||||
event::Event::DeviceEvent {
|
||||
event,
|
||||
..
|
||||
} => {
|
||||
example.device_event(&window,event);
|
||||
example.device_event(event);
|
||||
},
|
||||
event::Event::RedrawRequested(_) => {
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
use crate::model::{IndexedModelInstances,ModelInstance};
|
||||
|
||||
use crate::primitives;
|
||||
|
||||
fn class_is_a(class: &str, superclass: &str) -> bool {
|
||||
@ -30,71 +32,13 @@ fn get_texture_refs(dom:&rbx_dom_weak::WeakDom) -> Vec<rbx_dom_weak::types::Ref>
|
||||
//next class
|
||||
objects
|
||||
}
|
||||
fn get_attributes(name:&str,can_collide:bool,velocity:glam::Vec3)->crate::model::CollisionAttributes{
|
||||
let mut general=crate::model::GameMechanicAttributes::default();
|
||||
let mut intersecting=crate::model::IntersectingAttributes::default();
|
||||
let mut contacting=crate::model::ContactingAttributes::default();
|
||||
match name{
|
||||
// "Water"=>intersecting.water=Some(crate::model::IntersectingWater::default()),
|
||||
// "Accelerator"=>(),
|
||||
// "MapFinish"=>(),
|
||||
// "MapAnticheat"=>(),
|
||||
"Platform"=>general.stage_element=Some(crate::model::GameMechanicStageElement{
|
||||
mode_id:0,
|
||||
stage_id:0,
|
||||
force:false,
|
||||
behaviour:crate::model::StageElementBehaviour::Platform,
|
||||
}),
|
||||
other=>{
|
||||
let regman=lazy_regex::regex!(r"^(Force)?(SpawnAt|Trigger|Teleport|Platform)(\d+)$");
|
||||
if let Some(captures) = regman.captures(other) {
|
||||
general.stage_element=Some(crate::model::GameMechanicStageElement{
|
||||
mode_id:0,
|
||||
stage_id:captures[3].parse::<u32>().unwrap(),
|
||||
force:match captures.get(1){
|
||||
Some(m)=>m.as_str()=="Force",
|
||||
None=>false,
|
||||
},
|
||||
behaviour:match &captures[2]{
|
||||
"SpawnAt"=>crate::model::StageElementBehaviour::SpawnAt,
|
||||
"Trigger"=>crate::model::StageElementBehaviour::Trigger,
|
||||
"Teleport"=>crate::model::StageElementBehaviour::Teleport,
|
||||
"Platform"=>crate::model::StageElementBehaviour::Platform,
|
||||
_=>panic!("regex[2] messed up bad"),
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
//need some way to skip this
|
||||
if velocity!=glam::Vec3::ZERO{
|
||||
general.booster=Some(crate::model::GameMechanicBooster{velocity});
|
||||
}
|
||||
match can_collide{
|
||||
true=>{
|
||||
match name{
|
||||
//"Bounce"=>(),
|
||||
"Surf"=>contacting.surf=Some(crate::model::ContactingSurf{}),
|
||||
"Ladder"=>contacting.ladder=Some(crate::model::ContactingLadder{sticky:true}),
|
||||
other=>{
|
||||
//REGEX!!!!
|
||||
//Jump#
|
||||
//WormholeIn#
|
||||
}
|
||||
}
|
||||
return crate::model::CollisionAttributes::Contact{contacting,general};
|
||||
},
|
||||
false=>return crate::model::CollisionAttributes::Decoration,
|
||||
}
|
||||
}
|
||||
|
||||
struct RobloxAssetId(u64);
|
||||
struct RobloxAssetIdParseErr;
|
||||
impl std::str::FromStr for RobloxAssetId {
|
||||
type Err=RobloxAssetIdParseErr;
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err>{
|
||||
let regman=lazy_regex::regex!(r"(\d+)$");
|
||||
let regman=regex::Regex::new(r"(\d+)$").unwrap();
|
||||
if let Some(captures) = regman.captures(s) {
|
||||
if captures.len()==2{//captures[0] is all captures concatenated, and then each individual capture
|
||||
if let Ok(id) = captures[0].parse::<u64>() {
|
||||
@ -167,12 +111,10 @@ enum RobloxBasePartDescription{
|
||||
Wedge(RobloxWedgeDescription),
|
||||
CornerWedge(RobloxCornerWedgeDescription),
|
||||
}
|
||||
pub fn generate_indexed_models(dom:rbx_dom_weak::WeakDom) -> crate::model::IndexedModelInstances{
|
||||
pub fn generate_indexed_models_roblox(dom:rbx_dom_weak::WeakDom) -> Result<(IndexedModelInstances,glam::Vec3), Box<dyn std::error::Error>>{
|
||||
//IndexedModelInstances includes textures
|
||||
let mut spawn_point=glam::Vec3::ZERO;
|
||||
|
||||
let mut stages=Vec::new();
|
||||
|
||||
let mut indexed_models=Vec::new();
|
||||
let mut model_id_from_description=std::collections::HashMap::<RobloxBasePartDescription,usize>::new();
|
||||
|
||||
@ -187,17 +129,13 @@ pub fn generate_indexed_models(dom:rbx_dom_weak::WeakDom) -> crate::model::Index
|
||||
if let (
|
||||
Some(rbx_dom_weak::types::Variant::CFrame(cf)),
|
||||
Some(rbx_dom_weak::types::Variant::Vector3(size)),
|
||||
Some(rbx_dom_weak::types::Variant::Vector3(velocity)),
|
||||
Some(rbx_dom_weak::types::Variant::Float32(transparency)),
|
||||
Some(rbx_dom_weak::types::Variant::Color3uint8(color3)),
|
||||
Some(rbx_dom_weak::types::Variant::Bool(can_collide)),
|
||||
) = (
|
||||
object.properties.get("CFrame"),
|
||||
object.properties.get("Size"),
|
||||
object.properties.get("Velocity"),
|
||||
object.properties.get("Transparency"),
|
||||
object.properties.get("Color"),
|
||||
object.properties.get("CanCollide"),
|
||||
)
|
||||
{
|
||||
let model_transform=glam::Affine3A::from_translation(
|
||||
@ -214,11 +152,13 @@ pub fn generate_indexed_models(dom:rbx_dom_weak::WeakDom) -> crate::model::Index
|
||||
glam::Vec3::new(size.x,size.y,size.z)/2.0
|
||||
);
|
||||
if object.name=="MapStart"{
|
||||
spawn_point=model_transform.transform_point3(-glam::Vec3::Y)+glam::vec3(0.0,2.5+0.1,0.0);
|
||||
spawn_point=model_transform.transform_point3(glam::Vec3::Y)+glam::vec3(0.0,2.5,0.0);
|
||||
println!("Found MapStart{:?}",spawn_point);
|
||||
}
|
||||
if *transparency==1.0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
//TODO: also detect "CylinderMesh" etc here
|
||||
let shape=match &object.class[..]{
|
||||
"Part"=>{
|
||||
if let Some(rbx_dom_weak::types::Variant::Enum(shape))=object.properties.get("Shape"){
|
||||
@ -228,10 +168,14 @@ pub fn generate_indexed_models(dom:rbx_dom_weak::WeakDom) -> crate::model::Index
|
||||
2=>primitives::Primitives::Cylinder,
|
||||
3=>primitives::Primitives::Wedge,
|
||||
4=>primitives::Primitives::CornerWedge,
|
||||
_=>panic!("Funky roblox PartType={};",shape.to_u32()),
|
||||
_=>{
|
||||
println!("Funky roblox PartType={}; defaulting to cube",shape.to_u32());
|
||||
primitives::Primitives::Cube
|
||||
},
|
||||
}
|
||||
}else{
|
||||
panic!("Part has no Shape!");
|
||||
println!("Part has no Shape! defaulting to cube");
|
||||
primitives::Primitives::Cube
|
||||
}
|
||||
},
|
||||
"WedgePart"=>primitives::Primitives::Wedge,
|
||||
@ -242,6 +186,38 @@ pub fn generate_indexed_models(dom:rbx_dom_weak::WeakDom) -> crate::model::Index
|
||||
}
|
||||
};
|
||||
|
||||
//TODO: also detect "CylinderMesh" etc here
|
||||
let mut face_map=std::collections::HashMap::new();
|
||||
match shape{
|
||||
primitives::Primitives::Cube => {
|
||||
face_map.insert(0,0);//Right
|
||||
face_map.insert(1,1);//Top
|
||||
face_map.insert(2,2);//Back
|
||||
face_map.insert(3,3);//Left
|
||||
face_map.insert(4,4);//Bottom
|
||||
face_map.insert(5,5);//Front
|
||||
},
|
||||
primitives::Primitives::Wedge => {
|
||||
face_map.insert(0,0);//Right
|
||||
face_map.insert(1,1);//Top -> TopFront (some surf maps put surf textures on the Top face)
|
||||
face_map.insert(2,1);//Front -> TopFront
|
||||
face_map.insert(3,2);//Back
|
||||
face_map.insert(4,3);//Left
|
||||
face_map.insert(5,4);//Bottom
|
||||
},
|
||||
primitives::Primitives::CornerWedge => {
|
||||
//Right -> None
|
||||
face_map.insert(1,0);//Top
|
||||
//Back -> None
|
||||
face_map.insert(3,1);//Right
|
||||
face_map.insert(4,2);//Bottom
|
||||
face_map.insert(5,3);//Front
|
||||
},
|
||||
//do not support textured spheres/cylinders imported from roblox
|
||||
//this can be added later, there are some maps that use it
|
||||
primitives::Primitives::Sphere
|
||||
|primitives::Primitives::Cylinder => (),
|
||||
}
|
||||
//use the biggest one and cut it down later...
|
||||
let mut part_texture_description:RobloxPartDescription=[None,None,None,None,None,None];
|
||||
temp_objects.clear();
|
||||
@ -269,7 +245,7 @@ pub fn generate_indexed_models(dom:rbx_dom_weak::WeakDom) -> crate::model::Index
|
||||
texture_id
|
||||
};
|
||||
let normal_id=normalid.to_u32();
|
||||
if normal_id<6{
|
||||
if let Some(&face)=face_map.get(&normal_id){
|
||||
let mut roblox_texture_transform=RobloxTextureTransform::default();
|
||||
let mut roblox_texture_color=glam::Vec4::ONE;
|
||||
if decal.class=="Texture"{
|
||||
@ -293,7 +269,7 @@ pub fn generate_indexed_models(dom:rbx_dom_weak::WeakDom) -> crate::model::Index
|
||||
3=>(size.z,size.y),//left
|
||||
4=>(size.x,size.z),//bottom
|
||||
5=>(size.x,size.y),//front
|
||||
_=>panic!("unreachable"),
|
||||
_=>(1.,1.),
|
||||
};
|
||||
roblox_texture_transform=RobloxTextureTransform{
|
||||
offset_u:*ox/(*sx),offset_v:*oy/(*sy),
|
||||
@ -302,7 +278,7 @@ pub fn generate_indexed_models(dom:rbx_dom_weak::WeakDom) -> crate::model::Index
|
||||
roblox_texture_color=glam::vec4(decal_color3.r,decal_color3.g,decal_color3.b,1.0-*decal_transparency);
|
||||
}
|
||||
}
|
||||
part_texture_description[normal_id as usize]=Some(RobloxFaceTextureDescription{
|
||||
part_texture_description[face]=Some(RobloxFaceTextureDescription{
|
||||
texture:texture_id,
|
||||
color:roblox_texture_color,
|
||||
transform:roblox_texture_transform,
|
||||
@ -320,11 +296,11 @@ pub fn generate_indexed_models(dom:rbx_dom_weak::WeakDom) -> crate::model::Index
|
||||
primitives::Primitives::Sphere=>RobloxBasePartDescription::Sphere,
|
||||
primitives::Primitives::Cube=>RobloxBasePartDescription::Part([f0,f1,f2,f3,f4,f5]),
|
||||
primitives::Primitives::Cylinder=>RobloxBasePartDescription::Cylinder,
|
||||
//use front face texture first and use top face texture as a fallback
|
||||
primitives::Primitives::Wedge=>RobloxBasePartDescription::Wedge([f0,if f2.is_some(){f2}else{f1},f3,f4,f5]),
|
||||
primitives::Primitives::CornerWedge=>RobloxBasePartDescription::CornerWedge([f1,f3,f4,f5]),
|
||||
//HAHAHA
|
||||
primitives::Primitives::Wedge=>RobloxBasePartDescription::Wedge([f0,f1,f2,f3,f4]),
|
||||
primitives::Primitives::CornerWedge=>RobloxBasePartDescription::CornerWedge([f0,f1,f2,f3]),
|
||||
};
|
||||
//make new model if unit cube has not been created before
|
||||
//make new model if unit cube has not been crated before
|
||||
let model_id=if let Some(&model_id)=model_id_from_description.get(&basepart_texture_description){
|
||||
//push to existing texture model
|
||||
model_id
|
||||
@ -394,18 +370,15 @@ pub fn generate_indexed_models(dom:rbx_dom_weak::WeakDom) -> crate::model::Index
|
||||
});
|
||||
model_id
|
||||
};
|
||||
indexed_models[model_id].instances.push(crate::model::ModelInstance {
|
||||
indexed_models[model_id].instances.push(ModelInstance {
|
||||
transform:model_transform,
|
||||
color:glam::vec4(color3.r as f32/255f32, color3.g as f32/255f32, color3.b as f32/255f32, 1.0-*transparency),
|
||||
attributes:get_attributes(&object.name,*can_collide,glam::vec3(velocity.x,velocity.y,velocity.z)),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
crate::model::IndexedModelInstances{
|
||||
Ok((IndexedModelInstances{
|
||||
textures:asset_id_from_texture_id.iter().map(|t|t.to_string()).collect(),
|
||||
models:indexed_models,
|
||||
spawn_point,
|
||||
stages,
|
||||
}
|
||||
},spawn_point))
|
||||
}
|
||||
|
299
src/main.rs
299
src/main.rs
@ -46,7 +46,6 @@ pub struct GraphicsPipelines {
|
||||
pub struct GraphicsData {
|
||||
start_time: std::time::Instant,
|
||||
screen_size: (u32, u32),
|
||||
manual_mouse_lock:bool,
|
||||
physics: body::PhysicsState,
|
||||
pipelines: GraphicsPipelines,
|
||||
bind_groups: GraphicsBindGroups,
|
||||
@ -88,31 +87,28 @@ impl GraphicsData {
|
||||
for model in &indexed_models.models{
|
||||
//make aabb and run vertices to get realistic bounds
|
||||
for model_instance in &model.instances{
|
||||
if let Some(model_physics)=body::ModelPhysics::from_model(model,model_instance){
|
||||
self.physics.models.push(model_physics);
|
||||
}
|
||||
self.physics.models.push(body::ModelPhysics::from_model(&model,model_instance.transform));
|
||||
}
|
||||
}
|
||||
println!("Physics Objects: {}",self.physics.models.len());
|
||||
}
|
||||
fn generate_model_graphics(&mut self,device:&wgpu::Device,queue:&wgpu::Queue,indexed_models:model::IndexedModelInstances){
|
||||
fn generate_model_graphics(&mut self,device:&wgpu::Device,queue:&wgpu::Queue,mut indexed_models:model::IndexedModelInstances){
|
||||
//generate texture view per texture
|
||||
|
||||
//idk how to do this gooder lol
|
||||
let mut double_map=std::collections::HashMap::<u32,u32>::new();
|
||||
let mut texture_loading_threads=Vec::new();
|
||||
let num_textures=indexed_models.textures.len();
|
||||
for (i,texture_id) in indexed_models.textures.into_iter().enumerate(){
|
||||
if let Ok(mut file) = std::fs::File::open(std::path::Path::new(&format!("textures/{}.dds",texture_id))){
|
||||
for (i,t) in indexed_models.textures.iter().enumerate(){
|
||||
if let Ok(mut file) = std::fs::File::open(std::path::Path::new(&format!("textures/{}.dds",t))){
|
||||
double_map.insert(i as u32, texture_loading_threads.len() as u32);
|
||||
texture_loading_threads.push((texture_id,std::thread::spawn(move ||{
|
||||
ddsfile::Dds::read(&mut file).unwrap()
|
||||
})));
|
||||
texture_loading_threads.push(std::thread::spawn(move ||{
|
||||
(i,ddsfile::Dds::read(&mut file).unwrap())
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
let texture_views:Vec<wgpu::TextureView>=texture_loading_threads.into_iter().map(|(texture_id,thread)|{
|
||||
let image=thread.join().unwrap();
|
||||
let texture_views:Vec<wgpu::TextureView>=texture_loading_threads.into_iter().map(|t|{
|
||||
let (i,image)=t.join().unwrap();
|
||||
|
||||
let (mut width,mut height)=(image.get_width(),image.get_height());
|
||||
|
||||
@ -148,39 +144,35 @@ impl GraphicsData {
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format,
|
||||
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
|
||||
label: Some(format!("Texture{}",texture_id).as_str()),
|
||||
label: Some(format!("Texture{}",i).as_str()),
|
||||
view_formats: &[],
|
||||
},
|
||||
&image.data,
|
||||
);
|
||||
texture.create_view(&wgpu::TextureViewDescriptor {
|
||||
label: Some(format!("Texture{} View",texture_id).as_str()),
|
||||
label: Some(format!("Texture{} View",i).as_str()),
|
||||
dimension: Some(wgpu::TextureViewDimension::D2),
|
||||
..wgpu::TextureViewDescriptor::default()
|
||||
})
|
||||
}).collect();
|
||||
|
||||
let indexed_models_len=indexed_models.models.len();
|
||||
//split groups with different textures into separate models
|
||||
//the models received here are supposed to be tightly packed, i.e. no code needs to check if two models are using the same groups.
|
||||
let indexed_models_len=indexed_models.models.len();
|
||||
let mut unique_texture_models=Vec::with_capacity(indexed_models_len);
|
||||
for mut model in indexed_models.models.into_iter(){
|
||||
let mut unique_texture_models=Vec::with_capacity(indexed_models.models.len());
|
||||
for mut model in indexed_models.models.drain(..){
|
||||
//convert ModelInstance into ModelGraphicsInstance
|
||||
let instances:Vec<ModelGraphicsInstance>=model.instances.into_iter().filter_map(|instance|{
|
||||
if instance.color.w==0.0{
|
||||
None
|
||||
}else{
|
||||
Some(ModelGraphicsInstance{
|
||||
transform: glam::Mat4::from(instance.transform),
|
||||
normal_transform: glam::Mat4::from(instance.transform.inverse()).transpose(),
|
||||
color: instance.color,
|
||||
})
|
||||
let instances:Vec<ModelGraphicsInstance>=model.instances.iter().map(|instance|{
|
||||
ModelGraphicsInstance{
|
||||
transform: glam::Mat4::from(instance.transform),
|
||||
normal_transform: glam::Mat4::from(instance.transform.inverse()).transpose(),
|
||||
color: instance.color,
|
||||
}
|
||||
}).collect();
|
||||
//check each group, if it's using a new texture then make a new clone of the model
|
||||
let id=unique_texture_models.len();
|
||||
let mut unique_textures=Vec::new();
|
||||
for group in model.groups.into_iter(){
|
||||
for group in model.groups.drain(..){
|
||||
//ignore zero coppy optimization for now
|
||||
let texture_index=if let Some(texture_index)=unique_textures.iter().position(|&texture|texture==group.texture){
|
||||
texture_index
|
||||
@ -207,7 +199,7 @@ impl GraphicsData {
|
||||
}
|
||||
//de-index models
|
||||
let mut models=Vec::with_capacity(unique_texture_models.len());
|
||||
for model in unique_texture_models.into_iter(){
|
||||
for model in unique_texture_models.drain(..){
|
||||
let mut vertices = Vec::new();
|
||||
let mut index_from_vertex = std::collections::HashMap::new();//::<IndexedVertex,usize>
|
||||
let mut entities = Vec::new();
|
||||
@ -244,13 +236,13 @@ impl GraphicsData {
|
||||
texture:model.texture,
|
||||
});
|
||||
}
|
||||
//.into_iter() the modeldata vec so entities can be /moved/ to models.entities
|
||||
//drain the modeldata vec so entities can be /moved/ to models.entities
|
||||
let mut model_count=0;
|
||||
let mut instance_count=0;
|
||||
let uniform_buffer_binding_size=<GraphicsData as framework::Example>::required_limits().max_uniform_buffer_binding_size as usize;
|
||||
let chunk_size=uniform_buffer_binding_size/MODEL_BUFFER_SIZE_BYTES;
|
||||
self.models.reserve(models.len());
|
||||
for model in models.into_iter() {
|
||||
for model in models.drain(..) {
|
||||
instance_count+=model.instances.len();
|
||||
for instances_chunk in model.instances.rchunks(chunk_size){
|
||||
model_count+=1;
|
||||
@ -312,7 +304,7 @@ impl GraphicsData {
|
||||
});
|
||||
}
|
||||
}
|
||||
println!("Texture References={}",num_textures);
|
||||
println!("Texture References={}",indexed_models.textures.len());
|
||||
println!("Textures Loaded={}",texture_views.len());
|
||||
println!("Indexed Models={}",indexed_models_len);
|
||||
println!("Graphics Objects: {}",self.models.len());
|
||||
@ -377,46 +369,33 @@ impl framework::Example for GraphicsData {
|
||||
indexed_models[0].instances.push(ModelInstance{
|
||||
transform:glam::Affine3A::from_translation(glam::vec3(10.,0.,-10.)),
|
||||
color:glam::Vec4::ONE,
|
||||
attributes:model::CollisionAttributes::contact(),
|
||||
});
|
||||
//quad monkeys
|
||||
indexed_models[1].instances.push(ModelInstance{
|
||||
transform:glam::Affine3A::from_translation(glam::vec3(10.,5.,10.)),
|
||||
color:glam::Vec4::ONE,
|
||||
attributes:model::CollisionAttributes::contact(),
|
||||
});
|
||||
indexed_models[1].instances.push(ModelInstance{
|
||||
transform:glam::Affine3A::from_translation(glam::vec3(20.,5.,10.)),
|
||||
color:glam::vec4(1.0,0.0,0.0,1.0),
|
||||
attributes:model::CollisionAttributes::contact(),
|
||||
});
|
||||
indexed_models[1].instances.push(ModelInstance{
|
||||
transform:glam::Affine3A::from_translation(glam::vec3(10.,5.,20.)),
|
||||
color:glam::vec4(0.0,1.0,0.0,1.0),
|
||||
attributes:model::CollisionAttributes::contact(),
|
||||
});
|
||||
indexed_models[1].instances.push(ModelInstance{
|
||||
transform:glam::Affine3A::from_translation(glam::vec3(20.,5.,20.)),
|
||||
color:glam::vec4(0.0,0.0,1.0,1.0),
|
||||
attributes:model::CollisionAttributes::contact(),
|
||||
});
|
||||
//decorative monkey
|
||||
indexed_models[1].instances.push(ModelInstance{
|
||||
transform:glam::Affine3A::from_translation(glam::vec3(15.,10.,15.)),
|
||||
color:glam::vec4(0.5,0.5,0.5,0.5),
|
||||
attributes:model::CollisionAttributes::Decoration,
|
||||
});
|
||||
//teapot
|
||||
indexed_models[2].instances.push(ModelInstance{
|
||||
transform:glam::Affine3A::from_scale_rotation_translation(glam::vec3(0.5, 1.0, 0.2),glam::quat(-0.22248298016985793,-0.839457167990537,-0.05603504040830783,-0.49261857546227916),glam::vec3(-10.,7.,10.)),
|
||||
color:glam::Vec4::ONE,
|
||||
attributes:model::CollisionAttributes::contact(),
|
||||
});
|
||||
//ground
|
||||
indexed_models[3].instances.push(ModelInstance{
|
||||
transform:glam::Affine3A::from_translation(glam::vec3(0.,0.,0.))*glam::Affine3A::from_scale(glam::vec3(160.0, 1.0, 160.0)),
|
||||
color:glam::Vec4::ONE,
|
||||
attributes:model::CollisionAttributes::contact(),
|
||||
});
|
||||
|
||||
let camera_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
@ -518,17 +497,22 @@ impl framework::Example for GraphicsData {
|
||||
spawn_point:glam::vec3(0.0,50.0,0.0),
|
||||
body: body::Body::with_pva(glam::vec3(0.0,50.0,0.0),glam::vec3(0.0,0.0,0.0),glam::vec3(0.0,-100.0,0.0)),
|
||||
time: 0,
|
||||
style:body::StyleModifiers::default(),
|
||||
tick: 0,
|
||||
strafe_tick_num: 100,//100t
|
||||
strafe_tick_den: 1_000_000_000,
|
||||
gravity: glam::vec3(0.0,-100.0,0.0),
|
||||
friction: 1.2,
|
||||
walk_accel: 90.0,
|
||||
mv: 2.7,
|
||||
grounded: false,
|
||||
walkspeed: 18.0,
|
||||
contacts: std::collections::HashSet::new(),
|
||||
models: Vec::new(),
|
||||
walk: body::WalkState::new(),
|
||||
hitbox_halfsize: glam::vec3(1.0,2.5,1.0),
|
||||
camera: body::Camera::from_offset(glam::vec3(0.0,4.5-2.5,0.0),(config.width as f32)/(config.height as f32)),
|
||||
mouse_interpolation: body::MouseInterpolationState::new(),
|
||||
controls: 0,
|
||||
world:body::WorldState{},
|
||||
game:body::GameMechanicsState::default(),
|
||||
stages:Vec::new(),
|
||||
};
|
||||
|
||||
//load textures
|
||||
@ -759,7 +743,6 @@ impl framework::Example for GraphicsData {
|
||||
let depth_view = Self::create_depth_texture(config, device);
|
||||
|
||||
let mut graphics=GraphicsData {
|
||||
manual_mouse_lock:false,
|
||||
start_time: Instant::now(),
|
||||
screen_size: (config.width,config.height),
|
||||
physics,
|
||||
@ -783,98 +766,82 @@ impl framework::Example for GraphicsData {
|
||||
let indexed_model_instances=model::IndexedModelInstances{
|
||||
textures:Vec::new(),
|
||||
models:indexed_models,
|
||||
spawn_point:glam::Vec3::Y*50.0,
|
||||
stages:Vec::new(),
|
||||
};
|
||||
graphics.generate_model_physics(&indexed_model_instances);
|
||||
graphics.generate_model_graphics(&device,&queue,indexed_model_instances);
|
||||
|
||||
let args:Vec<String>=std::env::args().collect();
|
||||
if args.len()==2{
|
||||
graphics.load_file(std::path::PathBuf::from(&args[1]), device, queue);
|
||||
}
|
||||
|
||||
return graphics;
|
||||
}
|
||||
|
||||
fn load_file(&mut self,path: std::path::PathBuf, device: &wgpu::Device, queue: &wgpu::Queue){
|
||||
println!("Loading file: {:?}", &path);
|
||||
//oh boy! let's load the map!
|
||||
if let Ok(file)=std::fs::File::open(path){
|
||||
let mut input = std::io::BufReader::new(file);
|
||||
let mut first_8=[0u8;8];
|
||||
//.rbxm roblox binary = "<roblox!"
|
||||
//.rbxmx roblox xml = "<roblox "
|
||||
//.bsp = "VBSP"
|
||||
//.vmf =
|
||||
//.snf = "SNMF"
|
||||
//.snf = "SNBF"
|
||||
if let (Ok(()),Ok(()))=(std::io::Read::read_exact(&mut input, &mut first_8),std::io::Seek::rewind(&mut input)){
|
||||
//
|
||||
if let Some(indexed_model_instances)={
|
||||
match &first_8[0..4]{
|
||||
b"<rob"=>{
|
||||
match match &first_8[4..8]{
|
||||
b"lox!"=>rbx_binary::from_reader(input).map_err(|e|format!("{:?}",e)),
|
||||
b"lox "=>rbx_xml::from_reader(input,rbx_xml::DecodeOptions::default()).map_err(|e|format!("{:?}",e)),
|
||||
other=>Err(format!("Unknown Roblox file type {:?}",other)),
|
||||
}{
|
||||
Ok(dom)=>Some(load_roblox::generate_indexed_models(dom)),
|
||||
Err(e)=>{
|
||||
println!("Error loading roblox file:{:?}",e);
|
||||
None
|
||||
},
|
||||
}
|
||||
},
|
||||
//b"VBSP"=>Some(load_bsp::generate_indexed_models(input)),
|
||||
//b"SNFM"=>Some(sniffer::generate_indexed_models(input)),
|
||||
//b"SNFB"=>Some(sniffer::load_bot(input)),
|
||||
other=>{
|
||||
println!("loser file {:?}",other);
|
||||
None
|
||||
},
|
||||
}
|
||||
}{
|
||||
let spawn_point=indexed_model_instances.spawn_point;
|
||||
//if generate_indexed_models succeeds, clear the previous ones
|
||||
self.models.clear();
|
||||
self.physics.models.clear();
|
||||
self.generate_model_physics(&indexed_model_instances);
|
||||
self.generate_model_graphics(device,queue,indexed_model_instances);
|
||||
//manual reset
|
||||
let time=self.physics.time;
|
||||
instruction::InstructionConsumer::process_instruction(&mut self.physics, instruction::TimedInstruction{
|
||||
time,
|
||||
instruction: body::PhysicsInstruction::SetSpawnPosition(spawn_point),
|
||||
});
|
||||
instruction::InstructionConsumer::process_instruction(&mut self.physics, instruction::TimedInstruction{
|
||||
time,
|
||||
instruction: body::PhysicsInstruction::Input(body::InputInstruction::Reset),
|
||||
});
|
||||
}else{
|
||||
println!("No modeldatas were generated");
|
||||
}
|
||||
}else{
|
||||
println!("Failed to read first 8 bytes and seek back to beginning of file.");
|
||||
}
|
||||
}else{
|
||||
println!("Could not open file");
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::single_match)]
|
||||
fn update(&mut self, window: &winit::window::Window, device: &wgpu::Device, queue: &wgpu::Queue, event: winit::event::WindowEvent) {
|
||||
fn update(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, event: winit::event::WindowEvent) {
|
||||
//nothing atm
|
||||
match event {
|
||||
winit::event::WindowEvent::DroppedFile(path) => self.load_file(path,device,queue),
|
||||
winit::event::WindowEvent::Focused(state)=>{
|
||||
//pause unpause
|
||||
//recalculate pressed keys on focus
|
||||
}
|
||||
winit::event::WindowEvent::DroppedFile(path) => {
|
||||
println!("opening file: {:?}", &path);
|
||||
//oh boy! let's load the map!
|
||||
if let Ok(file)=std::fs::File::open(path){
|
||||
let mut input = std::io::BufReader::new(file);
|
||||
let mut first_8=[0u8;8];
|
||||
//.rbxm roblox binary = "<roblox!"
|
||||
//.rbxmx roblox xml = "<roblox "
|
||||
//.bsp = "VBSP"
|
||||
//.vmf =
|
||||
//.snf = "SNMF"
|
||||
//.snf = "SNBF"
|
||||
if let (Ok(()),Ok(()))=(std::io::Read::read_exact(&mut input, &mut first_8),std::io::Seek::rewind(&mut input)){
|
||||
//
|
||||
if let Some(Ok((indexed_model_instances,spawn_point)))={
|
||||
match &first_8[0..4]{
|
||||
b"<rob"=>{
|
||||
match match &first_8[4..8]{
|
||||
b"lox!"=>rbx_binary::from_reader(input).map_err(|e|format!("{:?}",e)),
|
||||
b"lox "=>rbx_xml::from_reader(input,rbx_xml::DecodeOptions::default()).map_err(|e|format!("{:?}",e)),
|
||||
other=>Err(format!("Unknown Roblox file type {:?}",other)),
|
||||
}{
|
||||
Ok(dom)=>Some(load_roblox::generate_indexed_models_roblox(dom)),
|
||||
Err(e)=>{
|
||||
println!("Error loading roblox file:{:?}",e);
|
||||
None
|
||||
},
|
||||
}
|
||||
},
|
||||
//b"VBSP"=>load_valve::generate_indexed_models_valve(input),
|
||||
//b"SNFM"=>sniffer::generate_indexed_models(input),
|
||||
//b"SNFB"=>sniffer::load_bot(input),
|
||||
_=>None,
|
||||
}
|
||||
}{
|
||||
//if generate_indexed_models succeeds, clear the previous ones
|
||||
self.models.clear();
|
||||
self.physics.models.clear();
|
||||
self.generate_model_physics(&indexed_model_instances);
|
||||
self.generate_model_graphics(device,queue,indexed_model_instances);
|
||||
//manual reset
|
||||
let time=self.physics.time;
|
||||
instruction::InstructionConsumer::process_instruction(&mut self.physics, instruction::TimedInstruction{
|
||||
time,
|
||||
instruction: body::PhysicsInstruction::SetSpawnPosition(spawn_point),
|
||||
});
|
||||
instruction::InstructionConsumer::process_instruction(&mut self.physics, instruction::TimedInstruction{
|
||||
time,
|
||||
instruction: body::PhysicsInstruction::Input(body::InputInstruction::Reset),
|
||||
});
|
||||
}else{
|
||||
println!("No modeldatas were generated");
|
||||
}
|
||||
}else{
|
||||
println!("Failed to read first 8 bytes and seek back to beginning of file.");
|
||||
}
|
||||
}else{
|
||||
println!("Could not open file");
|
||||
}
|
||||
},
|
||||
_=>(),
|
||||
}
|
||||
}
|
||||
|
||||
fn device_event(&mut self, window: &winit::window::Window, event: winit::event::DeviceEvent) {
|
||||
fn device_event(&mut self, event: winit::event::DeviceEvent) {
|
||||
//there's no way this is the best way get a timestamp.
|
||||
let time=self.start_time.elapsed().as_nanos() as i64;
|
||||
match event {
|
||||
@ -888,58 +855,18 @@ impl framework::Example for GraphicsData {
|
||||
winit::event::ElementState::Released => false,
|
||||
};
|
||||
if let Some(input_instruction)=match keycode {
|
||||
17=>Some(InputInstruction::MoveForward(s)),//W
|
||||
30=>Some(InputInstruction::MoveLeft(s)),//A
|
||||
31=>Some(InputInstruction::MoveBack(s)),//S
|
||||
32=>Some(InputInstruction::MoveRight(s)),//D
|
||||
18=>Some(InputInstruction::MoveUp(s)),//E
|
||||
16=>Some(InputInstruction::MoveDown(s)),//Q
|
||||
57=>Some(InputInstruction::Jump(s)),//Space
|
||||
44=>Some(InputInstruction::Zoom(s)),//Z
|
||||
19=>if s{Some(InputInstruction::Reset)}else{None},//R
|
||||
01=>{//Esc
|
||||
if s{
|
||||
self.manual_mouse_lock=false;
|
||||
match window.set_cursor_grab(winit::window::CursorGrabMode::None){
|
||||
Ok(())=>(),
|
||||
Err(e)=>println!("Could not release cursor: {:?}",e),
|
||||
}
|
||||
window.set_cursor_visible(true);
|
||||
}
|
||||
None
|
||||
},
|
||||
15=>{//Tab
|
||||
if s{
|
||||
self.manual_mouse_lock=false;
|
||||
match window.set_cursor_position(winit::dpi::PhysicalPosition::new(self.screen_size.0 as f32/2.0, self.screen_size.1 as f32/2.0)){
|
||||
Ok(())=>(),
|
||||
Err(e)=>println!("Could not set cursor position: {:?}",e),
|
||||
}
|
||||
match window.set_cursor_grab(winit::window::CursorGrabMode::None){
|
||||
Ok(())=>(),
|
||||
Err(e)=>println!("Could not release cursor: {:?}",e),
|
||||
}
|
||||
}else{
|
||||
//if cursor is outside window don't lock but apparently there's no get pos function
|
||||
//let pos=window.get_cursor_pos();
|
||||
match window.set_cursor_grab(winit::window::CursorGrabMode::Locked){
|
||||
Ok(())=>(),
|
||||
Err(_)=>{
|
||||
match window.set_cursor_grab(winit::window::CursorGrabMode::Confined){
|
||||
Ok(())=>(),
|
||||
Err(e)=>{
|
||||
self.manual_mouse_lock=true;
|
||||
println!("Could not confine cursor: {:?}",e)
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
window.set_cursor_visible(s);
|
||||
None
|
||||
},
|
||||
_ => {println!("scancode {}",keycode);None},
|
||||
}{
|
||||
17 => Some(InputInstruction::MoveForward(s)),//W
|
||||
30 => Some(InputInstruction::MoveLeft(s)),//A
|
||||
31 => Some(InputInstruction::MoveBack(s)),//S
|
||||
32 => Some(InputInstruction::MoveRight(s)),//D
|
||||
18 => Some(InputInstruction::MoveUp(s)),//E
|
||||
16 => Some(InputInstruction::MoveDown(s)),//Q
|
||||
57 => Some(InputInstruction::Jump(s)),//Space
|
||||
44 => Some(InputInstruction::Zoom(s)),//Z
|
||||
19 => if s{Some(InputInstruction::Reset)}else{None},//R
|
||||
_ => None,
|
||||
}
|
||||
{
|
||||
self.physics.run(time);
|
||||
self.physics.process_instruction(TimedInstruction{
|
||||
time,
|
||||
@ -950,12 +877,6 @@ impl framework::Example for GraphicsData {
|
||||
winit::event::DeviceEvent::MouseMotion {
|
||||
delta,//these (f64,f64) are integers on my machine
|
||||
} => {
|
||||
if self.manual_mouse_lock{
|
||||
match window.set_cursor_position(winit::dpi::PhysicalPosition::new(self.screen_size.0 as f32/2.0, self.screen_size.1 as f32/2.0)){
|
||||
Ok(())=>(),
|
||||
Err(e)=>println!("Could not set cursor position: {:?}",e),
|
||||
}
|
||||
}
|
||||
//do not step the physics because the mouse polling rate is higher than the physics can run.
|
||||
//essentially the previous input will be overwritten until a true step runs
|
||||
//which is fine because they run all the time.
|
||||
@ -967,8 +888,8 @@ impl framework::Example for GraphicsData {
|
||||
winit::event::DeviceEvent::MouseWheel {
|
||||
delta,
|
||||
} => {
|
||||
println!("mousewheel {:?}",delta);
|
||||
if false{//self.physics.style.use_scroll{
|
||||
println!("mousewheel{:?}",delta);
|
||||
if true{//self.physics.use_scroll
|
||||
self.physics.run(time);
|
||||
self.physics.process_instruction(TimedInstruction{
|
||||
time,
|
||||
|
121
src/model.rs
121
src/model.rs
@ -56,130 +56,13 @@ pub struct ModelGraphicsInstance{
|
||||
pub color:glam::Vec4,
|
||||
}
|
||||
pub struct ModelInstance{
|
||||
//pub id:u64,//this does not actually help with map fixes resimulating bots, they must always be resimulated
|
||||
pub transform:glam::Affine3A,
|
||||
pub color:glam::Vec4,//transparency is in here
|
||||
pub attributes:CollisionAttributes,
|
||||
pub color:glam::Vec4,
|
||||
}
|
||||
pub struct IndexedModelInstances{
|
||||
pub textures:Vec<String>,//RenderPattern
|
||||
pub models:Vec<IndexedModel>,
|
||||
//may make this into an object later.
|
||||
pub stages:Vec<StageDescription>,
|
||||
pub spawn_point:glam::Vec3,
|
||||
}
|
||||
//stage description referencing flattened ids is spooky, but the map loading is meant to be deterministic.
|
||||
pub struct StageDescription{
|
||||
pub start:u32,//start=model_id
|
||||
pub spawns:Vec<u32>,//spawns[spawn_id]=model_id
|
||||
pub ordered_checkpoints:Vec<u32>,//ordered_checkpoints[checkpoint_id]=model_id
|
||||
pub unordered_checkpoints:Vec<u32>,//unordered_checkpoints[checkpoint_id]=model_id
|
||||
}
|
||||
|
||||
//you have this effect while in contact
|
||||
#[derive(Clone)]
|
||||
pub struct ContactingSurf{}
|
||||
#[derive(Clone)]
|
||||
pub struct ContactingLadder{
|
||||
pub sticky:bool
|
||||
}
|
||||
//you have this effect while intersecting
|
||||
#[derive(Clone)]
|
||||
pub struct IntersectingWater{
|
||||
pub viscosity:i64,
|
||||
pub density:i64,
|
||||
pub current:glam::Vec3,
|
||||
}
|
||||
#[derive(Clone)]
|
||||
pub struct IntersectingAccelerator{
|
||||
pub acceleration:glam::Vec3
|
||||
}
|
||||
//All models can be given these attributes
|
||||
#[derive(Clone)]
|
||||
pub struct GameMechanicJumpLimit{
|
||||
pub count:u32,
|
||||
}
|
||||
#[derive(Clone)]
|
||||
pub struct GameMechanicBooster{
|
||||
pub velocity:glam::Vec3,
|
||||
}
|
||||
#[derive(Clone)]
|
||||
pub enum ZoneBehaviour{
|
||||
//Start is indexed
|
||||
//Checkpoints are indexed
|
||||
Finish,
|
||||
Anitcheat,
|
||||
}
|
||||
#[derive(Clone)]
|
||||
pub struct GameMechanicZone{
|
||||
pub mode_id:u32,
|
||||
pub behaviour:ZoneBehaviour,
|
||||
}
|
||||
// enum TrapCondition{
|
||||
// FasterThan(i64),
|
||||
// SlowerThan(i64),
|
||||
// InRange(i64,i64),
|
||||
// OutsideRange(i64,i64),
|
||||
// }
|
||||
#[derive(Clone)]
|
||||
pub enum StageElementBehaviour{
|
||||
//Spawn,//The behaviour of stepping on a spawn setting the spawnid
|
||||
SpawnAt,
|
||||
Trigger,
|
||||
Teleport,
|
||||
Platform,
|
||||
//Speedtrap(TrapCondition),//Acts as a trigger with a speed condition
|
||||
}
|
||||
#[derive(Clone)]
|
||||
pub struct GameMechanicStageElement{
|
||||
pub mode_id:u32,
|
||||
pub stage_id:u32,//which spawn to send to
|
||||
pub force:bool,//allow setting to lower spawn id i.e. 7->3
|
||||
pub behaviour:StageElementBehaviour
|
||||
}
|
||||
#[derive(Clone)]
|
||||
pub struct GameMechanicWormhole{//(position,angles)*=origin.transform.inverse()*destination.transform
|
||||
pub model_id:u32,
|
||||
}
|
||||
#[derive(Default,Clone)]
|
||||
pub struct GameMechanicAttributes{
|
||||
pub jump_limit:Option<GameMechanicJumpLimit>,
|
||||
pub booster:Option<GameMechanicBooster>,
|
||||
pub zone:Option<GameMechanicZone>,
|
||||
pub stage_element:Option<GameMechanicStageElement>,
|
||||
pub wormhole:Option<GameMechanicWormhole>,//stage_element and wormhole are in conflict
|
||||
}
|
||||
#[derive(Default,Clone)]
|
||||
pub struct ContactingAttributes{
|
||||
pub elasticity:Option<u32>,//[1/2^32,1] 0=None (elasticity+1)/2^32
|
||||
//friction?
|
||||
pub surf:Option<ContactingSurf>,
|
||||
pub ladder:Option<ContactingLadder>,
|
||||
}
|
||||
#[derive(Default,Clone)]
|
||||
pub struct IntersectingAttributes{
|
||||
pub water:Option<IntersectingWater>,
|
||||
pub accelerator:Option<IntersectingAccelerator>,
|
||||
}
|
||||
//Spawn(u32) NO! spawns are indexed in the map header instead of marked with attibutes
|
||||
pub enum CollisionAttributes{
|
||||
Decoration,//visual only
|
||||
Contact{//track whether you are contacting the object
|
||||
contacting:ContactingAttributes,
|
||||
general:GameMechanicAttributes,
|
||||
},
|
||||
Intersect{//track whether you are intersecting the object
|
||||
intersecting:IntersectingAttributes,
|
||||
general:GameMechanicAttributes,
|
||||
},
|
||||
}
|
||||
impl CollisionAttributes{
|
||||
pub fn contact() -> Self {
|
||||
Self::Contact{
|
||||
contacting:ContactingAttributes::default(),
|
||||
general:GameMechanicAttributes::default()
|
||||
}
|
||||
}
|
||||
//object_index for spawns, triggers etc?
|
||||
}
|
||||
|
||||
pub fn generate_indexed_model_list_from_obj(data:obj::ObjData,color:[f32;4]) -> Vec<IndexedModel>{
|
||||
|
@ -206,7 +206,7 @@ pub fn generate_partial_unit_cube(face_descriptions:CubeFaceDescription)->crate:
|
||||
let mut groups=Vec::new();
|
||||
let mut transforms=Vec::new();
|
||||
//note that on a cube every vertex is guaranteed to be unique, so there's no need to hash them against existing vertices.
|
||||
for (face,face_description) in face_descriptions.into_iter(){
|
||||
for (face,face_description) in face_descriptions.iter(){
|
||||
//assume that scanning short lists is faster than hashing.
|
||||
let transform_index=if let Some(transform_index)=transforms.iter().position(|&transform|transform==face_description.transform){
|
||||
transform_index
|
||||
@ -321,7 +321,7 @@ pub fn generate_partial_unit_wedge(face_descriptions:WedgeFaceDescription)->crat
|
||||
let mut groups=Vec::new();
|
||||
let mut transforms=Vec::new();
|
||||
//note that on a cube every vertex is guaranteed to be unique, so there's no need to hash them against existing vertices.
|
||||
for (face,face_description) in face_descriptions.into_iter(){
|
||||
for (face,face_description) in face_descriptions.iter(){
|
||||
//assume that scanning short lists is faster than hashing.
|
||||
let transform_index=if let Some(transform_index)=transforms.iter().position(|&transform|transform==face_description.transform){
|
||||
transform_index
|
||||
@ -433,7 +433,7 @@ pub fn generate_partial_unit_cornerwedge(face_descriptions:CornerWedgeFaceDescri
|
||||
let mut groups=Vec::new();
|
||||
let mut transforms=Vec::new();
|
||||
//note that on a cube every vertex is guaranteed to be unique, so there's no need to hash them against existing vertices.
|
||||
for (face,face_description) in face_descriptions.into_iter(){
|
||||
for (face,face_description) in face_descriptions.iter(){
|
||||
//assume that scanning short lists is faster than hashing.
|
||||
let transform_index=if let Some(transform_index)=transforms.iter().position(|&transform|transform==face_description.transform){
|
||||
transform_index
|
||||
|
Reference in New Issue
Block a user