From a021e621b9580274d95864af3dee2a0334b8466a Mon Sep 17 00:00:00 2001 From: excitedneon Date: Fri, 12 May 2017 02:03:27 +0300 Subject: [PATCH] Implement InteractionTypes --- Assets/Scenes/TestMap.unity | 32 ++++++++++++++--- Assets/Scripts/Controls/PlayerController.cs | 34 ++++++++++++++----- Assets/Scripts/Entities/InteractionType.cs | 2 +- Assets/Scripts/Entities/SyncBases/Button.cs | 29 ++++++++++++---- Assets/Scripts/Entities/SyncBases/Computer.cs | 20 ++++++----- Assets/Scripts/Entities/SyncBases/Door.cs | 6 ++-- .../Entities/SyncBases/Interactable.cs | 2 +- .../Scripts/Networking/Clientside/Client.cs | 4 ++- .../Scripts/Networking/Serverside/Server.cs | 2 +- .../{Entities/SyncBases => Util}/Hologram.cs | 33 +++++------------- .../SyncBases => Util}/Hologram.cs.meta | 0 11 files changed, 106 insertions(+), 58 deletions(-) rename Assets/Scripts/{Entities/SyncBases => Util}/Hologram.cs (55%) rename Assets/Scripts/{Entities/SyncBases => Util}/Hologram.cs.meta (100%) diff --git a/Assets/Scenes/TestMap.unity b/Assets/Scenes/TestMap.unity index 23d9c40..daffe5a 100644 --- a/Assets/Scenes/TestMap.unity +++ b/Assets/Scenes/TestMap.unity @@ -1713,7 +1713,7 @@ Prefab: objectReference: {fileID: 0} - target: {fileID: 4689370038551000, guid: 3da42b42c71f841c2a977cfb5255b00f, type: 2} propertyPath: m_LocalRotation.y - value: 0 + value: 1 objectReference: {fileID: 0} - target: {fileID: 4689370038551000, guid: 3da42b42c71f841c2a977cfb5255b00f, type: 2} propertyPath: m_LocalRotation.z @@ -1721,7 +1721,7 @@ Prefab: objectReference: {fileID: 0} - target: {fileID: 4689370038551000, guid: 3da42b42c71f841c2a977cfb5255b00f, type: 2} propertyPath: m_LocalRotation.w - value: 1 + value: 0 objectReference: {fileID: 0} - target: {fileID: 4689370038551000, guid: 3da42b42c71f841c2a977cfb5255b00f, type: 2} propertyPath: m_RootOrder @@ -1744,6 +1744,20 @@ Prefab: propertyPath: m_LocalScale.z value: 0.75 objectReference: {fileID: 0} + - target: {fileID: 114171175549544602, guid: 3da42b42c71f841c2a977cfb5255b00f, + type: 2} + propertyPath: TextProperties.Text + value: \n\n Toggle Door + objectReference: {fileID: 0} + - target: {fileID: 4689370038551000, guid: 3da42b42c71f841c2a977cfb5255b00f, type: 2} + propertyPath: m_LocalEulerAnglesHint.y + value: 180 + objectReference: {fileID: 0} + - target: {fileID: 114171175549544602, guid: 3da42b42c71f841c2a977cfb5255b00f, + type: 2} + propertyPath: TextProperties.FontSize + value: 36 + objectReference: {fileID: 0} m_RemovedComponents: [] m_ParentPrefab: {fileID: 100100000, guid: 3da42b42c71f841c2a977cfb5255b00f, type: 2} m_IsPrefabParent: 0 @@ -2005,7 +2019,7 @@ Prefab: - target: {fileID: 114327153796435004, guid: 660bef6a67fed4941b963e96ff20a5e0, type: 2} propertyPath: WillTrigger.Array.size - value: 2 + value: 1 objectReference: {fileID: 0} - target: {fileID: 4974430339442808, guid: 660bef6a67fed4941b963e96ff20a5e0, type: 2} propertyPath: m_LocalPosition.x @@ -2069,12 +2083,17 @@ Prefab: type: 2} propertyPath: WillTrigger.Array.data[0] value: - objectReference: {fileID: 865482261} + objectReference: {fileID: 310461193} - target: {fileID: 114327153796435004, guid: 660bef6a67fed4941b963e96ff20a5e0, type: 2} propertyPath: WillTrigger.Array.data[1] value: objectReference: {fileID: 310461193} + - target: {fileID: 114327153796435004, guid: 660bef6a67fed4941b963e96ff20a5e0, + type: 2} + propertyPath: Hologram + value: + objectReference: {fileID: 865482261} m_RemovedComponents: [] m_ParentPrefab: {fileID: 100100000, guid: 660bef6a67fed4941b963e96ff20a5e0, type: 2} m_IsPrefabParent: 0 @@ -2576,6 +2595,11 @@ Prefab: propertyPath: m_RootOrder value: 0 objectReference: {fileID: 0} + - target: {fileID: 114171175549544602, guid: 3da42b42c71f841c2a977cfb5255b00f, + type: 2} + propertyPath: TextProperties.Text + value: + objectReference: {fileID: 0} m_RemovedComponents: [] m_ParentPrefab: {fileID: 100100000, guid: 3da42b42c71f841c2a977cfb5255b00f, type: 2} m_IsPrefabParent: 0 diff --git a/Assets/Scripts/Controls/PlayerController.cs b/Assets/Scripts/Controls/PlayerController.cs index 3cbd41f..7e18d07 100644 --- a/Assets/Scripts/Controls/PlayerController.cs +++ b/Assets/Scripts/Controls/PlayerController.cs @@ -26,6 +26,7 @@ namespace Cyber.Controls { private CursorHandler CursorHandler; private Vector3 Rotation; + private GameObject LastLookedAt; private void Start() { CursorHandler = GameObject.Find("/Systems/CursorHandler").GetComponent(); @@ -50,17 +51,27 @@ namespace Cyber.Controls { Character.SetRotation(Rotation); // Interactions - if (Input.GetButtonDown("Activate")) { - GameObject LookedAtObject = GetLookedAtGameObject(); - if (LookedAtObject != null) { - Interactable LookingAt = LookedAtObject.GetComponent(); - if (LookingAt != null && (LookingAt.transform.position - Character.GetPosition()).magnitude < Character.InteractionDistance) { - LookingAt.Interact(Character); - if (LookingAt.GetInteractableSyncdata().PublicInteractions) { - Client.Send(PktType.Interact, new InteractionPkt(LookingAt.ID, InteractionType.Press)); + GameObject LookedAtObject = GetLookedAtGameObject(); + if (LookedAtObject != null) { + Interactable LookingAt = LookedAtObject.GetComponent(); + if (LookingAt != null && (LookingAt.transform.position - Character.GetPosition()).magnitude < Character.InteractionDistance) { + if (Input.GetButtonDown("Activate")) { + InteractWith(LookingAt, InteractionType.Activate); + } + if (Input.GetButtonUp("Activate")) { + InteractWith(LookingAt, InteractionType.Deactivate); + } + if (LookedAtObject != LastLookedAt) { + InteractWith(LookingAt, InteractionType.Enter); + if (LastLookedAt != null) { + InteractWith(LastLookedAt.GetComponent(), InteractionType.Exit); } } + LastLookedAt = LookedAtObject; } + } else if (LastLookedAt != null) { + InteractWith(LastLookedAt.GetComponent(), InteractionType.Exit); + LastLookedAt = null; } } else if (Character.Moving()) { // The debug console is open, stop the player. @@ -68,6 +79,13 @@ namespace Cyber.Controls { } } + private void InteractWith(Interactable interactable, InteractionType type) { + interactable.Interact(Character, type); + if (interactable.GetInteractableSyncdata().PublicInteractions) { + Client.Send(PktType.Interact, new InteractionPkt(interactable.ID, type)); + } + } + private GameObject GetLookedAtGameObject() { RaycastHit Hit; Physics.Raycast(Camera.transform.position, Camera.transform.forward, out Hit, Character.InteractionDistance); diff --git a/Assets/Scripts/Entities/InteractionType.cs b/Assets/Scripts/Entities/InteractionType.cs index 3d20b64..1416039 100644 --- a/Assets/Scripts/Entities/InteractionType.cs +++ b/Assets/Scripts/Entities/InteractionType.cs @@ -6,7 +6,7 @@ namespace Cyber.Entities { /// public enum InteractionType : byte { - Press, Release, Enter, Exit + Activate, Deactivate, Enter, Exit } } diff --git a/Assets/Scripts/Entities/SyncBases/Button.cs b/Assets/Scripts/Entities/SyncBases/Button.cs index a6f2991..32b058d 100644 --- a/Assets/Scripts/Entities/SyncBases/Button.cs +++ b/Assets/Scripts/Entities/SyncBases/Button.cs @@ -5,6 +5,7 @@ using UnityEngine.Networking; using Cyber.Networking; using Cyber.Console; using System; +using Cyber.Util; namespace Cyber.Entities.SyncBases { @@ -18,6 +19,12 @@ namespace Cyber.Entities.SyncBases { /// public Interactable[] WillTrigger; + /// + /// The informational hologram that will pop up when the player hovers + /// over this button. + /// + public Hologram Hologram; + /// /// The button mesh that will blink. /// @@ -46,14 +53,22 @@ namespace Cyber.Entities.SyncBases { /// for seconds, and calls Interact /// on the . /// - public override void Interact(SyncBase Trigger) { - BlinkTime = Time.time; - if (WillTrigger.Length > 0) { - foreach (Interactable Triggerable in WillTrigger) { - Triggerable.Interact(this); + public override void Interact(SyncBase Trigger, InteractionType type) { + if (type == InteractionType.Activate) { + BlinkTime = Time.time; + if (WillTrigger.Length > 0) { + foreach (Interactable Triggerable in WillTrigger) { + Triggerable.Interact(this, InteractionType.Activate); + } + } else { + Term.Println("FIXME: The button '" + gameObject.name + "' was pressed, but it doesn't have anything to trigger."); + } + } else if (Hologram != null) { + if (type == InteractionType.Enter) { + Hologram.Visible = true; + } else if (type == InteractionType.Exit) { + Hologram.Visible = false; } - } else { - Term.Println("FIXME: The button '" + gameObject.name + "' was pressed, but it doesn't have anything to trigger."); } } diff --git a/Assets/Scripts/Entities/SyncBases/Computer.cs b/Assets/Scripts/Entities/SyncBases/Computer.cs index e02a7cd..4e7466d 100644 --- a/Assets/Scripts/Entities/SyncBases/Computer.cs +++ b/Assets/Scripts/Entities/SyncBases/Computer.cs @@ -56,15 +56,17 @@ namespace Cyber.Entities.SyncBases { /// /// Determines the keycode given to the /// . - public override void Interact(SyncBase Trigger) { - Hologram.Visible = true; - if (Trigger == KeyLeft) { - Screen.SetTextProperties(new TextTextureProperties("\n Pressed left!")); - } else if (Trigger == KeyRight) { - Screen.SetTextProperties(new TextTextureProperties("\n Pressed right!")); - } else { - Screen.SetTextProperties(new TextTextureProperties("")); - Hologram.Visible = false; + public override void Interact(SyncBase trigger, InteractionType type) { + if (type == InteractionType.Activate) { + Hologram.Visible = true; + if (trigger == KeyLeft) { + Screen.SetTextProperties(new TextTextureProperties("\n Pressed left!")); + } else if (trigger == KeyRight) { + Screen.SetTextProperties(new TextTextureProperties("\n Pressed right!")); + } else { + Screen.SetTextProperties(new TextTextureProperties("")); + Hologram.Visible = false; + } } } diff --git a/Assets/Scripts/Entities/SyncBases/Door.cs b/Assets/Scripts/Entities/SyncBases/Door.cs index e7ee5f3..ead9584 100644 --- a/Assets/Scripts/Entities/SyncBases/Door.cs +++ b/Assets/Scripts/Entities/SyncBases/Door.cs @@ -23,8 +23,10 @@ namespace Cyber.Entities.SyncBases { /// /// Toggles the openness of the door. /// - public override void Interact(SyncBase Trigger) { - IsOpen = !IsOpen; + public override void Interact(SyncBase trigger, InteractionType type) { + if (type == InteractionType.Activate) { + IsOpen = !IsOpen; + } } /// diff --git a/Assets/Scripts/Entities/SyncBases/Interactable.cs b/Assets/Scripts/Entities/SyncBases/Interactable.cs index e0d8d28..cbc1339 100644 --- a/Assets/Scripts/Entities/SyncBases/Interactable.cs +++ b/Assets/Scripts/Entities/SyncBases/Interactable.cs @@ -14,7 +14,7 @@ namespace Cyber.Entities.SyncBases { /// /// All interactables should implement their interactions by overriding this. /// - public abstract void Interact(SyncBase Trigger); + public abstract void Interact(SyncBase trigger, InteractionType type); /// /// Get Interaction information about this interactible. diff --git a/Assets/Scripts/Networking/Clientside/Client.cs b/Assets/Scripts/Networking/Clientside/Client.cs index 89b28e6..23781ef 100644 --- a/Assets/Scripts/Networking/Clientside/Client.cs +++ b/Assets/Scripts/Networking/Clientside/Client.cs @@ -242,7 +242,9 @@ namespace Cyber.Networking.Clientside { SyncBase Target = Spawner.SyncDB.Get(Interaction.InteractSyncBaseID); if (Target != null && Target is Interactable) { - ((Interactable) Target).Interact(Spawner.SyncDB.Get(Interaction.OwnerSyncBaseID)); + ((Interactable) Target).Interact( + Spawner.SyncDB.Get(Interaction.OwnerSyncBaseID), + Interaction.InteractionType); } else { Term.Println("Server has sent an erroneus SyncBase ID!"); } diff --git a/Assets/Scripts/Networking/Serverside/Server.cs b/Assets/Scripts/Networking/Serverside/Server.cs index 74e8b90..3d02485 100644 --- a/Assets/Scripts/Networking/Serverside/Server.cs +++ b/Assets/Scripts/Networking/Serverside/Server.cs @@ -215,7 +215,7 @@ namespace Cyber.Networking.Serverside { Vector3 Delta = Interacted.gameObject.transform.position - Sender.gameObject.transform.position; float ServerInteractionDistance = Sender.InteractionDistance + Sender.MovementSpeed * 0.5f; if (Delta.magnitude <= ServerInteractionDistance) { - Interacted.Interact(Sender); + Interacted.Interact(Sender, Interaction.InteractionType); NetworkServer.SendToAll(PktType.Interact, Interaction); if (Interacted.GetInteractableSyncdata().RequiresSyncing) { Syncer.DirtSyncBase(Interacted.ID); diff --git a/Assets/Scripts/Entities/SyncBases/Hologram.cs b/Assets/Scripts/Util/Hologram.cs similarity index 55% rename from Assets/Scripts/Entities/SyncBases/Hologram.cs rename to Assets/Scripts/Util/Hologram.cs index 93e10e8..d11b7b3 100644 --- a/Assets/Scripts/Entities/SyncBases/Hologram.cs +++ b/Assets/Scripts/Util/Hologram.cs @@ -1,12 +1,12 @@ using UnityEngine; using Cyber.Util; -namespace Cyber.Entities.SyncBases { +namespace Cyber.Util { /// /// Helper component for hologram meshes. /// - public class Hologram : Interactable { + public class Hologram : MonoBehaviour { /// /// Whether the hologram is visible or not. @@ -21,38 +21,19 @@ namespace Cyber.Entities.SyncBases { private float CurrentScale; - public override void Interact(SyncBase Trigger) { - Visible = !Visible; - } - - public override void Deserialize(UnityEngine.Networking.NetworkReader reader) { - } - - public override void Serialize(UnityEngine.Networking.NetworkWriter writer) { - } - - public override SyncHandletype GetSyncHandletype() { - return new SyncHandletype(false, 10); - } - - public override InteractableSyncdata GetInteractableSyncdata() { - return new InteractableSyncdata(false, false); - } - private void Start() { - CurrentScale = Visible ? 1 : 0; + CurrentScale = GetTargetScale(); UpdateScale(); } private void Update() { - float Scale = Visible ? 1 : 0; - if (Scale != CurrentScale) { + if (GetTargetScale() != CurrentScale) { UpdateScale(); } } private void UpdateScale() { - float Scale = Visible ? 1 : 0; + float Scale = GetTargetScale(); CurrentScale = Mathf.Lerp(CurrentScale, Scale, 8f * Time.deltaTime); if (Mathf.Abs(Scale - CurrentScale) < 0.05) { CurrentScale = Scale; @@ -60,5 +41,9 @@ namespace Cyber.Entities.SyncBases { transform.localScale = new Vector3(CurrentScale, CurrentScale, CurrentScale); } + + private float GetTargetScale() { + return Visible ? 1 : 0; + } } } \ No newline at end of file diff --git a/Assets/Scripts/Entities/SyncBases/Hologram.cs.meta b/Assets/Scripts/Util/Hologram.cs.meta similarity index 100% rename from Assets/Scripts/Entities/SyncBases/Hologram.cs.meta rename to Assets/Scripts/Util/Hologram.cs.meta