Guest User

Untitled

a guest
Sep 3rd, 2015
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.69 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Diagnostics;
  4. using Assets.Scripts;
  5. using System.Collections.Generic;
  6. using UnityEngine.Networking;
  7.  
  8. public class ObjectInteractor : MonoBehaviour {
  9.     public GameObject focalObject;
  10.     public NetworkIdentity player;
  11.     public Light _flashLight;
  12.     private SecurityCamera securityCamera;
  13.     private Television television;
  14.     public int timeSensitivity;
  15.     private bool holdingObject;
  16.     private bool flashLightOn;
  17.     private Vector3 _lastRaycastHit;
  18.     Stopwatch timeSinceLastSwitch;
  19.     Stopwatch keyHeld;
  20.     PlacableObject placeableObject;
  21.     public List<InventoryHandler> _inventory;
  22.     public bool lockInventory;
  23.     public InventoryItem currentItem;
  24.     // Use this for initialization
  25.     void Start() {
  26.         timeSinceLastSwitch = Stopwatch.StartNew();
  27.         keyHeld = new Stopwatch();
  28.     }
  29.  
  30.     void FixedUpdate() {
  31.         if (player.isLocalPlayer) {
  32.             if (timeSinceLastSwitch.ElapsedMilliseconds >= timeSensitivity) {
  33.                 Ray ray = new Ray(transform.position, transform.forward);
  34.                 RaycastHit hit = new RaycastHit();
  35.                 Physics.Raycast(ray, out hit, 20f);
  36.                 if (hit.transform != null) {
  37.                     _lastRaycastHit = hit.point;
  38.                 }
  39.             }
  40.         } else {
  41.             GetComponent<AudioListener>().enabled = false;
  42.         }
  43.     }
  44.  
  45.     // Update is called once per frame
  46.     void Update() {
  47.         if (player.isLocalPlayer) {
  48.             if (Input.GetButtonDown("Fire1")) {
  49.                 Ray ray = new Ray(transform.position, transform.forward);
  50.                 RaycastHit hit = new RaycastHit();
  51.                 Physics.Raycast(ray, out hit, 20f);
  52.                 if (hit.transform != null) {
  53.                     if (hit.rigidbody != null) {
  54.                         PlayerController controller = hit.rigidbody.GetComponent<PlayerController>();
  55.                         if (controller != null) {
  56.                             if (!controller.stunned) {
  57.                                 // Send the stun command to the server
  58.                                 controller.CmdStun();
  59.                             }
  60.                         }
  61.                     }
  62.                 }
  63.             }
  64.             focalObject.transform.position = Vector3.Lerp(focalObject.transform.position, _lastRaycastHit, 0.2f);
  65.             if (timeSinceLastSwitch.ElapsedMilliseconds >= timeSensitivity) {
  66.                 CheckForFlashLightChange();
  67.                 if (Input.GetKeyUp(KeyCode.E)) {
  68.                     if (keyHeld.ElapsedMilliseconds <= 500) {
  69.                         if (!holdingObject) {
  70.                             RaycastHit hit = GetObject();
  71.                             if (hit.transform != null) {
  72.                                 placeableObject = hit.transform.gameObject.GetComponent<PlacableObject>();
  73.                                 placeableObject.Interact();
  74.                             }
  75.                         }
  76.                     } else {
  77.                         if (!holdingObject) {
  78.                             RaycastHit hit = GetObject();
  79.                             if (hit.transform != null) {
  80.                                 placeableObject = hit.transform.gameObject.GetComponent<PlacableObject>();
  81.                                 if (placeableObject != null) {
  82.                                     placeableObject._isBeingHeld = true;
  83.                                     holdingObject = true;
  84.                                 }
  85.                             }
  86.                         } else {
  87.                             ResetHold();
  88.                         }
  89.                     }
  90.                     keyHeld.Stop();
  91.                     keyHeld.Reset();
  92.                 }
  93.                 if (Input.GetKeyDown(KeyCode.E)) {
  94.                     if (!keyHeld.IsRunning) {
  95.                         keyHeld.Start();
  96.                     }
  97.                 }
  98.                 if (!lockInventory) {
  99.                     if (Input.GetKeyUp(KeyCode.Space)) {
  100.                         if (currentItem != null) {
  101.                             currentItem.Use();
  102.                         }
  103.                     }
  104.                     //if (Input.GetKeyUp(KeyCode.P)) {
  105.                     //  RaycastHit hit = GetObject();
  106.                     //  if (hit.transform != null) {
  107.                     //    InventoryItem newItem = hit.transform.gameObject.GetComponent<InventoryItem>();
  108.                     //    if (newItem.CanBeAddedToInventory) {
  109.                     //      switch (newItem.Type) {
  110.                     //        case InventoryItem.CategoryType.Equipment:
  111.                     //          newItem.AddToInventory(_inventory[0]._inventory);
  112.                     //          _inventory[0].Refresh();
  113.                     //          break;
  114.                     //        case InventoryItem.CategoryType.Paper:
  115.                     //          newItem.AddToInventory(_inventory[1]._inventory);
  116.                     //          break;
  117.                     //        case InventoryItem.CategoryType.Item:
  118.                     //          newItem.AddToInventory(_inventory[2]._inventory);
  119.                     //          break;
  120.                     //      }
  121.                     //    }
  122.                     //  }
  123.                     //}
  124.                 }
  125.             }
  126.             //if (Input.GetKeyUp(KeyCode.I)) {
  127.             //  FindObjectOfType<Canvas>().enabled = !FindObjectOfType<Canvas>().enabled;
  128.             //  lockInventory = FindObjectOfType<Canvas>().enabled;
  129.             //}
  130.         } else {
  131.             if (placeableObject != null) {
  132.                 ResetHold();
  133.             }
  134.         }
  135.     }
  136.  
  137.     private RaycastHit GetObject() {
  138.         Ray ray = new Ray(transform.position, transform.forward);
  139.         RaycastHit hit = new RaycastHit();
  140.         Physics.Raycast(ray, out hit, 20f);
  141.         return hit;
  142.     }
  143.  
  144.     private void CheckForFlashLightChange() {
  145.         if (Input.GetKeyDown(KeyCode.F)) {
  146.             flashLightOn = !flashLightOn;
  147.             if (flashLightOn) {
  148.                 _flashLight.intensity = 2;
  149.             } else {
  150.                 _flashLight.intensity = 0;
  151.             }
  152.         }
  153.     }
  154.     private void ResetHold() {
  155.         placeableObject._isBeingHeld = false;
  156.         placeableObject = null;
  157.         holdingObject = false;
  158.     }
  159.  
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment