Skip to content

Commit

Permalink
Add health items and death
Browse files Browse the repository at this point in the history
Also lots of other stuff :3
Closes #30, #31
  • Loading branch information
Alainx277 committed Jan 5, 2024
1 parent d4ee688 commit dfc3668
Show file tree
Hide file tree
Showing 27 changed files with 1,452 additions and 148 deletions.
49 changes: 49 additions & 0 deletions assets/creatures/ghost.scn.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
(
entities: {
0: (
components: {
"bevy_transform::components::transform::Transform": (
),
"networking::transform::NetworkTransform": (
),
"bevy_asset::handle::Handle<bevy_render::mesh::mesh::Mesh>": (
id: "models/ghost.glb#Mesh0/Primitive0"
),
"bevy_asset::handle::Handle<bevy_pbr::pbr_material::StandardMaterial>": (
id: "models/ghost.glb#Material0"
),
"ssnt::Player": (),
"ssnt::body::Body": (
),
"physics::RigidBody": (
kind: Dynamic
),
"bevy_rapier3d::dynamics::rigid_body::ReadMassProperties": (()),
"bevy_rapier3d::dynamics::rigid_body::Velocity": (),
"bevy_rapier3d::dynamics::rigid_body::LockedAxes": (
bits: 56
),
"bevy_hierarchy::components::children::Children": ([
1,
]),
}
),
// Basic collider
1: (
components: {
"bevy_hierarchy::components::parent::Parent": (0),
"bevy_transform::components::transform::Transform": (
translation: (
x: 0.0,
y: 0.65,
z: 0.0,
),
),
"physics::Collider": (
kind: Capsule (hy: 0.5, r: 0.18),
group: CharacterColliders,
)
}
),
}
)
41 changes: 41 additions & 0 deletions assets/items/bandage.scn.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
(
entities: {
0: (
components: {
"bevy_transform::components::transform::Transform": (
),
"networking::transform::NetworkTransform": (
),
"bevy_asset::handle::Handle<bevy_render::mesh::mesh::Mesh>": (
id: "models/items/bandage.glb#Mesh0/Primitive0"
),
"ssnt::items::Item": (
name: "Hydrogel Bandage"
),
"ssnt::body::health::items::HealingItem": (
),
"ssnt::body::health::items::HealOrganicLaceration": (
),
"physics::RigidBody": (
kind: Dynamic
),
"bevy_hierarchy::components::children::Children": ([1]),
}
),
1: (
components: {
"bevy_hierarchy::components::parent::Parent": (0),
"bevy_transform::components::transform::Transform": (
translation: (
x: 0.0,
y: 0.0,
z: 0.0,
),
),
"physics::Collider": (
kind: Cuboid (hx: 0.11, hy: 0.04, hz: 0.07)
)
}
)
}
)
39 changes: 39 additions & 0 deletions assets/items/blood bag.scn.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
(
entities: {
0: (
components: {
"bevy_transform::components::transform::Transform": (
),
"networking::transform::NetworkTransform": (
),
"bevy_asset::handle::Handle<bevy_render::mesh::mesh::Mesh>": (
id: "models/items/blood bag.glb#Mesh0/Primitive0"
),
"ssnt::items::Item": (
name: "Blood Bag"
),
"ssnt::body::health::items::BloodTransfusion": (
),
"physics::RigidBody": (
kind: Dynamic
),
"bevy_hierarchy::components::children::Children": ([1]),
}
),
1: (
components: {
"bevy_hierarchy::components::parent::Parent": (0),
"bevy_transform::components::transform::Transform": (
translation: (
x: 0.0,
y: 0.05,
z: 0.0,
),
),
"physics::Collider": (
kind: Cuboid (hx: 0.11, hy: 0.06, hz: 0.17)
)
}
)
}
)
39 changes: 39 additions & 0 deletions assets/items/defibrillator.scn.ron
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
(
entities: {
0: (
components: {
"bevy_transform::components::transform::Transform": (
),
"networking::transform::NetworkTransform": (
),
"bevy_asset::handle::Handle<bevy_render::mesh::mesh::Mesh>": (
id: "models/items/defibrillator.glb#Mesh1/Primitive0"
),
"ssnt::items::Item": (
name: "Defibrillator"
),
"ssnt::body::health::items::Defibrillator": (
),
"physics::RigidBody": (
kind: Dynamic
),
"bevy_hierarchy::components::children::Children": ([1]),
}
),
1: (
components: {
"bevy_hierarchy::components::parent::Parent": (0),
"bevy_transform::components::transform::Transform": (
translation: (
x: 0.0,
y: 0.0,
z: -0.15,
),
),
"physics::Collider": (
kind: Cuboid (hx: 0.2, hy: 0.23, hz: 0.15)
)
}
)
}
)
Binary file added assets/models/ghost.glb
Binary file not shown.
Binary file added assets/models/items/bandage.glb
Binary file not shown.
Binary file added assets/models/items/blood bag.glb
Binary file not shown.
Binary file added assets/models/items/defibrillator.glb
Binary file not shown.
2 changes: 1 addition & 1 deletion crates/networking/networking_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use syn::{
};

#[derive(Debug, FromDeriveInput)]
#[darling(attributes(networked), supports(struct_named))]
#[darling(attributes(networked), supports(struct_named, struct_unit))]
struct NetworkedInput {
ident: Ident,
data: ast::Data<darling::util::Ignored, NetworkedFieldInput>,
Expand Down
59 changes: 36 additions & 23 deletions crates/networking/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
time::ServerNetworkTime,
variable::*,
visibility::NetworkVisibilities,
NetworkManager, NetworkSet,
ConnectionId, NetworkManager, NetworkSet,
};

/// A message that contains data for a component.
Expand Down Expand Up @@ -51,25 +51,41 @@ fn send_networked_component_changed<S: NetworkedToClient + Component, C: Network
server_time: Res<ServerNetworkTime>,
mut sender: MessageSender,
mut param: bevy::ecs::system::StaticSystemParam<S::Param>,
mut observer_cache: Local<HashSet<ConnectionId>>,
) {
for (identity, mut component) in components.iter_mut() {
let visibility = match visibilities.visibility.get(identity) {
Some(v) => v,
None => continue,
};
let maybe_visibility = visibilities.visibility.get(identity);
let explicit_observers = component.limit_observers();
// Skip if no observer data
if maybe_visibility.is_none() && explicit_observers.is_none() {
continue;
}

// Check if component networked state changes
if !component.update_state(server_time.current_tick()) {
if !component.is_added() && !component.update_state(server_time.current_tick()) {
continue;
}

// Get relevant observers
observer_cache.clear();
if let Some(explicit) = explicit_observers {
observer_cache.extend(explicit);
} else {
observer_cache.extend(maybe_visibility.unwrap().observers().copied());
}

// Skip if not observed
if observer_cache.is_empty() {
continue;
}

let component_id = registry
.get_id(&C::TYPE_UUID)
.expect("Networked component incorrectly registered");
let priority = component.priority();
if S::receiver_matters() {
// Serialize component for every receiver
let priority = component.priority();
for connection in visibility.observers() {
for connection in observer_cache.iter() {
let data = match component.serialize(&mut param, Some(*connection), None) {
Some(d) => d,
None => continue,
Expand All @@ -86,21 +102,18 @@ fn send_networked_component_changed<S: NetworkedToClient + Component, C: Network
);
}
} else {
let new_observers: HashSet<_> = visibility.observers().copied().collect();
if !new_observers.is_empty() {
let Some(data) = component.serialize(&mut param, None, None) else {
continue;
};
sender.send_with_priority(
&NetworkedComponentMessage {
identity: *identity,
component_id,
data,
},
MessageReceivers::Set(new_observers),
component.priority(),
);
}
let Some(data) = component.serialize(&mut param, None, None) else {
continue;
};
sender.send_with_priority(
&NetworkedComponentMessage {
identity: *identity,
component_id,
data,
},
MessageReceivers::Set(observer_cache.clone()),
priority,
);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/networking/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl Command for NetworkCommand {
let mut entity = world.entity_mut(self.entity);
entity.insert(NetworkIdentity(id));

if !entity.contains::<InGrid>() {
if !entity.contains::<InGrid>() && entity.contains::<Transform>() {
entity.insert(InGrid::default());
}
}
Expand Down
Loading

0 comments on commit dfc3668

Please sign in to comment.