Add Interactable and a simple blinking lamp to interact with

This commit is contained in:
excitedneon 2017-05-10 18:37:58 +03:00
parent a5d5a7c9ef
commit 3f022a975c
11 changed files with 403 additions and 12 deletions

75
Assets/Materials/Lamp.mat Normal file
View File

@ -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}

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 82a7d1c3c9154445794eb04c29ab2bd3
timeCreated: 1494171362
licenseType: Free
NativeFormatImporter:
mainObjectFileID: 2100000
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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

View File

@ -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

View File

@ -18,6 +18,11 @@ namespace Cyber.Controls {
/// </summary>
public Character Character;
/// <summary>
/// The camera the player is seeing the world through.
/// </summary>
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<Interactable>();
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;
}
}
}
}

View File

@ -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 {
/// <summary>
///
/// </summary>
public class BlinkyBox : Interactable {
/// <summary>
/// The lamp mesh that will blink.
/// </summary>
public MeshRenderer Mesh;
/// <summary>
/// How long the blink lasts.
/// </summary>
public float BlinkLength = 1f;
/// <summary>
/// How bright the light is (upwards of 1 for the bloom to kick in)
/// </summary>
public float BlinkBrightness = 1.5f;
/// <summary>
/// The color of the blink.
/// </summary>
public Color BlinkColor = new Color(1f, 0.6f, 0f);
private double BlinkTime = 0;
private Material Material;
/// <summary>
/// When a BlinkyBox is interacted with, it blinks once, light lasting
/// for <see cref="BlinkLength"/> seconds.
/// </summary>
public override void Interact() {
BlinkTime = NetworkHelper.GetCurrentSystemTime();
}
/// <summary>
/// Deserializes this SyncBase for further use.
/// </summary>
/// <param name="reader"></param>
public override void Deserialize(NetworkReader reader) {
double ServerBlinkTime = reader.ReadDouble();
if (ServerBlinkTime > BlinkTime) {
BlinkTime = ServerBlinkTime;
}
}
/// <summary>
/// Serialize this SyncBase into a sync packet.
/// </summary>
/// <param name="writer"></param>
public override void Serialize(NetworkWriter writer) {
writer.Write(BlinkTime);
}
/// <summary>
/// The blinky boxes handletype is
/// </summary>
/// <returns>Sync Handletype containing sync information.</returns>
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);
}
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 0c07d843d1f7647df9d2fb28d9e51c51
timeCreated: 1494425788
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -15,6 +15,11 @@ namespace Cyber.Entities.SyncBases {
/// </summary>
public float MovementSpeed = 5.0f;
/// <summary>
/// The interaction distance of this player.
/// </summary>
public float InteractionDistance = 2.0f;
/// <summary>
/// The character controller, used to move the character. Handles collisions.
/// </summary>
@ -63,6 +68,14 @@ namespace Cyber.Entities.SyncBases {
transform.localEulerAngles = BodyRot;
}
/// <summary>
/// Sets the position of this character.
/// </summary>
/// <param name="Position">Position.</param>
public void SetPosition(Vector3 Position) {
transform.position = Position;
}
/// <summary>
/// Whether the player is moving or not.
/// </summary>
@ -80,6 +93,14 @@ namespace Cyber.Entities.SyncBases {
transform.localEulerAngles.y, Head.localEulerAngles.z);
}
/// <summary>
/// Returns the position of this character.
/// </summary>
/// <returns>The position.</returns>
public Vector3 GetPosition() {
return transform.position;
}
/// <summary>
/// Gets the Sync Handletype for Character, which doesn't require hash differences and syncs every tick.
/// </summary>
@ -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 {
/// </summary>
/// <param name="writer"></param>
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));
}
}

View File

@ -0,0 +1,18 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Cyber.Entities.SyncBases {
/// <summary>
/// Base class for all interactible entities in the world.
/// </summary>
public abstract class Interactable : SyncBase {
/// <summary>
/// All interactables should implement their interactions by overriding this.
/// </summary>
public abstract void Interact();
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 8032856a6943247cf9a30776c10df32e
timeCreated: 1494423974
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -38,7 +38,7 @@ InputManager:
axis: 0
joyNum: 0
- serializedVersion: 3
m_Name: Fire1
m_Name: Activate
descriptiveName:
descriptiveNegativeName:
negativeButton: