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-12 00:41:57 +02:00
|
|
|
|
using Cyber.Entities;
|
2017-05-12 10:53:57 +02:00
|
|
|
|
using Cyber.Util;
|
2017-05-15 21:30:12 +02:00
|
|
|
|
using Cyber.Items;
|
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-17 02:27:49 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The inventory interface.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public InventoryInterface InventoryInterface;
|
|
|
|
|
|
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 Vector3 Rotation;
|
2017-05-12 01:03:27 +02:00
|
|
|
|
private GameObject LastLookedAt;
|
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-12 10:53:57 +02:00
|
|
|
|
// Rotation (only when cursor is locked
|
|
|
|
|
if (CursorHandler.Locked()) {
|
2017-05-14 20:45:56 +02:00
|
|
|
|
Rotation.y += Input.GetAxis("Mouse X") * CursorHandler.GetMouseSensitivityX();
|
|
|
|
|
Rotation.x = Mathf.Clamp(Rotation.x - Input.GetAxis("Mouse Y") * CursorHandler.GetMouseSensitivityY(), -89, 89);
|
2017-05-12 10:53:57 +02:00
|
|
|
|
Character.SetRotation(Rotation);
|
|
|
|
|
}
|
2017-05-10 17:37:58 +02:00
|
|
|
|
|
|
|
|
|
// Interactions
|
2017-05-15 21:30:12 +02:00
|
|
|
|
bool Interacted = false;
|
2017-05-12 10:53:57 +02:00
|
|
|
|
GameObject LookedAtObject = CameraUtil.GetLookedAtGameObject(Camera, Character.InteractionDistance);
|
2017-05-12 01:03:27 +02:00
|
|
|
|
if (LookedAtObject != null) {
|
|
|
|
|
Interactable LookingAt = LookedAtObject.GetComponent<Interactable>();
|
|
|
|
|
if (LookingAt != null && (LookingAt.transform.position - Character.GetPosition()).magnitude < Character.InteractionDistance) {
|
|
|
|
|
if (Input.GetButtonDown("Activate")) {
|
|
|
|
|
InteractWith(LookingAt, InteractionType.Activate);
|
2017-05-15 21:30:12 +02:00
|
|
|
|
Interacted = true;
|
2017-05-12 01:03:27 +02:00
|
|
|
|
}
|
|
|
|
|
if (Input.GetButtonUp("Activate")) {
|
|
|
|
|
InteractWith(LookingAt, InteractionType.Deactivate);
|
2017-05-15 21:30:12 +02:00
|
|
|
|
Interacted = true;
|
2017-05-12 01:03:27 +02:00
|
|
|
|
}
|
|
|
|
|
if (LookedAtObject != LastLookedAt) {
|
|
|
|
|
InteractWith(LookingAt, InteractionType.Enter);
|
|
|
|
|
if (LastLookedAt != null) {
|
|
|
|
|
InteractWith(LastLookedAt.GetComponent<Interactable>(), InteractionType.Exit);
|
2017-05-11 00:52:05 +02:00
|
|
|
|
}
|
2017-05-10 17:37:58 +02:00
|
|
|
|
}
|
2017-05-12 01:03:27 +02:00
|
|
|
|
LastLookedAt = LookedAtObject;
|
2017-05-10 17:37:58 +02:00
|
|
|
|
}
|
2017-05-12 01:03:27 +02:00
|
|
|
|
} else if (LastLookedAt != null) {
|
|
|
|
|
InteractWith(LastLookedAt.GetComponent<Interactable>(), InteractionType.Exit);
|
|
|
|
|
LastLookedAt = null;
|
2017-05-10 17:37:58 +02:00
|
|
|
|
}
|
2017-05-15 21:30:12 +02:00
|
|
|
|
|
|
|
|
|
// Equipment actions
|
2017-05-17 02:27:49 +02:00
|
|
|
|
if (!Interacted && !InventoryInterface.IsOpen()) {
|
2017-05-15 21:30:12 +02:00
|
|
|
|
// Don't use equipment if you're interacting with something
|
|
|
|
|
// (ie. don't shoot at the buttons)
|
|
|
|
|
if (Input.GetButtonDown("Use Item (R)")) {
|
2017-05-16 10:36:31 +02:00
|
|
|
|
Character.Inventory.UseItemInSlot(EquipSlot.RightHand);
|
|
|
|
|
Client.Send(PktType.InventoryAction, Character.Inventory.ActionHandler.BuildUseItem(EquipSlot.RightHand));
|
2017-05-15 21:30:12 +02:00
|
|
|
|
}
|
|
|
|
|
if (Input.GetButtonDown("Use Item (L)")) {
|
2017-05-16 10:36:31 +02:00
|
|
|
|
Character.Inventory.UseItemInSlot(EquipSlot.LeftHand);
|
|
|
|
|
Client.Send(PktType.InventoryAction, Character.Inventory.ActionHandler.BuildUseItem(EquipSlot.LeftHand));
|
2017-05-15 21:30:12 +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
|
|
|
|
|
2017-05-12 01:03:27 +02:00
|
|
|
|
private void InteractWith(Interactable interactable, InteractionType type) {
|
|
|
|
|
interactable.Interact(Character, type);
|
|
|
|
|
if (interactable.GetInteractableSyncdata().PublicInteractions) {
|
|
|
|
|
Client.Send(PktType.Interact, new InteractionPkt(interactable.ID, type));
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-08 23:03:02 +02:00
|
|
|
|
}
|
|
|
|
|
}
|