Skip to content

Commit

Permalink
Fixed Tools calling server on client-side entities;
Browse files Browse the repository at this point in the history
Using new Health/Ammo events;
Fixed Gravitating Sound keeping playing;
  • Loading branch information
gtnardy committed Apr 2, 2022
1 parent cfe3a2e commit cf697d5
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 40 deletions.
55 changes: 28 additions & 27 deletions Client/Index.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,14 @@ function UpdateLocalCharacter(character)
UpdateHealth(character:GetHealth())

-- Sets on character an event to update the health's UI after it takes damage
character:Subscribe("TakeDamage", function(charac, damage, type, bone, from_direction, instigator, causer)
-- Plays a Hit Taken sound effect
Sound(Vector(), "nanos-world::A_HitTaken_Feedback", true)

-- Updates the Health UI
UpdateHealth(math.max(charac:GetHealth() - damage, 0))
end)

-- Sets on character an event to update the health's UI after it dies
character:Subscribe("Death", function(charac)
UpdateHealth(0)
end)
character:Subscribe("HealthChanged", function(charac, old_health, new_health)
-- Plays a Hit Taken sound effect if took damage
if (new_health < old_health) then
Sound(Vector(), "nanos-world::A_HitTaken_Feedback", true)
end

-- Sets on character an event to update the health's UI after it respawns
character:Subscribe("Respawn", function(charac)
UpdateHealth(100)
-- Immediatelly Updates the Health UI
UpdateHealth(new_health)
end)

-- Try to get if the character is holding any weapon
Expand All @@ -104,29 +96,28 @@ function UpdateLocalCharacter(character)
-- Sets on character an event to update his grabbing weapon (to show ammo on UI)
character:Subscribe("PickUp", function(charac, object)
if (object:GetType() == "Weapon" and not object:GetValue("ToolGun")) then
-- Immediatelly Updates the Ammo UI
UpdateAmmo(true, object:GetAmmoClip(), object:GetAmmoBag())

-- Trigger Weapon Hints
AddNotification("AIM_DOWN_SIGHT", "you can use mouse wheel to aim down sight with your Weapon when you are in First Person Mode", 10000, 3000)
AddNotification("HEADSHOTS", "headshots can cause more damage", 10000, 15000)

-- Sets on character an event to update the UI when he fires
character:Subscribe("Fire", function(charac, weapon)
UpdateAmmo(true, weapon:GetAmmoClip(), weapon:GetAmmoBag())
end)

-- Sets on character an event to update the UI when he reloads the weapon
character:Subscribe("Reload", function(charac, weapon, ammo_to_reload)
UpdateAmmo(true, weapon:GetAmmoClip(), weapon:GetAmmoBag())
end)
-- Subscribes on the weapon when the Ammo changes
object:Subscribe("AmmoClipChanged", OnAmmoClipChanged)
object:Subscribe("AmmoBagChanged", OnAmmoBagChanged)
end
end)

-- Sets on character an event to remove the ammo ui when he drops it's weapon
character:Subscribe("Drop", function(charac, object)
UpdateAmmo(false)
character:Unsubscribe("Fire")
character:Unsubscribe("Reload")
-- Unsubscribes from events
if (object:GetType() == "Weapon") then
UpdateAmmo(false)
object:Unsubscribe("AmmoClipChanged", OnAmmoClipChanged)
object:Unsubscribe("AmmoBagChanged", OnAmmoBagChanged)
end

ToggleToolGunAiming(object, "", false)
end)

Expand Down Expand Up @@ -156,6 +147,16 @@ function UpdateHealth(health)
MainHUD:CallEvent("UpdateHealth", health)
end

-- Callback when Weapon Ammo Clip changes
function OnAmmoClipChanged(weapon, old_ammo_clip, new_ammo_clip)
UpdateAmmo(true, new_ammo_clip, weapon:GetAmmoBag())
end

-- Callback when Weapon Ammo Bag changes
function OnAmmoBagChanged(weapon, old_ammo_bag, new_ammo_bag)
UpdateAmmo(true, weapon:GetAmmoClip(), new_ammo_bag)
end

Input.Bind("NoClip", InputEvent.Pressed, function()
Events.CallRemote("ToggleNoClip")
end)
Expand Down
2 changes: 1 addition & 1 deletion Client/SpawnMenu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ Client.Subscribe("Tick", function(delta_time)

if (not trace_result.Success) then return end

local color = trace_result.Entity and DrawDebugToolGun.ColorEntity or DrawDebugToolGun.ColorNoEntity
local color = trace_result.Entity and trace_result.Entity:IsNetworkDistributed() and DrawDebugToolGun.ColorEntity or DrawDebugToolGun.ColorNoEntity

Client.DrawDebugCrosshairs(trace_result.Location, Rotator(), 25, color, 0, 2)
end
Expand Down
2 changes: 1 addition & 1 deletion Client/Tools/Balloon.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function HandleBalloonTool(weapon)

if (trace_result.Success) then
local distance_trace_object = Vector()
if (trace_result.Entity) then
if (trace_result.Entity and trace_result.Entity:IsNetworkDistributed()) then
-- If hit an entity, then calculates the offset distance from the Hit and the Object
distance_trace_object = trace_result.Entity:GetLocation() - trace_result.Location
end
Expand Down
2 changes: 1 addition & 1 deletion Client/Tools/Color.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function HandleColorTool(tool)
local trace_result = TraceFor(10000, CollisionChannel.WorldStatic | CollisionChannel.WorldDynamic | CollisionChannel.PhysicsBody | CollisionChannel.Vehicle | CollisionChannel.Pawn)

-- If hit an object, then get a random Color and call server to update the color for everyone
if (trace_result.Success and trace_result.Entity) then
if (trace_result.Success and trace_result.Entity and trace_result.Entity:IsNetworkDistributed()) then
local color = Color.RandomPalette()
Events.CallRemote("ColorObject", trace_result.Entity, trace_result.Location, trace_result.Normal, color)
else
Expand Down
4 changes: 2 additions & 2 deletions Client/Tools/PhysicsGun.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function TryPickUpObject()
local trace_result = Client.Trace(start_location, end_location, collision_trace, false, true)

-- If hit something and hit an Entity
if (trace_result.Success and trace_result.Entity) then
if (trace_result.Success and trace_result.Entity and trace_result.Entity:IsNetworkDistributed()) then
-- Cannot grab Characters (yet?), cannot grab attached entities or entities which are being grabbed
if (NanosUtils.IsA(trace_result.Entity, Character) or trace_result.Entity:GetAttachedTo() or trace_result.Entity:GetValue("IsBeingGrabbed")) then
return end_location
Expand All @@ -119,7 +119,7 @@ function TryPickUpObject()

-- Spawns a 'graviting' sound attached to the gravited object
PhysicsGun.grabbed_sound = Sound(Vector(), "nanos-world::A_VR_Object_Grabbed_Loop", false, false, SoundType.SFX, 0.25)
PhysicsGun.grabbed_sound:AttachTo(PhysicsGun.picking_object)
PhysicsGun.grabbed_sound:AttachTo(PhysicsGun.picking_object, AttachmentRule.SnapToTarget, "", 0)

-- Calculates the offset of the hit and the center of the object
PhysicsGun.picking_object_relative_location = PhysicsGun.picking_object:GetRotation():RotateVector(PhysicsGun.picking_object:GetLocation() - trace_result.Location)
Expand Down
2 changes: 1 addition & 1 deletion Client/Tools/Remover.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function HandleRemoverTool(weapon)
local trace_result = TraceFor(5000, CollisionChannel.WorldStatic | CollisionChannel.WorldDynamic | CollisionChannel.PhysicsBody | CollisionChannel.Vehicle)

-- If hit an object, calls the server to destroy it
if (trace_result.Success and trace_result.Entity and not NanosUtils.IsA(trace_result.Entity, Character)) then
if (trace_result.Success and trace_result.Entity and not NanosUtils.IsA(trace_result.Entity, Character) and trace_result.Entity:IsNetworkDistributed()) then
Events.CallRemote("DestroyItem", trace_result.Entity)
else
-- If didn't hit anything, plays a negative sound
Expand Down
2 changes: 1 addition & 1 deletion Client/Tools/Resizer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function ResizerPullUse(weapon, shooter)
local trace_result = TraceFor(10000, CollisionChannel.WorldStatic | CollisionChannel.WorldDynamic | CollisionChannel.PhysicsBody | CollisionChannel.Vehicle | CollisionChannel.Pawn)

-- If hit an object, then sets this object to be the "resized" one
if (trace_result.Success and trace_result.Entity) then
if (trace_result.Success and trace_result.Entity and trace_result.Entity:IsNetworkDistributed()) then
ResizerTool.resizing_object = trace_result.Entity
ResizerTool.current_scale = ResizerTool.resizing_object:GetScale()
ResizerTool.resizing_object:SetHighlightEnabled(true, 0)
Expand Down
2 changes: 1 addition & 1 deletion Client/Tools/Rope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function HandleRopeTool(tool)
return

-- If is not yet attached to start
elseif (trace_result.Entity) then
elseif (trace_result.Entity and trace_result.Entity:IsNetworkDistributed()) then
RopeTool.attaching_start_to = trace_result.Entity
RopeTool.attaching_start_location = trace_result.Location

Expand Down
2 changes: 1 addition & 1 deletion Client/Tools/Thruster.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function HandleThrusterTool(weapon)
local trace_result = TraceFor(10000, CollisionChannel.WorldStatic | CollisionChannel.WorldDynamic | CollisionChannel.PhysicsBody | CollisionChannel.Vehicle)

-- If hit some object, then spawns a thruster on attached it
if (trace_result.Success and trace_result.Entity and not NanosUtils.IsA(trace_result.Entity, Character)) then
if (trace_result.Success and trace_result.Entity and not NanosUtils.IsA(trace_result.Entity, Character) and trace_result.Entity:IsNetworkDistributed()) then
Events.CallRemote("SpawnThruster", trace_result.Location, trace_result.Normal, trace_result.Entity)
else
-- If didn't hit anything, plays a negative sound
Expand Down
2 changes: 1 addition & 1 deletion Client/Tools/Trail.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function HandleTrailTool(weapon)
local trace_result = TraceFor(10000, CollisionChannel.WorldStatic | CollisionChannel.WorldDynamic | CollisionChannel.PhysicsBody | CollisionChannel.Vehicle | CollisionChannel.Pawn)

-- If hit some object, then spawns a trail on attached it
if (trace_result.Success and trace_result.Entity) then
if (trace_result.Success and trace_result.Entity and trace_result.Entity:IsNetworkDistributed()) then
Events.CallRemote("SpawnTrail", trace_result.Location, trace_result.Normal, trace_result.Entity)
else
-- If didn't hit anything, plays a negative sound
Expand Down
2 changes: 1 addition & 1 deletion Client/Tools/Weld.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function HandleWeldTool(tool)
return

-- If is not yet attached to start
elseif (trace_result.Entity) then
elseif (trace_result.Entity and trace_result.Entity:IsNetworkDistributed()) then
WeldTool.welding_start_to = trace_result.Entity

-- Enable Highlighting on index 0
Expand Down
2 changes: 1 addition & 1 deletion Package.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# contributors
author = "nanos™"
# version
version = "2.10.1"
version = "2.10.2"
# image URL
image = "https://i.imgur.com/aoEa4N4.jpg"
# package type: 'script' (normal package), 'game-mode' (unique package - can only load one at a time) or 'loading-screen' (special package loaded in loading screen)
Expand Down
3 changes: 2 additions & 1 deletion Server/Index.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ MALE_DEATH_SOUNDS = {
"nanos-world::A_Male_03_Death",
"nanos-world::A_Male_04_Death",
"nanos-world::A_Male_05_Death",
"nanos-world::A_Male_06_Death"
"nanos-world::A_Male_06_Death",
"nanos-world::A_Wilhelm_Scream"
}

-- List of Pain Male voices
Expand Down

0 comments on commit cf697d5

Please sign in to comment.