Skip to content

Commit

Permalink
Fixed Physics Gun not grabbing objects from other net. authority play…
Browse files Browse the repository at this point in the history
…ers.

Fixed deprecated MouseMove and Scroll events.
ResizerGun plays error sound if in size limits.
Sky interval default is now 5 minutes.
Spawn Menu doesnt auto enter vehicles anymore.
  • Loading branch information
gtnardy committed Apr 10, 2023
1 parent 99547d5 commit e8a7601
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 99 deletions.
107 changes: 56 additions & 51 deletions Client/Tools/PhysicsGun.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ PhysicsGun.picking_object_snapped_moved = 0
PhysicsGun.is_rotating_object = false
PhysicsGun.is_snapping_to_grid = false
PhysicsGun.is_using = false
PhysicsGun.is_picking_object_server = false
PhysicsGun.grabbed_sound = nil
PhysicsGun.accumulated_rotation_x = 0
PhysicsGun.accumulated_rotation_y = 0
Expand Down Expand Up @@ -67,9 +68,8 @@ function PhysicsGun:OnLocalPlayerPickUp(character)
Input.Subscribe("KeyPress", PhysicsGunKeyPress)
Input.Subscribe("KeyDown", PhysicsGunKeyDown)
Input.Subscribe("MouseDown", PhysicsGunMouseDown)
Input.Subscribe("MouseUp", PhysicsGunMouseUp)
Input.Subscribe("MouseMoveX", PhysicsGunMouseMoveX)
Input.Subscribe("MouseMoveY", PhysicsGunMouseMoveY)
Input.Subscribe("MouseScroll", PhysicsGunMouseScroll)
Input.Subscribe("MouseMove", PhysicsGunMouseMove)

PhysicsGun.tick_timer = Timer.SetInterval(PhysicsGunTick, 0.033)

Expand All @@ -89,9 +89,8 @@ function PhysicsGun:OnLocalPlayerDrop(character)
Input.Unsubscribe("KeyPress", PhysicsGunKeyPress)
Input.Unsubscribe("KeyDown", PhysicsGunKeyDown)
Input.Unsubscribe("MouseDown", PhysicsGunMouseDown)
Input.Unsubscribe("MouseUp", PhysicsGunMouseUp)
Input.Unsubscribe("MouseMoveX", PhysicsGunMouseMoveX)
Input.Unsubscribe("MouseMoveY", PhysicsGunMouseMoveY)
Input.Unsubscribe("MouseScroll", PhysicsGunMouseScroll)
Input.Unsubscribe("MouseMove", PhysicsGunMouseMove)

Timer.ClearInterval(PhysicsGun.tick_timer)

Expand Down Expand Up @@ -125,9 +124,23 @@ function PhysicsGunWeaponAimModeChanged(character, old_state, new_state)
end
end

Events.SubscribeRemote("PickUpObject", function(object, is_grabbing)
function PhysicsGun:OnPickUpObject(object, is_grabbing)
object:SetOutlineEnabled(is_grabbing, 2)
end)
self:OnToggleTargetParticles(not is_grabbing)

-- Spawns a sound for grabbing/ungrabbing it
if (is_grabbing) then
Sound(object:GetLocation(), "nanos-world::A_VR_Grab", false, true, SoundType.SFX, 0.25, 0.9)
else
Sound(object:GetLocation(), "nanos-world::A_VR_Ungrab", false, true, SoundType.SFX, 0.25, 0.9)
end

if (self == PhysicsGun.weapon) then
PhysicsGun.is_picking_object_server = is_grabbing
end
end

PhysicsGun.SubscribeRemote("PickUpObject", PhysicsGun.OnPickUpObject)

-- Function to handle when I'm using a Physics Gun
function TogglePhysicsGunLocal(is_using, freeze)
Expand All @@ -152,6 +165,7 @@ function TogglePhysicsGunLocal(is_using, freeze)

PhysicsGun.is_rotating_object = false
PhysicsGun.is_snapping_to_grid = false
PhysicsGun.is_picking_object_server = false
end

PhysicsGun.is_using = is_using
Expand Down Expand Up @@ -272,63 +286,48 @@ function PhysicsGunMouseDown(key_name)
end
end

function PhysicsGunMouseUp(key_name, a, b)
-- Scrolling will or move the object to far
if (key_name == "MouseScrollUp") then
-- If mouse scroll, updates the Distance of the object from the camera
PhysicsGun.picking_object_distance = PhysicsGun.picking_object_distance + PhysicsGun.picking_object_distance * 0.1
return
end
function PhysicsGunMouseScroll(mouse_x, mouse_y, delta)
-- If mouse scroll, updates the Distance of the object from the camera
PhysicsGun.picking_object_distance = PhysicsGun.picking_object_distance + PhysicsGun.picking_object_distance * 0.1 * delta

-- Scrolling will or move the object to far
if (key_name == "MouseScrollDown") then
-- If mouse scroll, updates the PhysicsGun.picking_object_distance of the object from the camera
PhysicsGun.picking_object_distance = PhysicsGun.picking_object_distance - PhysicsGun.picking_object_distance * 0.1

-- Cannot scroll too close
if (PhysicsGun.picking_object_distance < 100) then PhysicsGun.picking_object_distance = 100 end
return
-- Cannot scroll too close
if (delta < 0 and PhysicsGun.picking_object_distance < 100) then
PhysicsGun.picking_object_distance = 100
end
end

function PhysicsGunMouseMoveX(delta, delta_time, num_samples)
function PhysicsGunMouseMove(delta_x, delta_y, mouse_x, mouse_y)
if (not PhysicsGun.picking_object) then return end

if (PhysicsGun.is_rotating_object) then
if (PhysicsGun.is_snapping_to_grid) then
PhysicsGun.accumulated_rotation_x = PhysicsGun.accumulated_rotation_x + delta * 0.005
if (math.abs(PhysicsGun.accumulated_rotation_x) > 1) then
local diff = PhysicsGun.accumulated_rotation_x > 0 and PhysicsGun.quaternion_rotate_right or PhysicsGun.quaternion_rotate_left
PhysicsGun.accumulated_rotation_x = 0
PhysicsGun.picking_object_initial_rotation = (diff * PhysicsGun.picking_object_initial_rotation:Quaternion()):Rotator()
-- Accumulates rotation
PhysicsGun.accumulated_rotation_x = PhysicsGun.accumulated_rotation_x + delta_x * 0.005
PhysicsGun.accumulated_rotation_y = PhysicsGun.accumulated_rotation_y - delta_y * 0.005

Sound(PhysicsGun.picking_object:GetLocation(), "nanos-world::A_Object_Snaps_To_Grid", false, true, SoundType.SFX, 0.05, 0.5)
end
else
PhysicsGun.quaternion_mouse_move_x.Z = 0.001 * delta
PhysicsGun.picking_object_initial_rotation = (PhysicsGun.quaternion_mouse_move_x * PhysicsGun.picking_object_initial_rotation:Quaternion()):Rotator()
end
local has_accumulated_enough_x = math.abs(PhysicsGun.accumulated_rotation_x) > 1
local has_accumulated_enough_y = math.abs(PhysicsGun.accumulated_rotation_y) > 1

return false
end
end

function PhysicsGunMouseMoveY(delta, delta_time, num_samples)
if (not PhysicsGun.picking_object) then return end
if (has_accumulated_enough_x or has_accumulated_enough_y) then
Sound(PhysicsGun.picking_object:GetLocation(), "nanos-world::A_Object_Snaps_To_Grid", false, true, SoundType.SFX, 0.05, 0.5)

if (PhysicsGun.is_rotating_object) then
if (PhysicsGun.is_snapping_to_grid) then
PhysicsGun.accumulated_rotation_y = PhysicsGun.accumulated_rotation_y + delta * 0.005
if (math.abs(PhysicsGun.accumulated_rotation_y) > 1) then
local diff = PhysicsGun.accumulated_rotation_y > 0 and PhysicsGun.quaternion_rotate_front or PhysicsGun.quaternion_rotate_back
PhysicsGun.accumulated_rotation_y = 0
PhysicsGun.picking_object_initial_rotation = (diff * PhysicsGun.picking_object_initial_rotation:Quaternion()):Rotator()
if (has_accumulated_enough_x) then
local diff = PhysicsGun.accumulated_rotation_x > 0 and PhysicsGun.quaternion_rotate_right or PhysicsGun.quaternion_rotate_left
PhysicsGun.accumulated_rotation_x = 0
PhysicsGun.picking_object_initial_rotation = (diff * PhysicsGun.picking_object_initial_rotation:Quaternion()):Rotator()
end

Sound(PhysicsGun.picking_object:GetLocation(), "nanos-world::A_Object_Snaps_To_Grid", false, true, SoundType.SFX, 0.05, 0.5)
if (has_accumulated_enough_y) then
local diff = PhysicsGun.accumulated_rotation_y > 0 and PhysicsGun.quaternion_rotate_front or PhysicsGun.quaternion_rotate_back
PhysicsGun.accumulated_rotation_y = 0
PhysicsGun.picking_object_initial_rotation = (diff * PhysicsGun.picking_object_initial_rotation:Quaternion()):Rotator()
end
end
else
PhysicsGun.quaternion_mouse_move_y.Y = 0.001 * delta
PhysicsGun.picking_object_initial_rotation = (PhysicsGun.quaternion_mouse_move_y * PhysicsGun.picking_object_initial_rotation:Quaternion()):Rotator()
PhysicsGun.quaternion_mouse_move_x.Z = 0.001 * delta_x
PhysicsGun.quaternion_mouse_move_y.Y = 0.001 * -delta_y

PhysicsGun.picking_object_initial_rotation = (PhysicsGun.quaternion_mouse_move_y * PhysicsGun.quaternion_mouse_move_x * PhysicsGun.picking_object_initial_rotation:Quaternion()):Rotator()
end

return false
Expand Down Expand Up @@ -371,6 +370,7 @@ Client.Subscribe("Tick", function(delta_time)

beam_particle:SetParameterVector("BeamEnd", end_location)

-- TODO move target particle to a new Physics Gun beam particle asset
local target_particle = physics_gun.target_particle
if (target_particle and target_particle:IsValid()) then
target_particle:SetLocation(end_location)
Expand Down Expand Up @@ -400,6 +400,11 @@ function PhysicsGunTick()
if (PhysicsGun.picking_object == nil) then return end
end

-- If server didn't confirm yet, skips
if (not PhysicsGun.is_picking_object_server) then
return
end

-- If lost network authority somehow, stops gravitating it
if (not PhysicsGun.picking_object:HasNetworkAuthority()) then
TogglePhysicsGunLocal(false)
Expand Down
42 changes: 14 additions & 28 deletions Client/Tools/ResizerGun.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function ResizerGun:OnLocalPlayerPickUp(character)

self:Subscribe("ReleaseUse", ResizerGun.OnReleaseUse)
character:Subscribe("WeaponAimModeChange", ResizerGunWeaponAimModeChanged)
Input.Subscribe("MouseUp", ResizerGunMouseUp)
Input.Subscribe("MouseScroll", ResizerGunMouseScroll)
Input.Subscribe("KeyPress", ResizerGunKeyPress)

-- Sets some notification when grabbing the Tool
Expand All @@ -63,7 +63,7 @@ function ResizerGun:OnLocalPlayerDrop(character)

self:Unsubscribe("ReleaseUse", ResizerGun.OnReleaseUse)
character:Unsubscribe("WeaponAimModeChange", ResizerGunWeaponAimModeChanged)
Input.Unsubscribe("MouseUp", ResizerGunMouseUp)
Input.Unsubscribe("MouseScroll", ResizerGunMouseScroll)
Input.Unsubscribe("KeyPress", ResizerGunKeyPress)

if (ResizerGun.resizing_object) then
Expand All @@ -81,38 +81,24 @@ function ResizerGunWeaponAimModeChanged(character, old_state, new_state)
end
end

function ResizerGunMouseUp(key_name)
if (not ResizerGun.weapon) then return end
function ResizerGunMouseScroll(mouse_x, mouse_y, delta)
if (not ResizerGun.weapon or not ResizerGun.resizing_object) then return end

-- Scrolls up to increase the scale
if (key_name == "MouseScrollUp") then
if (ResizerGun.resizing_object) then
ResizerGun.current_scale = ResizerGun.current_scale + ResizerGun.current_scale * 0.1

-- Cannot resize too big
if (ResizerGun.current_scale.X > 20) then
ResizerGun.current_scale = Vector(20)
end
ResizerGun.current_scale = ResizerGun.current_scale + ResizerGun.current_scale * 0.1 * delta

ResizerGun.weapon:CallRemoteEvent("ResizeObject", ResizerGun.resizing_object, ResizerGun.current_scale, true)
-- Cannot resize too big or too small
if (delta > 0) then
if (ResizerGun.current_scale.X > 20) then
ResizerGun.current_scale = Vector(20)
end
return
end

-- Scrolls down to dencrease the scale
if (key_name == "MouseScrollDown") then
if (ResizerGun.resizing_object) then
ResizerGun.current_scale = ResizerGun.current_scale - ResizerGun.current_scale * 0.1

-- Cannot resize too small
if (ResizerGun.current_scale.X < 0.1) then
ResizerGun.current_scale = Vector(0.1)
end

ResizerGun.weapon:CallRemoteEvent("ResizeObject", ResizerGun.resizing_object, ResizerGun.current_scale, false)
elseif (delta < 0) then
if (ResizerGun.current_scale.X < 0.1) then
ResizerGun.current_scale = Vector(0.1)
end
return
end

ResizerGun.weapon:CallRemoteEvent("ResizeObject", ResizerGun.resizing_object, ResizerGun.current_scale, true)
end

function ResizerGunKeyPress(key_name)
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 = "3.5.1"
version = "3.6.0"

# game-mode configurations
[game_mode]
Expand Down
2 changes: 1 addition & 1 deletion Server/Sky.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
SandboxSky = {
current_weather = 5, -- PartlyCloudy
probability_change_weather = 0.5, -- 50% of chance
weather_change_check_interval = 120000, -- 2 minutes
weather_change_check_interval = 300000, -- 5 minutes
weather_transition_time_min = 20,
weather_transition_time_max = 40,
}
Expand Down
6 changes: 1 addition & 5 deletions Server/SpawnMenu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ Events.SubscribeRemote("SpawnItem", function(player, tab, id, spawn_location, sp
local character = player:GetControlledCharacter()

if (tab == "vehicles") then
spawn_location = character:GetLocation() + Vector(0, 0, 50)
spawn_rotation = character:GetRotation()
spawn_rotation.Yaw = spawn_rotation.Yaw - 90
elseif (tab == "tools" or tab == "weapons") then
spawn_location = character:GetLocation()
end
Expand Down Expand Up @@ -59,9 +58,6 @@ Events.SubscribeRemote("SpawnItem", function(player, tab, id, spawn_location, sp
if (selected_option ~= "") then
ApplyWeaponPattern(item, selected_option)
end
elseif (tab == "vehicles") then
-- Enters the Character
character:EnterVehicle(item, 0)
elseif (item:IsA(Melee) or item:IsA(Grenade)) then
-- Destroys the current picked up item
local current_picking_weapon = character:GetPicked()
Expand Down
17 changes: 4 additions & 13 deletions Server/Tools/PhysicsGun.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,20 @@ end

function PhysicsGun:OnPickUpObject(player, object, is_grabbing, picking_object_relative_location, freeze)
if (is_grabbing) then
-- Forces it not being network authority distributed while someone is grabbing it
object:SetNetworkAuthorityAutoDistributed(false)

-- Only updates the Network Authority if this entity is network distributed
if (object:IsNetworkDistributed()) then
object:SetNetworkAuthority(player)
end

-- Forces it not being network authority distributed while someone is grabbing it
object:SetNetworkAuthorityAutoDistributed(false)

object:SetValue("IsBeingGrabbed", true, true)

-- Sets the particle values so all Clients can set the correct position of them
self.beam_particle:SetValue("RelativeLocationObject", picking_object_relative_location, true)
self.beam_particle:SetValue("BeamEndObject", object, true)

-- Spawns a sound for grabbing it
Events.BroadcastRemote("SpawnSound", object:GetLocation(), "nanos-world::A_VR_Grab", false, 0.25, 0.9)

self:BroadcastRemoteEvent("ToggleTargetParticles", false)
else
-- Restores auto network authority distribution of this object
object:SetNetworkAuthorityAutoDistributed(true)
Expand All @@ -46,11 +42,6 @@ function PhysicsGun:OnPickUpObject(player, object, is_grabbing, picking_object_r
-- Resets particle values
self.beam_particle:SetValue("RelativeLocationObject", nil, true)
self.beam_particle:SetValue("BeamEndObject", nil, true)

-- Spawns a sound for ungrabbing it
Events.BroadcastRemote("SpawnSound", object:GetLocation(), "nanos-world::A_VR_Ungrab", false, 0.25, 0.9)

self:BroadcastRemoteEvent("ToggleTargetParticles", true)
end

-- Disables/Enables the gravity of the object so he can 'fly' freely
Expand All @@ -63,7 +54,7 @@ function PhysicsGun:OnPickUpObject(player, object, is_grabbing, picking_object_r
-- Disables/Enables the character to Aim, so he can use the Mouse Wheel properly
player:GetControlledCharacter():SetCanAim(not is_grabbing)

Events.BroadcastRemote("PickUpObject", object, is_grabbing)
self:BroadcastRemoteEvent("PickUpObject", object, is_grabbing)
end

function PhysicsGun:OnToggle(player, enable)
Expand Down
5 changes: 5 additions & 0 deletions Server/Tools/ResizerGun.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ function ResizerGun:Constructor(location, rotation)
end

function ResizerGun:OnResizeObject(player, object, scale, up)
if (object:GetScale():Equals(scale, 0.001)) then
Events.BroadcastRemote("SpawnSound", object:GetLocation(), "nanos-world::A_Invalid_Action", false, 1, 1)
return
end

object:SetScale(scale)
Events.BroadcastRemote("SpawnSound", object:GetLocation(), "nanos-world::A_Object_Snaps_To_Grid", false, 1, up and 0.9 or 0.8)
end
Expand Down

0 comments on commit e8a7601

Please sign in to comment.