Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Diagnostics;
- using Assets.Scripts;
- using System.Collections.Generic;
- using UnityEngine.Networking;
- public class ObjectInteractor : MonoBehaviour {
- public GameObject focalObject;
- public NetworkIdentity player;
- public Light _flashLight;
- private SecurityCamera securityCamera;
- private Television television;
- public int timeSensitivity;
- private bool holdingObject;
- private bool flashLightOn;
- private Vector3 _lastRaycastHit;
- Stopwatch timeSinceLastSwitch;
- Stopwatch keyHeld;
- PlacableObject placeableObject;
- public List<InventoryHandler> _inventory;
- public bool lockInventory;
- public InventoryItem currentItem;
- // Use this for initialization
- void Start() {
- timeSinceLastSwitch = Stopwatch.StartNew();
- keyHeld = new Stopwatch();
- }
- void FixedUpdate() {
- if (player.isLocalPlayer) {
- if (timeSinceLastSwitch.ElapsedMilliseconds >= timeSensitivity) {
- Ray ray = new Ray(transform.position, transform.forward);
- RaycastHit hit = new RaycastHit();
- Physics.Raycast(ray, out hit, 20f);
- if (hit.transform != null) {
- _lastRaycastHit = hit.point;
- }
- }
- } else {
- GetComponent<AudioListener>().enabled = false;
- }
- }
- // Update is called once per frame
- void Update() {
- if (player.isLocalPlayer) {
- if (Input.GetButtonDown("Fire1")) {
- Ray ray = new Ray(transform.position, transform.forward);
- RaycastHit hit = new RaycastHit();
- Physics.Raycast(ray, out hit, 20f);
- if (hit.transform != null) {
- if (hit.rigidbody != null) {
- PlayerController controller = hit.rigidbody.GetComponent<PlayerController>();
- if (controller != null) {
- if (!controller.stunned) {
- // Send the stun command to the server
- controller.CmdStun();
- }
- }
- }
- }
- }
- focalObject.transform.position = Vector3.Lerp(focalObject.transform.position, _lastRaycastHit, 0.2f);
- if (timeSinceLastSwitch.ElapsedMilliseconds >= timeSensitivity) {
- CheckForFlashLightChange();
- if (Input.GetKeyUp(KeyCode.E)) {
- if (keyHeld.ElapsedMilliseconds <= 500) {
- if (!holdingObject) {
- RaycastHit hit = GetObject();
- if (hit.transform != null) {
- placeableObject = hit.transform.gameObject.GetComponent<PlacableObject>();
- placeableObject.Interact();
- }
- }
- } else {
- if (!holdingObject) {
- RaycastHit hit = GetObject();
- if (hit.transform != null) {
- placeableObject = hit.transform.gameObject.GetComponent<PlacableObject>();
- if (placeableObject != null) {
- placeableObject._isBeingHeld = true;
- holdingObject = true;
- }
- }
- } else {
- ResetHold();
- }
- }
- keyHeld.Stop();
- keyHeld.Reset();
- }
- if (Input.GetKeyDown(KeyCode.E)) {
- if (!keyHeld.IsRunning) {
- keyHeld.Start();
- }
- }
- if (!lockInventory) {
- if (Input.GetKeyUp(KeyCode.Space)) {
- if (currentItem != null) {
- currentItem.Use();
- }
- }
- //if (Input.GetKeyUp(KeyCode.P)) {
- // RaycastHit hit = GetObject();
- // if (hit.transform != null) {
- // InventoryItem newItem = hit.transform.gameObject.GetComponent<InventoryItem>();
- // if (newItem.CanBeAddedToInventory) {
- // switch (newItem.Type) {
- // case InventoryItem.CategoryType.Equipment:
- // newItem.AddToInventory(_inventory[0]._inventory);
- // _inventory[0].Refresh();
- // break;
- // case InventoryItem.CategoryType.Paper:
- // newItem.AddToInventory(_inventory[1]._inventory);
- // break;
- // case InventoryItem.CategoryType.Item:
- // newItem.AddToInventory(_inventory[2]._inventory);
- // break;
- // }
- // }
- // }
- //}
- }
- }
- //if (Input.GetKeyUp(KeyCode.I)) {
- // FindObjectOfType<Canvas>().enabled = !FindObjectOfType<Canvas>().enabled;
- // lockInventory = FindObjectOfType<Canvas>().enabled;
- //}
- } else {
- if (placeableObject != null) {
- ResetHold();
- }
- }
- }
- private RaycastHit GetObject() {
- Ray ray = new Ray(transform.position, transform.forward);
- RaycastHit hit = new RaycastHit();
- Physics.Raycast(ray, out hit, 20f);
- return hit;
- }
- private void CheckForFlashLightChange() {
- if (Input.GetKeyDown(KeyCode.F)) {
- flashLightOn = !flashLightOn;
- if (flashLightOn) {
- _flashLight.intensity = 2;
- } else {
- _flashLight.intensity = 0;
- }
- }
- }
- private void ResetHold() {
- placeableObject._isBeingHeld = false;
- placeableObject = null;
- holdingObject = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment