From 3f022a975cc66ce9366f25c8a7aa8cc788c54465 Mon Sep 17 00:00:00 2001 From: excitedneon Date: Wed, 10 May 2017 18:37:58 +0300 Subject: [PATCH] Add Interactable and a simple blinking lamp to interact with --- Assets/Materials/Lamp.mat | 75 ++++++++++ Assets/Materials/Lamp.mat.meta | 9 ++ Assets/Prefabs/PC.prefab | 2 + Assets/Scenes/TestMap.unity | 133 +++++++++++++++++- Assets/Scripts/Controls/PlayerController.cs | 32 ++++- .../Scripts/Entities/SyncBases/BlinkyBox.cs | 86 +++++++++++ .../Entities/SyncBases/BlinkyBox.cs.meta | 12 ++ .../Scripts/Entities/SyncBases/Character.cs | 34 ++++- .../Entities/SyncBases/Interactable.cs | 18 +++ .../Entities/SyncBases/Interactable.cs.meta | 12 ++ ProjectSettings/InputManager.asset | 2 +- 11 files changed, 403 insertions(+), 12 deletions(-) create mode 100644 Assets/Materials/Lamp.mat create mode 100644 Assets/Materials/Lamp.mat.meta create mode 100644 Assets/Scripts/Entities/SyncBases/BlinkyBox.cs create mode 100644 Assets/Scripts/Entities/SyncBases/BlinkyBox.cs.meta create mode 100644 Assets/Scripts/Entities/SyncBases/Interactable.cs create mode 100644 Assets/Scripts/Entities/SyncBases/Interactable.cs.meta diff --git a/Assets/Materials/Lamp.mat b/Assets/Materials/Lamp.mat new file mode 100644 index 0000000..c8ecedd --- /dev/null +++ b/Assets/Materials/Lamp.mat @@ -0,0 +1,75 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!21 &2100000 +Material: + serializedVersion: 6 + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_Name: Lamp + m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0} + m_ShaderKeywords: _EMISSION + m_LightmapFlags: 1 + m_EnableInstancingVariants: 0 + m_CustomRenderQueue: -1 + stringTagMap: {} + disabledShaderPasses: [] + m_SavedProperties: + serializedVersion: 3 + m_TexEnvs: + - _BumpMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailAlbedoMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailMask: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailNormalMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _EmissionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MainTex: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MetallicGlossMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _OcclusionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _ParallaxMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + m_Floats: + - _BumpScale: 1 + - _Cutoff: 0.5 + - _DetailNormalMapScale: 1 + - _DstBlend: 0 + - _GlossMapScale: 1 + - _Glossiness: 0 + - _GlossyReflections: 1 + - _Metallic: 0 + - _Mode: 0 + - _OcclusionStrength: 1 + - _Parallax: 0.02 + - _SmoothnessTextureChannel: 0 + - _SpecularHighlights: 1 + - _SrcBlend: 1 + - _UVSec: 0 + - _ZWrite: 1 + m_Colors: + - _Color: {r: 0.022058845, g: 0.022058845, b: 0.022058845, a: 1} + - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} diff --git a/Assets/Materials/Lamp.mat.meta b/Assets/Materials/Lamp.mat.meta new file mode 100644 index 0000000..9c25f50 --- /dev/null +++ b/Assets/Materials/Lamp.mat.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 82a7d1c3c9154445794eb04c29ab2bd3 +timeCreated: 1494171362 +licenseType: Free +NativeFormatImporter: + mainObjectFileID: 2100000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Prefabs/PC.prefab b/Assets/Prefabs/PC.prefab index 707b6ab..d3d7e6a 100644 --- a/Assets/Prefabs/PC.prefab +++ b/Assets/Prefabs/PC.prefab @@ -307,6 +307,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: Character: {fileID: 114575501420754388} + Camera: {fileID: 20678872378488080} --- !u!114 &114390919699494652 MonoBehaviour: m_ObjectHideFlags: 1 @@ -332,6 +333,7 @@ MonoBehaviour: m_EditorClassIdentifier: ID: 0 MovementSpeed: 5 + InteractionDistance: 2 CharacterController: {fileID: 143869468979164672} Head: {fileID: 4883048021777868} --- !u!124 &124775686376978134 diff --git a/Assets/Scenes/TestMap.unity b/Assets/Scenes/TestMap.unity index 2b5b7a0..c5d12a2 100644 --- a/Assets/Scenes/TestMap.unity +++ b/Assets/Scenes/TestMap.unity @@ -38,7 +38,7 @@ RenderSettings: m_ReflectionIntensity: 1 m_CustomReflection: {fileID: 0} m_Sun: {fileID: 0} - m_IndirectSpecularColor: {r: 0.4474725, g: 0.49742317, b: 0.57529545, a: 1} + m_IndirectSpecularColor: {r: 0.44747335, g: 0.49742365, b: 0.57529324, a: 1} --- !u!157 &3 LightmapSettings: m_ObjectHideFlags: 0 @@ -1063,6 +1063,75 @@ Transform: m_Father: {fileID: 949094013} m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &1229079789 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + serializedVersion: 5 + m_Component: + - component: {fileID: 1229079790} + - component: {fileID: 1229079793} + - component: {fileID: 1229079791} + m_Layer: 0 + m_Name: Blinky Mesh + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &1229079790 +Transform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1229079789} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0.3, z: 0} + m_LocalScale: {x: 0.6, y: 0.6, z: 0.6} + m_Children: [] + m_Father: {fileID: 1988211392} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!23 &1229079791 +MeshRenderer: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1229079789} + m_Enabled: 1 + m_CastShadows: 1 + m_ReceiveShadows: 1 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_Materials: + - {fileID: 2100000, guid: 82a7d1c3c9154445794eb04c29ab2bd3, type: 2} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_PreserveUVs: 1 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_SelectedEditorRenderState: 3 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 +--- !u!33 &1229079793 +MeshFilter: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1229079789} + m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0} --- !u!1 &1377895204 GameObject: m_ObjectHideFlags: 0 @@ -1361,13 +1430,13 @@ Transform: m_PrefabParentObject: {fileID: 0} m_PrefabInternal: {fileID: 0} m_GameObject: {fileID: 1810916631} - m_LocalRotation: {x: 0.40528938, y: -0.33801213, z: 0.073977225, w: 0.84617704} + m_LocalRotation: {x: 0.30459416, y: -0.7348772, z: 0.2774102, w: 0.53872216} m_LocalPosition: {x: -4.039651, y: 5.292325, z: 10.772432} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 19192150} m_RootOrder: 1 - m_LocalEulerAnglesHint: {x: 47.384003, y: -49.138, z: -12.694} + m_LocalEulerAnglesHint: {x: 47.384003, y: -113.1, z: -12.694} --- !u!1 &1815899003 GameObject: m_ObjectHideFlags: 0 @@ -1488,6 +1557,64 @@ Transform: m_Father: {fileID: 344272455} m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &1988211391 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + serializedVersion: 5 + m_Component: + - component: {fileID: 1988211392} + - component: {fileID: 1988211393} + - component: {fileID: 1988211394} + m_Layer: 0 + m_Name: BlinkyBox + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &1988211392 +Transform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1988211391} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: -3, y: 1, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: + - {fileID: 1229079790} + m_Father: {fileID: 0} + m_RootOrder: 3 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &1988211393 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1988211391} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 0c07d843d1f7647df9d2fb28d9e51c51, type: 3} + m_Name: + m_EditorClassIdentifier: + ID: 0 + Mesh: {fileID: 1229079791} + BlinkLength: 1 + BlinkColor: {r: 1, g: 0.6, b: 0, a: 1} +--- !u!65 &1988211394 +BoxCollider: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1988211391} + m_Material: {fileID: 0} + m_IsTrigger: 0 + m_Enabled: 1 + serializedVersion: 2 + m_Size: {x: 0.6, y: 0.6, z: 0.6} + m_Center: {x: 0, y: 0.3, z: 0} --- !u!1 &2000206069 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Controls/PlayerController.cs b/Assets/Scripts/Controls/PlayerController.cs index ecbdafd..e7f35e6 100644 --- a/Assets/Scripts/Controls/PlayerController.cs +++ b/Assets/Scripts/Controls/PlayerController.cs @@ -18,6 +18,11 @@ namespace Cyber.Controls { /// public Character Character; + /// + /// The camera the player is seeing the world through. + /// + public Camera Camera; + private CursorHandler CursorHandler; private Vector3 Rotation; @@ -27,25 +32,48 @@ namespace Cyber.Controls { private void Update() { if (!Term.IsVisible()) { + // Don't do any "gameplay stuff" if the debug console is up + // Handle inputs + // Movement Vector3 Move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); if (Move.sqrMagnitude != 0) { Character.Move(Character.transform.TransformDirection(Move)); - Client.Send(PktType.MoveCreature, new MoveCreaturePkt(Character.transform.TransformDirection(Move), Character.ID)); } else if (Character.Moving()) { Character.Stop(); - Client.Send(PktType.MoveCreature, new MoveCreaturePkt(new Vector3(), Character.ID)); } + // Rotation Rotation.y += Input.GetAxis("Mouse X") * CursorHandler.MouseSensitivityX; Rotation.x = Mathf.Clamp(Rotation.x - Input.GetAxis("Mouse Y") * CursorHandler.MouseSensitivityY, -89, 89); 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(); + } + } + } } else if (Character.Moving()) { // The debug console is open, stop the player. Character.Stop(); } } + + private GameObject GetLookedAtGameObject() { + RaycastHit Hit; + Physics.Raycast(Camera.transform.position, Camera.transform.forward, out Hit, Character.InteractionDistance); + if (Hit.collider != null) { + return Hit.collider.gameObject; + } else { + return null; + } + } } } \ No newline at end of file diff --git a/Assets/Scripts/Entities/SyncBases/BlinkyBox.cs b/Assets/Scripts/Entities/SyncBases/BlinkyBox.cs new file mode 100644 index 0000000..c0e983c --- /dev/null +++ b/Assets/Scripts/Entities/SyncBases/BlinkyBox.cs @@ -0,0 +1,86 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.Networking; +using Cyber.Networking; +using Cyber.Console; + +namespace Cyber.Entities.SyncBases { + /// + /// + /// + public class BlinkyBox : Interactable { + + /// + /// The lamp mesh that will blink. + /// + public MeshRenderer Mesh; + + /// + /// How long the blink lasts. + /// + public float BlinkLength = 1f; + + /// + /// How bright the light is (upwards of 1 for the bloom to kick in) + /// + public float BlinkBrightness = 1.5f; + + /// + /// The color of the blink. + /// + public Color BlinkColor = new Color(1f, 0.6f, 0f); + + private double BlinkTime = 0; + private Material Material; + + /// + /// When a BlinkyBox is interacted with, it blinks once, light lasting + /// for seconds. + /// + public override void Interact() { + BlinkTime = NetworkHelper.GetCurrentSystemTime(); + } + + /// + /// Deserializes this SyncBase for further use. + /// + /// + public override void Deserialize(NetworkReader reader) { + double ServerBlinkTime = reader.ReadDouble(); + if (ServerBlinkTime > BlinkTime) { + BlinkTime = ServerBlinkTime; + } + } + + /// + /// Serialize this SyncBase into a sync packet. + /// + /// + public override void Serialize(NetworkWriter writer) { + writer.Write(BlinkTime); + } + + /// + /// The blinky boxes handletype is + /// + /// Sync Handletype containing sync information. + public override SyncHandletype GetSyncHandletype() { + return new SyncHandletype(false, 5); + } + + private void Start() { + Material = Mesh.material; + } + + private void Update() { + float Time = (float) (NetworkHelper.GetCurrentSystemTime() - BlinkTime); + if (Time < BlinkLength) { + float Brightness = (1f - Time / BlinkLength) * BlinkBrightness; + Color NewColor = new Color(Brightness * BlinkColor.r, + Brightness * BlinkColor.g, Brightness * BlinkColor.b); + Material.SetColor("_EmissionColor", NewColor); + } + } + } +} diff --git a/Assets/Scripts/Entities/SyncBases/BlinkyBox.cs.meta b/Assets/Scripts/Entities/SyncBases/BlinkyBox.cs.meta new file mode 100644 index 0000000..50e3da8 --- /dev/null +++ b/Assets/Scripts/Entities/SyncBases/BlinkyBox.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 0c07d843d1f7647df9d2fb28d9e51c51 +timeCreated: 1494425788 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Entities/SyncBases/Character.cs b/Assets/Scripts/Entities/SyncBases/Character.cs index 09c759d..c784e29 100644 --- a/Assets/Scripts/Entities/SyncBases/Character.cs +++ b/Assets/Scripts/Entities/SyncBases/Character.cs @@ -15,6 +15,11 @@ namespace Cyber.Entities.SyncBases { /// public float MovementSpeed = 5.0f; + /// + /// The interaction distance of this player. + /// + public float InteractionDistance = 2.0f; + /// /// The character controller, used to move the character. Handles collisions. /// @@ -63,6 +68,14 @@ namespace Cyber.Entities.SyncBases { transform.localEulerAngles = BodyRot; } + /// + /// Sets the position of this character. + /// + /// Position. + public void SetPosition(Vector3 Position) { + transform.position = Position; + } + /// /// Whether the player is moving or not. /// @@ -80,6 +93,14 @@ namespace Cyber.Entities.SyncBases { transform.localEulerAngles.y, Head.localEulerAngles.z); } + /// + /// Returns the position of this character. + /// + /// The position. + public Vector3 GetPosition() { + return transform.position; + } + /// /// Gets the Sync Handletype for Character, which doesn't require hash differences and syncs every tick. /// @@ -97,18 +118,19 @@ namespace Cyber.Entities.SyncBases { Vector3 ServerMovementDirection = reader.ReadVector3(); Vector3 ServerRotation = reader.ReadVector3(); - float Drift = (ServerPosition - transform.position).magnitude; + float Drift = (ServerPosition - GetPosition()).magnitude; // Update position if this is the local player - if (Drift > MovementSpeed * 0.5f && Client.GetConnectedPlayer().Character.Equals(this)) { - transform.position = ServerPosition; + Character LocalCharacter = Client.GetConnectedPlayer().Character; + if (Drift > MovementSpeed * 0.5f && LocalCharacter.Equals(this)) { + SetPosition(ServerPosition); MovementDirection = ServerMovementDirection; } // Update position more often (with lerping) if this is not the local player if (Drift < 0.1) { ServerPositionShouldLerpSync = false; - } else if (!Client.GetConnectedPlayer().Character.Equals(this)) { + } else if (!LocalCharacter.Equals(this)) { ServerPositionShouldLerpSync = true; } } @@ -118,14 +140,14 @@ namespace Cyber.Entities.SyncBases { /// /// public override void Serialize(NetworkWriter writer) { - writer.Write(transform.position); + writer.Write(GetPosition()); writer.Write(MovementDirection); writer.Write(GetRotation()); } private void Update() { if (ServerPositionShouldLerpSync) { - transform.position = Vector3.Lerp(transform.position, ServerPosition, 10f * Time.deltaTime); + SetPosition(Vector3.Lerp(GetPosition(), ServerPosition, 10f * Time.deltaTime)); } } diff --git a/Assets/Scripts/Entities/SyncBases/Interactable.cs b/Assets/Scripts/Entities/SyncBases/Interactable.cs new file mode 100644 index 0000000..4819eb1 --- /dev/null +++ b/Assets/Scripts/Entities/SyncBases/Interactable.cs @@ -0,0 +1,18 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace Cyber.Entities.SyncBases { + + /// + /// Base class for all interactible entities in the world. + /// + public abstract class Interactable : SyncBase { + + /// + /// All interactables should implement their interactions by overriding this. + /// + public abstract void Interact(); + + } +} \ No newline at end of file diff --git a/Assets/Scripts/Entities/SyncBases/Interactable.cs.meta b/Assets/Scripts/Entities/SyncBases/Interactable.cs.meta new file mode 100644 index 0000000..a4e059b --- /dev/null +++ b/Assets/Scripts/Entities/SyncBases/Interactable.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 8032856a6943247cf9a30776c10df32e +timeCreated: 1494423974 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ProjectSettings/InputManager.asset b/ProjectSettings/InputManager.asset index 7d89d18..1486ea0 100644 --- a/ProjectSettings/InputManager.asset +++ b/ProjectSettings/InputManager.asset @@ -38,7 +38,7 @@ InputManager: axis: 0 joyNum: 0 - serializedVersion: 3 - m_Name: Fire1 + m_Name: Activate descriptiveName: descriptiveNegativeName: negativeButton: