Skip to content

Commit

Permalink
reimplement player system
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhhh committed Oct 15, 2024
1 parent f9d8aea commit e3a2d8e
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 89 deletions.
1 change: 0 additions & 1 deletion crates/unavi-scripting/src/api/wired/physics/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pub(crate) fn update_physics_transforms(
}

if let Some(node) = node.0.upgrade() {
info!("--------- FOUND NODE -----------> {:?}", transform);
node.write().unwrap().transform = *transform;
}
}
Expand Down
51 changes: 30 additions & 21 deletions crates/unavi-scripting/src/api/wired/player/player.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::{Arc, RwLock};
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};

use wasm_bridge::component::Resource;
use wasm_bridge_wasi::ResourceTable;
Expand Down Expand Up @@ -27,33 +27,42 @@ impl HostPlayer for ScriptData {
#[derive(Clone)]
pub struct PlayerRes(pub Arc<RwLock<PlayerData>>);

impl PlayerRes {
pub fn read(&self) -> RwLockReadGuard<PlayerData> {
self.0.read().unwrap()
}
pub fn write(&self) -> RwLockWriteGuard<PlayerData> {
self.0.write().unwrap()
}
}

pub struct PlayerData {
pub root: NodeRes,
pub skeleton: SkeletonData,
}

#[derive(Clone)]
pub struct SkeletonData {
hips: NodeRes,
spine: NodeRes,
chest: NodeRes,
upper_chest: NodeRes,
neck: NodeRes,
head: NodeRes,
left_shoulder: NodeRes,
left_upper_arm: NodeRes,
left_lower_arm: NodeRes,
left_foot: NodeRes,
left_hand: NodeRes,
left_lower_leg: NodeRes,
left_upper_leg: NodeRes,
right_shoulder: NodeRes,
right_upper_arm: NodeRes,
right_lower_arm: NodeRes,
right_foot: NodeRes,
right_hand: NodeRes,
right_lower_leg: NodeRes,
right_upper_leg: NodeRes,
pub hips: NodeRes,
pub spine: NodeRes,
pub chest: NodeRes,
pub upper_chest: NodeRes,
pub neck: NodeRes,
pub head: NodeRes,
pub left_shoulder: NodeRes,
pub left_upper_arm: NodeRes,
pub left_lower_arm: NodeRes,
pub left_foot: NodeRes,
pub left_hand: NodeRes,
pub left_lower_leg: NodeRes,
pub left_upper_leg: NodeRes,
pub right_shoulder: NodeRes,
pub right_upper_arm: NodeRes,
pub right_lower_arm: NodeRes,
pub right_foot: NodeRes,
pub right_hand: NodeRes,
pub right_lower_leg: NodeRes,
pub right_upper_leg: NodeRes,
}

impl PlayerRes {
Expand Down
112 changes: 51 additions & 61 deletions crates/unavi-scripting/src/api/wired/player/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct CopyGlobalTransform(pub Entity);

pub(crate) fn update_player_skeletons(
bones: Query<(Entity, &PlayerId, &BoneName, &Transform)>,
commands: Commands,
mut commands: Commands,
players: Query<(Entity, &PlayerId), With<PlayerAvatar>>,
script_map: NonSendMut<ScriptMap>,
scripts: Query<(Entity, &ScriptTickrate)>,
Expand All @@ -31,72 +31,62 @@ pub(crate) fn update_player_skeletons(
};

let data = script.store.data_mut();
let player = &data.api.wired_player.as_ref().unwrap().local_player;
let skeleton = &player.0.read().unwrap().skeleton;
let player = &data.api.wired_player.as_ref().unwrap().local_player.read();

for (player_ent, id) in players.iter() {
if id.0 != LOCAL_PLAYER_ID {
continue;
}

// if let Ok(nodes) = data.api.wired_scene.as_ref().unwrap().entities.nodes.read() {
// if let Some(node_ent) = nodes.get(&root.rep()) {
// commands
// .entity(*node_ent)
// .insert(CopyGlobalTransform(player_ent));
// }
// }
//
// let pairs: [(BoneName, &wasm_bridge::component::Resource<NodeRes>); 20] = [
// (BoneName::Hips, &skeleton.hips),
// (BoneName::Chest, &skeleton.chest),
// (BoneName::UpperChest, &skeleton.upper_chest),
// (BoneName::Neck, &skeleton.neck),
// (BoneName::Head, &skeleton.head),
// (BoneName::Spine, &skeleton.spine),
// (BoneName::LeftShoulder, &skeleton.left_shoulder),
// (BoneName::LeftUpperArm, &skeleton.left_upper_arm),
// (BoneName::LeftLowerArm, &skeleton.left_lower_arm),
// (BoneName::LeftHand, &skeleton.left_hand),
// (BoneName::LeftUpperLeg, &skeleton.left_upper_leg),
// (BoneName::LeftLowerLeg, &skeleton.left_lower_leg),
// (BoneName::LeftFoot, &skeleton.left_foot),
// (BoneName::RightShoulder, &skeleton.right_shoulder),
// (BoneName::RightUpperArm, &skeleton.right_upper_arm),
// (BoneName::RightLowerArm, &skeleton.right_lower_arm),
// (BoneName::RightHand, &skeleton.right_hand),
// (BoneName::RightUpperLeg, &skeleton.right_upper_leg),
// (BoneName::RightLowerLeg, &skeleton.right_lower_leg),
// (BoneName::RightFoot, &skeleton.right_foot),
// ];
//
// for (bone_ent, bone_id, bone_name, bone_transform) in bones.iter() {
// if bone_id != id {
// continue;
// }
//
// for (pair_name, node_res) in pairs.iter() {
// if pair_name != bone_name {
// continue;
// }
//
// if let Ok(nodes) = data.api.wired_scene.as_ref().unwrap().entities.nodes.read()
// {
// if let Some(node_ent) = nodes.get(&node_res.rep()) {
// commands.entity(*node_ent).insert(
// // "Parent" node entity to bone.
// // We do not use actual parenting, because it causes issues when
// // physics colliders are children of the player rigid body.
// CopyTransform(bone_ent),
// );
// }
// }
//
// // Set node resource transform.
// let node = data.table.get_mut(node_res).unwrap();
// node.transform = *bone_transform;
// }
// }
commands
.entity(*player.root.read().entity.get().unwrap())
.insert(CopyGlobalTransform(player_ent));

let pairs = [
(BoneName::Hips, &player.skeleton.hips),
(BoneName::Chest, &player.skeleton.chest),
(BoneName::UpperChest, &player.skeleton.upper_chest),
(BoneName::Neck, &player.skeleton.neck),
(BoneName::Head, &player.skeleton.head),
(BoneName::Spine, &player.skeleton.spine),
(BoneName::LeftShoulder, &player.skeleton.left_shoulder),
(BoneName::LeftUpperArm, &player.skeleton.left_upper_arm),
(BoneName::LeftLowerArm, &player.skeleton.left_lower_arm),
(BoneName::LeftHand, &player.skeleton.left_hand),
(BoneName::LeftUpperLeg, &player.skeleton.left_upper_leg),
(BoneName::LeftLowerLeg, &player.skeleton.left_lower_leg),
(BoneName::LeftFoot, &player.skeleton.left_foot),
(BoneName::RightShoulder, &player.skeleton.right_shoulder),
(BoneName::RightUpperArm, &player.skeleton.right_upper_arm),
(BoneName::RightLowerArm, &player.skeleton.right_lower_arm),
(BoneName::RightHand, &player.skeleton.right_hand),
(BoneName::RightUpperLeg, &player.skeleton.right_upper_leg),
(BoneName::RightLowerLeg, &player.skeleton.right_lower_leg),
(BoneName::RightFoot, &player.skeleton.right_foot),
];

for (bone_ent, bone_id, bone_name, bone_transform) in bones.iter() {
if bone_id != id {
continue;
}

for (pair_name, node) in pairs.iter() {
if pair_name != bone_name {
continue;
}

let mut node = node.write();

// We do not use parenting, because it causes issues when
// physics colliders are children of the player rigid body.
commands
.entity(*node.entity.get().unwrap())
.insert(CopyTransform(bone_ent));

// Set node resource transform.
node.transform = *bone_transform;
}
}
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions crates/unavi-scripting/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ pub fn init_scripts(
script_map: NonSendMut<ScriptMap>,
) {
for (entity, name) in to_init.iter() {
let mut scripts = script_map.lock().unwrap();
let script = scripts.get_mut(&entity).expect("Script not found");

#[allow(clippy::await_holding_lock)]
let result = block_on(async {
info!("Initializing script {}", name);

let mut scripts = script_map.lock().unwrap();
let script = scripts.get_mut(&entity).expect("Script not found");

script
.script
.wired_script_types()
Expand All @@ -42,6 +42,8 @@ pub fn init_scripts(
}
};

script.store.data().push_commands(&mut commands);

commands.entity(entity).insert(ScriptResource(result));
}
}
Expand Down
8 changes: 5 additions & 3 deletions crates/unavi-scripting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@ impl Plugin for ScriptingPlugin {
.add_systems(
FixedUpdate,
(
load::load_scripts,
execution::tick_scripts,
(
api::wired::physics::systems::update_physics_transforms,
api::wired::player::systems::copy_global_transforms,
api::wired::player::systems::copy_transforms,
api::wired::player::systems::update_player_skeletons,
),
execution::update_scripts,
execution::init_scripts,
(
execution::init_scripts,
execution::update_scripts,
load::load_scripts,
),
apply_deferred,
)
.chain(),
Expand Down
2 changes: 2 additions & 0 deletions crates/unavi-scripting/src/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub fn load_scripts(
let bytes = wasm.0.clone();
let scripts = scripts.clone();

builder.data.push_commands(&mut commands);

pool.spawn(async move {
let script = builder.instantiate_script(&bytes).await?;

Expand Down

0 comments on commit e3a2d8e

Please sign in to comment.