Skip to content

Commit

Permalink
fix: laser projectile fixes (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-xr authored Oct 12, 2023
1 parent db92519 commit e35f8fe
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 30 deletions.
5 changes: 4 additions & 1 deletion shock2vr/src/command/spawn_item_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use cgmath::{vec3, Matrix4, Quaternion, SquareMatrix};
use dark::SCALE_FACTOR;
use shipyard::{UniqueView, World};

use crate::{scripts::Effect, util::vec3_to_point3, PlayerInfo};
use crate::{
mission::entity_creator::CreateEntityOptions, scripts::Effect, util::vec3_to_point3, PlayerInfo,
};

use super::Command;
// SpawnItemCommand
Expand Down Expand Up @@ -38,6 +40,7 @@ impl Command for SpawnItemCommand {
position: pos + forward,
orientation: rot,
root_transform: Matrix4::identity(),
options: CreateEntityOptions::default(),
}
}
}
32 changes: 23 additions & 9 deletions shock2vr/src/mission/entity_creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,19 @@ pub fn create_entity_with_position(
entity_info: &ss2_entity_info::SystemShock2EntityInfo,
obj_name_map: &HashMap<i32, String>, // name override map
template_to_entity_id: &HashMap<i32, WrappedEntityId>, // realized entities from level start
additional_options: CreateEntityOptions,
) -> EntityCreationInfo {
// Create initial entity
let entity_id = world.add_entity(());

// Add props, based on inheritance
initialize_entity_with_props(template_id, entity_info, world, entity_id, obj_name_map);

if additional_options.force_visible {
world.add_component(entity_id, PropHasRefs(true));
world.add_component(entity_id, PropRenderType(RenderType::Normal));
};

initialize_links_for_entity(
template_id,
entity_id,
Expand Down Expand Up @@ -115,6 +121,7 @@ pub fn create_entity_with_position(
entity_info,
template_to_entity_id,
obj_name_map,
additional_options,
)
}

Expand All @@ -128,6 +135,7 @@ pub fn initialize_entity(
entity_info: &ss2_entity_info::SystemShock2EntityInfo,
obj_name_map: &HashMap<i32, String>, // name override map
template_to_entity_id: &HashMap<i32, WrappedEntityId>, // realized entities from level start
additional_options: CreateEntityOptions,
) -> EntityCreationInfo {
let scale = {
let v_scale = world.borrow::<View<PropScale>>().unwrap();
Expand Down Expand Up @@ -159,6 +167,7 @@ pub fn initialize_entity(
entity_info,
template_to_entity_id,
obj_name_map,
additional_options,
)
}

Expand All @@ -172,6 +181,7 @@ pub fn create_entity_core(
entity_info: &ss2_entity_info::SystemShock2EntityInfo,
_template_to_entity_id: &HashMap<i32, WrappedEntityId>, // realized entities from level start
obj_map: &HashMap<i32, String>,
additional_options: CreateEntityOptions,
) -> EntityCreationInfo {
// Add template id
world.add_component(entity_id, PropTemplateId { template_id });
Expand Down Expand Up @@ -332,15 +342,6 @@ fn create_model(
.unwrap();

if let (Ok(pos), Ok(model)) = (v_prop_position.get(entity_id), v_prop_model.get(entity_id)) {
// We have some sort of model, but need to refine
// TODO: This logic keeps projectiles from rendering - why?
if v_rendertype.contains(entity_id) {
let render_type = v_rendertype.get(entity_id).unwrap();
if render_type.0 == RenderType::EditorOnly || render_type.0 == RenderType::NoRender {
return None;
};
}

let model_name = model.0.to_owned();
let maybe_model = asset_cache.get_opt(&MODELS_IMPORTER, &format!("{model_name}.BIN"));

Expand Down Expand Up @@ -763,3 +764,16 @@ pub fn create_physics_representation(
}
}
}

#[derive(Clone, Debug)]
pub struct CreateEntityOptions {
pub force_visible: bool,
}

impl Default for CreateEntityOptions {
fn default() -> Self {
CreateEntityOptions {
force_visible: false,
}
}
}
28 changes: 25 additions & 3 deletions shock2vr/src/mission/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ use dark::{
Link, LinkDefinition, LinkDefinitionWithData, Links, PhysicsModelType, PropCreature,
PropFrameAnimState, PropHasRefs, PropLocalPlayer, PropModelName, PropMotionActorTags,
PropParticleGroup, PropParticleLaunchInfo, PropPhysDimensions, PropPhysInitialVelocity,
PropPhysState, PropPhysType, PropPosition, PropScripts, PropTeleported, PropTripFlags,
PropertyDefinition, ToLink, TripFlags, WrappedEntityId,
PropPhysState, PropPhysType, PropPosition, PropRenderType, PropScripts, PropTeleported,
PropTripFlags, PropertyDefinition, RenderType, ToLink, TripFlags, WrappedEntityId,
},
ss2_entity_info::{self, SystemShock2EntityInfo},
BitmapAnimation, SCALE_FACTOR,
Expand Down Expand Up @@ -81,7 +81,10 @@ use crate::{
vr_config, GameOptions,
};

use self::{entity_creator::EntityCreationInfo, visibility_engine::VisibilityEngine};
use self::{
entity_creator::{CreateEntityOptions, EntityCreationInfo},
visibility_engine::VisibilityEngine,
};
#[cfg(target_os = "android")]
const BASE_PATH: &str = "/mnt/sdcard/shock2quest";

Expand Down Expand Up @@ -290,6 +293,7 @@ impl Mission {
// TODO:
&HashMap::new(),
&template_to_entity_id,
CreateEntityOptions::default(),
);

Self::finish_instantiating_entity(
Expand Down Expand Up @@ -588,6 +592,7 @@ impl Mission {
vec3_to_point3(position),
rotation,
Matrix4::identity(),
CreateEntityOptions::default(),
);
//did_slay = true;
}
Expand All @@ -602,6 +607,7 @@ impl Mission {
vec3_to_point3(position),
rotation,
Matrix4::identity(),
CreateEntityOptions::default(),
);
//did_slay = true;
}
Expand Down Expand Up @@ -630,6 +636,7 @@ impl Mission {
position,
orientation,
Matrix4::identity(),
CreateEntityOptions::default(),
))
} else {
None
Expand Down Expand Up @@ -710,6 +717,7 @@ impl Mission {
position: Point3<f32>,
orientation: Quaternion<f32>,
root_transform: Matrix4<f32>,
additional_options: CreateEntityOptions,
) -> EntityCreationInfo {
let created_entity = {
entity_creator::create_entity_with_position(
Expand All @@ -725,6 +733,7 @@ impl Mission {
// TODO:
&HashMap::new(),
&HashMap::new(),
additional_options,
)
};

Expand Down Expand Up @@ -881,13 +890,15 @@ impl Mission {
position,
orientation,
root_transform,
options,
} => {
self.create_entity_with_position(
asset_cache,
template_id,
position,
orientation,
root_transform,
options,
);
}
Effect::DropEntityInfo {
Expand Down Expand Up @@ -1089,6 +1100,7 @@ impl Mission {
vec3_to_point3(position),
rotation,
Matrix4::identity(),
CreateEntityOptions::default(),
);

if new_entity_info.rigid_body.is_some() {
Expand Down Expand Up @@ -1382,6 +1394,7 @@ impl Mission {
let _v_position = self.world.borrow::<View<PropPosition>>().unwrap();
let v_transform = self.world.borrow::<View<RuntimePropTransform>>().unwrap();
let v_frame_state = self.world.borrow::<View<PropFrameAnimState>>().unwrap();
let v_render_type = self.world.borrow::<View<PropRenderType>>().unwrap();

// Start with built in scene objects
let mut scene = self.scene_objects.clone();
Expand All @@ -1396,6 +1409,14 @@ impl Mission {
continue;
}

if v_render_type.contains(*entity_id) {
let render_type = v_render_type.get(*entity_id).unwrap();
if render_type.0 == RenderType::EditorOnly || render_type.0 == RenderType::NoRender
{
continue;
};
}

if !self.visibility_engine.is_visible(*entity_id) {
continue;
}
Expand Down Expand Up @@ -1610,6 +1631,7 @@ impl Mission {
vec3_to_point3(position),
rotation,
Matrix4::identity(),
CreateEntityOptions::default(),
);
}
VirtualHandEffect::HoldItem {
Expand Down
11 changes: 6 additions & 5 deletions shock2vr/src/scripts/ai/ai_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@ use shipyard::{EntityId, Get, IntoIter, IntoWithId, UniqueView, View, World};

use crate::{
creature,
mission::PlayerInfo,
mission::{entity_creator::CreateEntityOptions, PlayerInfo},
physics::{InternalCollisionGroups, PhysicsWorld},
runtime_props::{RuntimePropJointTransforms, RuntimePropTransform},
scripts::{
script_util::{get_first_link_with_template_and_data},
Effect,
},
scripts::{script_util::get_first_link_with_template_and_data, Effect},
util,
};

Expand Down Expand Up @@ -148,6 +145,7 @@ pub fn fire_ranged_weapon(world: &World, entity_id: EntityId, rotation: Quaterni
position,
orientation: rotation,
root_transform: root_transform.0,
options: CreateEntityOptions::default(),
}
} else {
let rot_matrix: Matrix4<f32> = rotation.into();
Expand Down Expand Up @@ -183,6 +181,7 @@ pub fn fire_ranged_weapon(world: &World, entity_id: EntityId, rotation: Quaterni
position: point3(0.0, 0.0, 0.0) + forward,
orientation: Quaternion::from_angle_y(Deg(90.0)),
root_transform: root_transform.0 * rot_matrix,
options: CreateEntityOptions::default(),
});

fire_effects.push(play_positional_sound(
Expand All @@ -208,6 +207,7 @@ pub fn fire_ranged_weapon(world: &World, entity_id: EntityId, rotation: Quaterni
position: point3(0.0, 0.0, 0.0) + forward,
orientation: Quaternion::from_angle_y(Deg(90.0)),
root_transform: root_transform.0 * rot_matrix,
options: CreateEntityOptions::default(),
})
}

Expand Down Expand Up @@ -287,6 +287,7 @@ pub fn fire_ranged_projectile(world: &World, entity_id: EntityId) -> Effect {
orientation: Quaternion::from_angle_y(Deg(90.0)),
// root_transform: transform * rot_matrix,
root_transform: transform,
options: CreateEntityOptions::default(),
}
} else {
Effect::NoEffect
Expand Down
4 changes: 3 additions & 1 deletion shock2vr/src/scripts/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use dark::{
EnvSoundQuery,
};
use engine::audio::AudioHandle;
use shipyard::EntityId;
use shipyard::{Component, EntityId};

use crate::{
gui::{GuiComponentRenderInfo, GuiHandle},
mission::entity_creator::CreateEntityOptions,
vr_config::Handedness,
};

Expand Down Expand Up @@ -68,6 +69,7 @@ pub enum Effect {
position: Point3<f32>,
orientation: Quaternion<f32>,
root_transform: Matrix4<f32>,
options: CreateEntityOptions,
},

DrawDebugLines {
Expand Down
3 changes: 2 additions & 1 deletion shock2vr/src/scripts/internal_fast_projectile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
time::Time,
util::{
get_position_from_transform, get_rotation_from_forward_vector,
},
}, mission::entity_creator::CreateEntityOptions,
};

use super::{Effect, MessagePayload, Script};
Expand Down Expand Up @@ -120,6 +120,7 @@ impl Script for InternalFastProjectileScript {
orientation: get_rotation_from_forward_vector(hit_normal)
* Quaternion::from_axis_angle(vec3(0.0, 1.0, 0.0), Deg(90.0)),
root_transform: Matrix4::identity(),
options: CreateEntityOptions::default(),
});

Effect::combine(effects)
Expand Down
15 changes: 7 additions & 8 deletions shock2vr/src/scripts/weapon_script.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@


use cgmath::{
point3, Deg, Matrix4, Quaternion, Rotation, Rotation3, Transform,
};
use dark::properties::{
GunFlashOptions, Link, ProjectileOptions,
};
use cgmath::{point3, Deg, Matrix4, Quaternion, Rotation, Rotation3, Transform};
use dark::properties::{GunFlashOptions, Link, ProjectileOptions};
use engine::audio::AudioHandle;
use shipyard::{EntityId, Get, View, World};

use crate::{
mission::entity_creator::CreateEntityOptions,
physics::PhysicsWorld,
runtime_props::{RuntimePropTransform, RuntimePropVhots},
vr_config,
Expand Down Expand Up @@ -123,6 +118,7 @@ fn create_muzzle_flash(
position: vhot_offset,
orientation,
root_transform: transform.0,
options: CreateEntityOptions::default(),
}
}

Expand Down Expand Up @@ -170,5 +166,8 @@ fn create_projectile(
// models are rotated 90 degrees
orientation: Quaternion::from_angle_y(Deg(90.0)),
root_transform: transform.0 * rot_matrix * projectile_rotation,
options: CreateEntityOptions {
force_visible: true,
},
}
}
20 changes: 18 additions & 2 deletions shock2vr/src/vr_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ impl VRHandModelAdjustments {
projectile_rotation,
}
}

pub fn with_projectile_rotation(
self,
projectile_rotation: Quaternion<f32>,
) -> VRHandModelAdjustments {
VRHandModelAdjustments {
projectile_rotation,
..self
}
}
}

static HAND_MODEL_POSITIONING: Lazy<HashMap<&str, VRHandModelAdjustments>> = Lazy::new(|| {
Expand All @@ -79,7 +89,8 @@ static HAND_MODEL_POSITIONING: Lazy<HashMap<&str, VRHandModelAdjustments>> = Laz
held_weapon_left,
held_weapon_right,
Quaternion::from_angle_y(Deg(0.0)),
);
)
.with_projectile_rotation(Quaternion::from_angle_y(Deg(12.)));

let held_item_hand = VRHandModelPerHandAdjustments::new().rotate_y(Deg(180.0));
let held_item = VRHandModelAdjustments::new(
Expand All @@ -101,7 +112,12 @@ static HAND_MODEL_POSITIONING: Lazy<HashMap<&str, VRHandModelAdjustments>> = Laz
// Weapons
("atek_h", held_weapon.clone()),
("amp_h", held_weapon.clone()),
("lasehand", held_weapon.clone()),
(
"lasehand",
held_weapon
.clone()
.with_projectile_rotation(Quaternion::from_angle_y(Deg(12.))),
),
("empgun", held_weapon.clone()),
("wrench_h", default.clone()),
("sg_w", held_weapon.clone()),
Expand Down

0 comments on commit e35f8fe

Please sign in to comment.