Cleaned up camera code, added locks to camera so only one player can control it at a time.

This commit is contained in:
Randall Dolifka 2017-10-16 22:08:59 -05:00
parent 8d4ec72dda
commit aabd87ea2e
20 changed files with 3056 additions and 174 deletions

78
Project Undercover/Assets/Resources/Guard Camera.prefab Normal file → Executable file
View file

@ -19,13 +19,13 @@ GameObject:
serializedVersion: 5
m_Component:
- component: {fileID: 4661043116803332}
- component: {fileID: 114306799516802978}
- component: {fileID: 20437103875467350}
- component: {fileID: 124909405360866564}
- component: {fileID: 92524413853108834}
- component: {fileID: 81070402336043930}
- component: {fileID: 114560588529924034}
- component: {fileID: 114324944445225698}
- component: {fileID: 114214698486704570}
- component: {fileID: 114503650767683602}
- component: {fileID: 108724348204580334}
m_Layer: 0
m_Name: Guard Camera
m_TagString: GuardCamera
@ -179,18 +179,68 @@ Behaviour:
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1889434021946074}
m_Enabled: 1
--- !u!114 &114306799516802978
MonoBehaviour:
--- !u!108 &108724348204580334
Light:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1889434021946074}
m_Enabled: 0
serializedVersion: 8
m_Type: 0
m_Color: {r: 1, g: 0, b: 0, a: 1}
m_Intensity: 3
m_Range: 10
m_SpotAngle: 48.2
m_CookieSize: 10
m_Shadows:
m_Type: 2
m_Resolution: -1
m_CustomResolution: -1
m_Strength: 1
m_Bias: 0.05
m_NormalBias: 0.4
m_NearPlane: 0.2
m_Cookie: {fileID: 0}
m_DrawHalo: 0
m_Flare: {fileID: 0}
m_RenderMode: 0
m_CullingMask:
serializedVersion: 2
m_Bits: 4294967295
m_Lightmapping: 4
m_AreaSize: {x: 1, y: 1}
m_BounceIntensity: 1
m_FalloffTable:
m_Table[0]: 0
m_Table[1]: 0
m_Table[2]: 0
m_Table[3]: 0
m_Table[4]: 0
m_Table[5]: 0
m_Table[6]: 0
m_Table[7]: 0
m_Table[8]: 0
m_Table[9]: 0
m_Table[10]: 0
m_Table[11]: 0
m_Table[12]: 0
m_ColorTemperature: 6570
m_UseColorTemperature: 0
m_ShadowRadius: 0
m_ShadowAngle: 0
--- !u!114 &114214698486704570
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1889434021946074}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1ef9de564c5529c4faeace3894ec9e0b, type: 3}
m_Script: {fileID: 11500000, guid: 5b4e3fd11940c1048a7e28e5dfc80560, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &114324944445225698
--- !u!114 &114503650767683602
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
@ -216,20 +266,6 @@ MonoBehaviour:
instantiationId: -1
currentMasterID: -1
isRuntimeInstantiated: 0
--- !u!114 &114560588529924034
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1889434021946074}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5b4e3fd11940c1048a7e28e5dfc80560, type: 3}
m_Name:
m_EditorClassIdentifier:
xRotation: 0
yRotation: 0
laserSightEnabled: 0
--- !u!124 &124909405360866564
Behaviour:
m_ObjectHideFlags: 1

View file

@ -67,4 +67,7 @@ MonoBehaviour:
- SetIsInteractingRPC
- SetSelectedInteractionRPC
- SetCharacterColorRPC
- SetOwner
- AddPlayer
- RemovePlayer
DisableAutoOpenWizard: 1

View file

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 47748aaed0f9bd04683409f11181e880
folderAsset: yes
timeCreated: 1508192016
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: e6ffeb27a9cb4a244bc7c814c7346df2
timeCreated: 1508191855
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: b65613489fb49064c808101090e077c4
timeCreated: 1506843441
licenseType: Free
NativeFormatImporter:
mainObjectFileID: 23800000
userData:
assetBundleName:
assetBundleVariant:

View file

@ -1331,6 +1331,20 @@ Light:
m_Lightmapping: 2
m_AreaSize: {x: 1, y: 1}
m_BounceIntensity: 1
m_FalloffTable:
m_Table[0]: 0
m_Table[1]: 0
m_Table[2]: 0
m_Table[3]: 0
m_Table[4]: 0
m_Table[5]: 0
m_Table[6]: 0
m_Table[7]: 0
m_Table[8]: 0
m_Table[9]: 0
m_Table[10]: 0
m_Table[11]: 0
m_Table[12]: 0
m_ColorTemperature: 6570
m_UseColorTemperature: 0
m_ShadowRadius: 0
@ -1961,6 +1975,20 @@ Light:
m_Lightmapping: 4
m_AreaSize: {x: 1, y: 1}
m_BounceIntensity: 1
m_FalloffTable:
m_Table[0]: 0
m_Table[1]: 0
m_Table[2]: 0
m_Table[3]: 0
m_Table[4]: 0
m_Table[5]: 0
m_Table[6]: 0
m_Table[7]: 0
m_Table[8]: 0
m_Table[9]: 0
m_Table[10]: 0
m_Table[11]: 0
m_Table[12]: 0
m_ColorTemperature: 6570
m_UseColorTemperature: 0
m_ShadowRadius: 0
@ -2102,12 +2130,12 @@ Prefab:
- target: {fileID: 114324944445225698, guid: c67fa24334c0f364cad8798522e92116,
type: 2}
propertyPath: viewIdField
value: 3
value: 1
objectReference: {fileID: 0}
- target: {fileID: 114324944445225698, guid: c67fa24334c0f364cad8798522e92116,
type: 2}
propertyPath: instantiationId
value: 3
value: 1
objectReference: {fileID: 0}
- target: {fileID: 114560588529924034, guid: c67fa24334c0f364cad8798522e92116,
type: 2}
@ -2159,6 +2187,20 @@ Light:
m_Lightmapping: 4
m_AreaSize: {x: 1, y: 1}
m_BounceIntensity: 1
m_FalloffTable:
m_Table[0]: 0
m_Table[1]: 0
m_Table[2]: 0
m_Table[3]: 0
m_Table[4]: 0
m_Table[5]: 0
m_Table[6]: 0
m_Table[7]: 0
m_Table[8]: 0
m_Table[9]: 0
m_Table[10]: 0
m_Table[11]: 0
m_Table[12]: 0
m_ColorTemperature: 6570
m_UseColorTemperature: 0
m_ShadowRadius: 0

177
Project Undercover/Assets/Scripts/Guard/GuardCamera.cs Normal file → Executable file
View file

@ -1,39 +1,138 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GuardCamera : Photon.PunBehaviour {
public float xRotation = 0.0f;
public float yRotation = 0.0f;
public Light spotLight;
void Start () {
xRotation = transform.eulerAngles.y;
yRotation = transform.eulerAngles.x;
}
void Update () {
Quaternion newRotation = Quaternion.AngleAxis(xRotation, Vector3.up);
newRotation *= Quaternion.AngleAxis(-yRotation, -Vector3.right);
transform.localRotation = Quaternion.Slerp(transform.rotation, newRotation, Time.time * 0.01f);
}
public void UpdateRotation()
{
photonView.RPC("UpdateRotation", PhotonTargets.All, xRotation, yRotation);
}
[PunRPC]
void UpdateRotation(float xRotation, float yRotation)
{
this.xRotation = xRotation;
this.yRotation = yRotation;
}
[PunRPC]
void SetEnabledRPC(bool enabled)
{
spotLight.enabled = enabled;
}
}
using System.Collections.Generic;
using UnityEngine;
public class GuardCamera : Photon.PunBehaviour {
private Light mSpotlight;
private Camera mCamera;
private AudioListener mListener;
private float xRotation = 0.0f;
private float yRotation = 0.0f;
private List<int> mPlayers; // Photo Player IDs
void Start()
{
// Get components
mSpotlight = GetComponent<Light>();
mCamera = GetComponent<Camera>();
mListener = GetComponent<AudioListener>();
// Everything off by default
mSpotlight.enabled = false;
mCamera.enabled = false;
mListener.enabled = false;
// Initalize rotation information
xRotation = transform.eulerAngles.y;
yRotation = transform.eulerAngles.x;
// Initialize empty player queue
mPlayers = new List<int>();
}
void Update()
{
Quaternion newRotation = Quaternion.AngleAxis(xRotation, Vector3.up);
newRotation *= Quaternion.AngleAxis(-yRotation, -Vector3.right);
transform.localRotation = Quaternion.Slerp(transform.rotation, newRotation, Time.time * 0.01f);
}
/**
* Called when the player switches to a camera.
*/
public void Activate()
{
// Turn on this camera for the player.
mCamera.enabled = true;
mListener.enabled = true;
// Trigger adding a player to this camera.
photonView.RPC("AddPlayer", PhotonTargets.All, PhotonNetwork.player.ID);
}
/**
* Called when a player switches away from a camera.
*/
public void Deactivate()
{
// Turn off this camera for the player.
mCamera.enabled = false;
mListener.enabled = false;
// Trigger removing a player from this camera.
photonView.RPC("RemovePlayer", PhotonTargets.All, PhotonNetwork.player.ID);
}
public bool InControl()
{
if (mPlayers.Count == 0 || mPlayers[0] != PhotonNetwork.player.ID)
{
return false;
}
return true;
}
/**
* Attempt to rotate the camera.
*/
public void Rotate(float xRotation, float yRotation)
{
// Do nothing if not in control.
if (!InControl())
{
return;
}
// Append the new rotation.
this.xRotation += xRotation;
this.yRotation += yRotation;
// Limit the rotation according to these constraints.
this.xRotation = this.xRotation % 360;
this.yRotation = Mathf.Clamp(this.yRotation, -45, 80);
// Notify everyone else that the camera rotated.
photonView.RPC("UpdateRotation", PhotonTargets.Others, this.xRotation, this.yRotation);
}
/*
* Called via RPC to add a player to this camera.
*/
[PunRPC]
void AddPlayer(int player)
{
// Add the player to the queue.
mPlayers.Add(player);
// Because there is now a player, make sure the spotlight is on.
mSpotlight.enabled = true;
}
/*
* Called via RPC to remove a player from this camera.
*/
[PunRPC]
void RemovePlayer(int player)
{
// Remove the player from the queue.
mPlayers.Remove(player);
// If no more players, turn off spotlight.
if (mPlayers.Count == 0)
{
mSpotlight.enabled = false;
}
}
/**
* Called via RPC to update with new rotation data.
*/
[PunRPC]
void UpdateRotation(float xRotation, float yRotation)
{
this.xRotation = xRotation;
this.yRotation = yRotation;
}
}

View file

@ -1,6 +1,6 @@
fileFormatVersion: 2
guid: 5b4e3fd11940c1048a7e28e5dfc80560
timeCreated: 1505971935
timeCreated: 1508202966
licenseType: Free
MonoImporter:
serializedVersion: 2

View file

@ -1,91 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GuardCameraController : Photon.PunBehaviour {
private float camSens = 120.0f;
private GuardCamera cam;
static List<GuardCameraController> cameras;
static int currentCamera;
void Start () {
cam = GetComponent<GuardCamera>();
if (cameras == null)
{
cameras = new List<GuardCameraController>();
foreach (var camera in GameObject.FindGameObjectsWithTag(tag))
{
cameras.Add(camera.GetComponent<GuardCameraController>());
}
for (int i=0; i < cameras.Count; i++)
{
if (cameras[i] == this)
currentCamera = i;
else
SetCameraEnabled(cameras[i], false);
}
}
}
GuardCameraController GetCurrentCamera()
{
return cameras[currentCamera];
}
GuardCameraController GetNextCamera(out int nextCameraPos)
{
nextCameraPos = (currentCamera + 1) % cameras.Count;
return cameras[nextCameraPos];
}
void SwitchCamera()
{
var nextCam = GetNextCamera(out currentCamera);
SetCameraEnabled(nextCam, true);
SetCameraEnabled(this, false);
}
public void SetCameraEnabled(GuardCameraController gCamera, bool enabled)
{
gCamera.GetComponent<Camera>().enabled = enabled;
gCamera.GetComponent<AudioListener>().enabled = enabled;
gCamera.enabled = enabled;
gCamera.photonView.RPC("SetEnabledRPC", PhotonTargets.Others, enabled);
}
void Update () {
if (Input.GetKeyDown("space"))
SwitchCamera();
RaycastHit hit;
if (Input.GetButtonDown("Fire1"))
{
Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100.0f))
{
if (hit.transform.gameObject.tag == "NPC")
{
var manager = GameObject.FindGameObjectWithTag("GameManager").GetComponent<GameManager>();
manager.photonView.RPC("ShowSpiesWinScreen", PhotonTargets.All);
}
else if (hit.transform.gameObject.tag == "Spy")
{
var manager = GameObject.FindGameObjectWithTag("GameManager").GetComponent<GameManager>();
manager.photonView.RPC("ShowGuardsWinScreen", PhotonTargets.All);
}
}
}
if (Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0)
return;
cam.xRotation += Time.deltaTime * Input.GetAxis("Horizontal") * camSens;
cam.yRotation += Time.deltaTime * -Input.GetAxis("Vertical") * camSens;
cam.xRotation = cam.xRotation % 360;
cam.yRotation = Mathf.Clamp(cam.yRotation, -45, 80);
cam.UpdateRotation();
}
}

View file

@ -0,0 +1,120 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GuardController : Photon.PunBehaviour {
public Text cameraStatusText;
const float CAMERA_SENSITIVITY = 120.0f;
private int mCurrentCamera;
private List<GuardCamera> mCameras;
private bool mInControl;
void Start()
{
// 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);
}
}
// Default is no camera. First to get it in Update wins.
// Photon doesn't seem to sync camera players until update,
// so if we Activate() here, it won't lock.
mCurrentCamera = -1;
}
/**
* Set the text in the bottom-right corner, denoting if the camera is locked.
*/
void SetCameraText()
{
if (mInControl)
{
cameraStatusText.text = "In-Control";
}
else
{
cameraStatusText.text = "Locked";
}
}
/**
* Cycle the player to the next camera, or grab the first camera
* if we don't have one yet.
*/
void SwitchCamera()
{
// Get relevant camera indexes
int lastCamera = mCurrentCamera;
mCurrentCamera += 1;
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();
SetCameraText();
}
void Update()
{
if (Input.GetKeyDown("space") || mCurrentCamera == -1)
{
SwitchCamera();
}
// Update camera status text if necessary.
bool newInControl = mCameras[mCurrentCamera].InControl();
if (newInControl != mInControl)
{
mInControl = newInControl;
SetCameraText();
}
/*RaycastHit hit;
if (Input.GetButtonDown("Fire1"))
{
Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100.0f))
{
if (hit.transform.gameObject.tag == "NPC")
{
var manager = GameObject.FindGameObjectWithTag("GameManager").GetComponent<GameManager>();
manager.photonView.RPC("ShowSpiesWinScreen", PhotonTargets.All);
}
else if (hit.transform.gameObject.tag == "Spy")
{
var manager = GameObject.FindGameObjectWithTag("GameManager").GetComponent<GameManager>();
manager.photonView.RPC("ShowGuardsWinScreen", PhotonTargets.All);
}
}
}*/
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;
mCameras[mCurrentCamera].Rotate(xRotation, yRotation);
}
}

View file

@ -1,6 +1,6 @@
fileFormatVersion: 2
guid: 1ef9de564c5529c4faeace3894ec9e0b
timeCreated: 1505422157
guid: 828c2a73d6549534cb33b938c6964162
timeCreated: 1508202966
licenseType: Free
MonoImporter:
serializedVersion: 2

View file

@ -1,5 +1,4 @@
using System;
using System.Collections;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
@ -7,7 +6,7 @@ using UnityEngine.SceneManagement;
public class GameManager : Photon.PunBehaviour {
public GuardCameraController guardCamera;
public GameObject guardController;
public GameObject spyPrefab, NPCPrefab, cameraRigPrefab;
public int numNpcs = 9;
public int spyMissionsComplete = 0;
@ -17,6 +16,7 @@ public class GameManager : Photon.PunBehaviour {
public GameObject winPanel;
public GameObject guardPanel;
public GameObject spyPanel;
public GameObject guardCameraPanel;
public Text winText;
private int numOfMissions = 3;
@ -47,12 +47,12 @@ public class GameManager : Photon.PunBehaviour {
{
if (PersistantPlayerSettings.character == PersistantPlayerSettings.Character.Guard)
{
guardCamera.SetCameraEnabled(guardCamera, true);
guardController.SetActive(true);
guardCameraPanel.SetActive(true);
//guardPanel.SetActive(true);
}
else
{
guardCamera.GetComponent<GuardCamera>().spotLight.enabled = true;
Vector3 randPos = StateController.GetRandomLocation();
var spy = PhotonNetwork.Instantiate(spyPrefab.name, randPos, Quaternion.identity, 0);

View file

@ -1,6 +1,4 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
@ -40,7 +38,7 @@ public class LobbyManager : Photon.PunBehaviour {
public void StartGame()
{
if (PhotonNetwork.isMasterClient)
PhotonNetwork.LoadLevel("NewCharactersTest");
PhotonNetwork.LoadLevel("GuardCameraTest");
}
public override void OnPhotonPlayerConnected(PhotonPlayer other)

View file

@ -1,10 +1,6 @@
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
namespace Com.MyCompany.MyGame
{
[RequireComponent(typeof(InputField))]

View file

@ -12,8 +12,11 @@ EditorBuildSettings:
path: Assets/Scenes/Lobby.unity
guid: 9d65756f3d12ef647aea0782245fc241
- enabled: 0
path: Assets/Scenes/scene.unity
path:
guid: b679f50ae448b0642b9887e440a00e0f
- enabled: 1
path: Assets/Scenes/NewCharactersTest.unity
guid: 89e17cc55d8d66942862d86930906b5f
- enabled: 1
path: Assets/Scenes/GuardCameraTest.unity
guid: e6ffeb27a9cb4a244bc7c814c7346df2

View file

@ -368,9 +368,6 @@ PlayerSettings:
switchUdpSendBufferSize: 9
switchUdpReceiveBufferSize: 42
switchSocketBufferEfficiency: 4
switchSocketInitializeEnabled: 1
switchNetworkInterfaceManagerInitializeEnabled: 1
switchPlayerConnectionEnabled: 1
ps4NPAgeRating: 12
ps4NPTitleSecret:
ps4NPTrophyPackPath:

View file

@ -1 +1 @@
m_EditorVersion: 2017.1.1f1
m_EditorVersion: 2017.1.0f3

2
Project Undercover/ProjectSettings/TagManager.asset Normal file → Executable file
View file

@ -8,6 +8,8 @@ TagManager:
- GameManager
- Spy
- NPC
- GuardController
- GuardPanel
layers:
- Default
- TransparentFX