From 2b2e4131a5c787f5d36e9d8b2332666f74dbf5b0 Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Mon, 14 Oct 2024 02:27:40 -0400 Subject: [PATCH] fix(Reward): Simplified and better comments --- Assets/Mirror/Examples/Room/Scripts/Reward.cs | 39 ++++++++----------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/Assets/Mirror/Examples/Room/Scripts/Reward.cs b/Assets/Mirror/Examples/Room/Scripts/Reward.cs index 1e1fd9dcc..de715a7c3 100644 --- a/Assets/Mirror/Examples/Room/Scripts/Reward.cs +++ b/Assets/Mirror/Examples/Room/Scripts/Reward.cs @@ -24,34 +24,29 @@ protected override void OnValidate() [ServerCallback] void OnTriggerEnter(Collider other) { - if (other.gameObject.CompareTag("Player")) - ClaimPrize(other.gameObject); - } + // Set up physics layers to prevent this from being called by non-players + // and eliminate the need for a tag check here. + if (!other.CompareTag("Player")) return; - [ServerCallback] - void ClaimPrize(GameObject player) - { - if (available) - { - // This is a fast switch to prevent two players claiming the prize in a bang-bang close contest for it. - // First hit turns it off, pending the object being destroyed a few frames later. + // This is a fast switch to prevent two players claiming the prize in a bang-bang close contest for it. + // First to trigger turns it off, pending the object being destroyed a few frames later. + if (!available) + return; + else available = false; - Color32 color = randomColor.color; + // Calculate the points from the color...lighter scores higher as the average approaches 255 + // UnityEngine.Color RGB values are byte 0 to 255 + uint points = (uint)((randomColor.color.r + randomColor.color.g + randomColor.color.b) / 3); - // calculate the points from the color ... lighter scores higher as the average approaches 255 - // UnityEngine.Color RGB values are float fractions of 255 - uint points = (uint)(((color.r) + (color.g) + (color.b)) / 3); + // award the points via SyncVar on Player's PlayerScore + other.GetComponent().score += points; - // award the points via SyncVar on the PlayerController - player.GetComponent().score += points; + // spawn a replacement + Spawner.SpawnReward(); - // spawn a replacement - Spawner.SpawnReward(); - - // destroy this one - NetworkServer.Destroy(gameObject); - } + // destroy this one + NetworkServer.Destroy(gameObject); } } }