2017-10-16 22:08:59 -05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
|
|
public class GuardController : Photon.PunBehaviour {
|
|
|
|
|
|
2017-11-09 14:54:12 -06:00
|
|
|
|
const float CAMERA_SENSITIVITY = 120.0f;
|
2017-10-16 22:08:59 -05:00
|
|
|
|
|
2017-11-05 22:02:03 -06:00
|
|
|
|
private int mCurrentCamera; // -1 means preview mode
|
2017-10-16 22:08:59 -05:00
|
|
|
|
private List<GuardCamera> mCameras;
|
|
|
|
|
|
|
|
|
|
private bool mInControl;
|
|
|
|
|
|
2017-11-09 14:54:12 -06:00
|
|
|
|
void Start()
|
2017-10-16 22:08:59 -05:00
|
|
|
|
{
|
|
|
|
|
// Fetch all cameras
|
|
|
|
|
mCameras = new List<GuardCamera>();
|
|
|
|
|
foreach (var camera in GameObject.FindGameObjectsWithTag("GuardCamera"))
|
|
|
|
|
{
|
|
|
|
|
var component = camera.GetComponent<GuardCamera>();
|
|
|
|
|
if (component != null)
|
|
|
|
|
{
|
|
|
|
|
mCameras.Add(component);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-05 22:02:03 -06:00
|
|
|
|
// Default is preview mode.
|
2017-10-16 22:08:59 -05:00
|
|
|
|
mCurrentCamera = -1;
|
2017-11-05 22:02:03 -06:00
|
|
|
|
EnablePreviewMode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Disable preview mode on all cameras.
|
|
|
|
|
*/
|
|
|
|
|
void DisablePreviewMode()
|
|
|
|
|
{
|
|
|
|
|
if (mCurrentCamera >= 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
foreach (var guardCamera in mCameras)
|
|
|
|
|
{
|
|
|
|
|
guardCamera.DisablePreviewMode();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Enable preview mode on call cameras and position them appropriately.
|
|
|
|
|
*/
|
|
|
|
|
void EnablePreviewMode()
|
|
|
|
|
{
|
|
|
|
|
// Deactivate current camera, if necessary.
|
|
|
|
|
if (mCurrentCamera != -1)
|
|
|
|
|
{
|
|
|
|
|
mCameras[mCurrentCamera].Deactivate();
|
|
|
|
|
}
|
|
|
|
|
mCurrentCamera = -1;
|
|
|
|
|
|
|
|
|
|
// Cameras are always arranged in ZxZ rows and columns. Determine Z.
|
|
|
|
|
int z = (int) Mathf.Ceil(Mathf.Sqrt(mCameras.Count));
|
|
|
|
|
float size = 1.0f / z;
|
|
|
|
|
|
|
|
|
|
// Set all cameras to preview mode appropriately.
|
|
|
|
|
for (int i = 0; i < mCameras.Count; ++i)
|
|
|
|
|
{
|
|
|
|
|
mCameras[i].EnablePreviewMode(size, i % z, i / z);
|
|
|
|
|
}
|
2017-10-16 22:08:59 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-11-05 22:02:03 -06:00
|
|
|
|
* Cycle the player to a specific camera, or just the next one.
|
2017-10-16 22:08:59 -05:00
|
|
|
|
*/
|
2017-11-05 22:02:03 -06:00
|
|
|
|
void SwitchCamera(int nextCamera = -1)
|
2017-10-16 22:08:59 -05:00
|
|
|
|
{
|
|
|
|
|
// Get relevant camera indexes
|
|
|
|
|
int lastCamera = mCurrentCamera;
|
2017-11-05 22:02:03 -06:00
|
|
|
|
mCurrentCamera = nextCamera == -1 ? mCurrentCamera + 1 : nextCamera;
|
2017-10-16 22:08:59 -05:00
|
|
|
|
if (mCurrentCamera >= mCameras.Count)
|
|
|
|
|
{
|
|
|
|
|
mCurrentCamera = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Deactivate the old camera, activate new one
|
|
|
|
|
mCameras[mCurrentCamera].Activate();
|
|
|
|
|
if (lastCamera != -1)
|
|
|
|
|
{
|
|
|
|
|
mCameras[lastCamera].Deactivate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Determine if we are in control.
|
|
|
|
|
mInControl = mCameras[mCurrentCamera].InControl();
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-09 02:07:37 -06:00
|
|
|
|
GuardCamera GetCurrentGuardCamera()
|
|
|
|
|
{
|
|
|
|
|
return mCameras[mCurrentCamera];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Update()
|
2017-10-16 22:08:59 -05:00
|
|
|
|
{
|
2017-11-05 22:02:03 -06:00
|
|
|
|
if (mCurrentCamera < 0)
|
|
|
|
|
{
|
|
|
|
|
UpdatePreviewMode();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
UpdateSingle();
|
|
|
|
|
}
|
2017-11-09 14:54:12 -06:00
|
|
|
|
}
|
2017-11-05 22:02:03 -06:00
|
|
|
|
|
|
|
|
|
void UpdatePreviewMode()
|
|
|
|
|
{
|
|
|
|
|
// If the player clicks a camera, switch to that camera.
|
|
|
|
|
if (Input.GetButtonDown("Fire1"))
|
|
|
|
|
{
|
|
|
|
|
// Get the click location.
|
|
|
|
|
float x = Input.mousePosition.x / Screen.width;
|
|
|
|
|
float y = Input.mousePosition.y / Screen.height;
|
|
|
|
|
|
|
|
|
|
// Cameras are always arranged in ZxZ rows and columns. Determine Z.
|
|
|
|
|
int z = (int)Mathf.Ceil(Mathf.Sqrt(mCameras.Count));
|
|
|
|
|
float size = 1.0f / z;
|
|
|
|
|
|
|
|
|
|
// Determine the camera that the click corresponds to, if any.
|
|
|
|
|
int tileX = (int) (x / size);
|
|
|
|
|
int tileY = (int) (y / size);
|
|
|
|
|
int camera = tileY * z + tileX;
|
|
|
|
|
|
|
|
|
|
// If the camera is valid, switch to it.
|
|
|
|
|
if (camera < mCameras.Count)
|
|
|
|
|
{
|
|
|
|
|
DisablePreviewMode();
|
|
|
|
|
SwitchCamera(camera);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UpdateSingle()
|
|
|
|
|
{
|
|
|
|
|
// Escape switches to Preview Mode.
|
|
|
|
|
if (Input.GetKeyDown("escape"))
|
|
|
|
|
{
|
|
|
|
|
EnablePreviewMode();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Space switches to next camera.
|
|
|
|
|
if (Input.GetKeyDown("space"))
|
2017-10-16 22:08:59 -05:00
|
|
|
|
{
|
|
|
|
|
SwitchCamera();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update camera status text if necessary.
|
|
|
|
|
bool newInControl = mCameras[mCurrentCamera].InControl();
|
|
|
|
|
if (newInControl != mInControl)
|
|
|
|
|
{
|
|
|
|
|
mInControl = newInControl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Input.GetButtonDown("Fire1"))
|
|
|
|
|
{
|
2017-11-09 02:07:37 -06:00
|
|
|
|
RaycastHit hit;
|
2017-11-26 21:23:09 -06:00
|
|
|
|
Ray ray = GetCurrentGuardCamera().Camera.ScreenPointToRay(Input.mousePosition);
|
2017-10-16 22:08:59 -05:00
|
|
|
|
|
|
|
|
|
if (Physics.Raycast(ray, out hit, 100.0f))
|
|
|
|
|
{
|
|
|
|
|
if (hit.transform.gameObject.tag == "NPC")
|
|
|
|
|
{
|
2017-11-09 02:07:37 -06:00
|
|
|
|
ScorePanelController.GuardCaughtNPC();
|
2017-10-16 22:08:59 -05:00
|
|
|
|
}
|
|
|
|
|
else if (hit.transform.gameObject.tag == "Spy")
|
|
|
|
|
{
|
2017-11-09 14:04:38 -06:00
|
|
|
|
ScorePanelController.CaughtSpy(hit.transform.gameObject.GetPhotonView().viewID);
|
2017-10-16 22:08:59 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-09 02:07:37 -06:00
|
|
|
|
}
|
2017-10-16 22:08:59 -05:00
|
|
|
|
|
|
|
|
|
if (Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float xRotation = Time.deltaTime * Input.GetAxis("Horizontal") * CAMERA_SENSITIVITY;
|
|
|
|
|
float yRotation = Time.deltaTime * -Input.GetAxis("Vertical") * CAMERA_SENSITIVITY;
|
2017-11-09 02:07:37 -06:00
|
|
|
|
GetCurrentGuardCamera().Rotate(xRotation, yRotation);
|
2017-11-09 14:54:12 -06:00
|
|
|
|
}
|
|
|
|
|
}
|