2017-05-10 15:05:02 +02:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using Cyber.Entities.SyncBases;
|
2017-05-08 23:03:02 +02:00
|
|
|
|
using Cyber.Console;
|
2017-05-09 06:15:34 +02:00
|
|
|
|
using Cyber.Networking.Clientside;
|
|
|
|
|
using Cyber.Networking;
|
|
|
|
|
using Cyber.Networking.Messages;
|
2017-05-08 23:03:02 +02:00
|
|
|
|
|
|
|
|
|
namespace Cyber.Controls {
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Controls the player character. Shouldn't exist on the server, and only one
|
|
|
|
|
/// should exist per client (the character that client is controlling).
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class PlayerController : MonoBehaviour {
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The character this controller should control.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Character Character;
|
|
|
|
|
|
2017-05-10 17:37:58 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The camera the player is seeing the world through.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Camera Camera;
|
|
|
|
|
|
2017-05-09 12:11:36 +02:00
|
|
|
|
private CursorHandler CursorHandler;
|
|
|
|
|
private Vector3 Rotation;
|
|
|
|
|
|
|
|
|
|
private void Start() {
|
2017-05-10 15:16:19 +02:00
|
|
|
|
CursorHandler = GameObject.Find("/Systems/CursorHandler").GetComponent<CursorHandler>();
|
2017-05-09 12:11:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-08 23:03:02 +02:00
|
|
|
|
private void Update() {
|
|
|
|
|
if (!Term.IsVisible()) {
|
2017-05-10 17:37:58 +02:00
|
|
|
|
// Don't do any "gameplay stuff" if the debug console is up
|
|
|
|
|
|
2017-05-08 23:03:02 +02:00
|
|
|
|
// Handle inputs
|
2017-05-10 17:37:58 +02:00
|
|
|
|
// Movement
|
2017-05-08 23:03:02 +02:00
|
|
|
|
Vector3 Move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
|
|
|
|
|
if (Move.sqrMagnitude != 0) {
|
2017-05-09 12:30:56 +02:00
|
|
|
|
Character.Move(Character.transform.TransformDirection(Move));
|
2017-05-08 23:03:02 +02:00
|
|
|
|
} else if (Character.Moving()) {
|
|
|
|
|
Character.Stop();
|
|
|
|
|
}
|
2017-05-09 12:11:36 +02:00
|
|
|
|
|
2017-05-10 17:37:58 +02:00
|
|
|
|
// Rotation
|
2017-05-09 12:11:36 +02:00
|
|
|
|
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);
|
2017-05-10 17:37:58 +02:00
|
|
|
|
|
|
|
|
|
// 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) {
|
2017-05-11 18:58:33 +02:00
|
|
|
|
LookingAt.Interact(Character);
|
2017-05-11 00:52:05 +02:00
|
|
|
|
if (LookingAt.GetInteractableSyncdata().PublicInteractions) {
|
2017-05-11 21:00:52 +02:00
|
|
|
|
Client.Send(PktType.Interact, new InteractionPkt(LookingAt.ID));
|
2017-05-11 00:52:05 +02:00
|
|
|
|
}
|
2017-05-10 17:37:58 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-08 23:03:02 +02:00
|
|
|
|
} else if (Character.Moving()) {
|
|
|
|
|
// The debug console is open, stop the player.
|
|
|
|
|
Character.Stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-10 17:37:58 +02:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-08 23:03:02 +02:00
|
|
|
|
}
|
|
|
|
|
}
|