Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Diagnostics;
- using System;
- using System.Collections.Generic;
- using Assets.Scripts;
- public class PlayerController : RPCNetworkObject {
- #region Constructors
- public int _health = 100;
- public int _mana = 100;
- public float _force;
- public float _jump;
- public float _speedCap;
- private bool _shiftEnded = true;
- private bool _walkingBackwards = false;
- private int _moveButtonPushCount = 0;
- public Animator[] _animators;
- public SpriteEvents _events;
- public bool _useAI = false;
- public bool _activateInputChange = false;
- public string _faction;
- public bool _modifyOpponentHealth;
- public int _healthModifier;
- public bool _freeForAll = false;
- private bool _running = false;
- private Vector3 _firstPos = new Vector3();
- Stopwatch _deathTimer = new Stopwatch();
- Stopwatch _vaultTimer = Stopwatch.StartNew();
- public bool _claimedCamera;
- private bool _checkedPos = false;
- public bool AbilityActive;
- public int Damage;
- public bool canReceiveInput;
- public bool damagePerformed;
- public bool isBlocking;
- public ArmableObject _leftHand;
- public ArmableObject _rightHand;
- private PlayerController _lastAntagoniser;
- public GameObject _focalPoint;
- public Stopwatch _syncTimer = Stopwatch.StartNew();
- private Vector3 _lastPos = new Vector3();
- public AudioSource[] _playerAttackSounds;
- public List<Weapon> _weaponInventory;
- private WeildingSystem _inventorySystemLeft;
- private WeildingSystem _inventorySystemRight;
- private WeaponPairingSystem _weaponPairingSystem;
- #endregion Constructors
- // Use this for initialization
- void Start() {
- base.Start();
- networkView.RPC("SetPosition", PhotonTargets.All, transform.position);
- InitializePlayer();
- // Check and see if this is a free for all server.
- if (_freeForAll) {
- // Create a randomly generated _faction. Nobody is a friend here.
- _faction = DateTime.Now.Ticks.ToString() + UnityEngine.Random.Range(0, 999);
- }
- // Enable or disable the camera manager for this game object, depending on if its a human or AI.
- GetComponent<CameraManager>().enabled = !_useAI;
- _inventorySystemLeft = new WeildingSystem(_leftHand);
- _inventorySystemRight = new WeildingSystem(_rightHand);
- _weaponPairingSystem = new WeaponPairingSystem(_inventorySystemLeft, _inventorySystemRight, _animators);
- }
- void Update() {
- CheckForWeaponSwap();
- }
- private void CheckForWeaponSwap() {
- }
- // Update is called once per frame
- void FixedUpdate() {
- GetComponent<Rigidbody>().maxAngularVelocity = 0.5f;
- // Check if somebody else owns this game object.
- if (!photonView.isMine) {
- // Ease into the newest position.
- transform.position = Vector3.Lerp(transform.position, _lastPos, 0.3f);
- }
- // Check to see if we've tracked our first position.
- if (!_checkedPos) {
- // Track initial position.
- TrackInitialPos();
- }
- // Check to see player is below the map.
- CheckIfPlayerBelowMap();
- // Check to see if the player is dead.
- if (_health <= 0) {
- DeathOperations();
- } else {
- // Check to see if we can receive input.
- if (canReceiveInput) {
- CheckForEditorChanges();
- if (_events == null) {
- InitializePlayer();
- }
- base.Update();
- // Get our vertical value.
- float vertical = _events != null ? _events.Vertical : 0;
- // Get our horizontal value.
- float horizontal = _events != null ? _events.Horizontal : 0;
- // Get our jump value.
- float jump = _events != null ? _events.JumpValue : 0;
- // Trigger nessecary animation.
- TriggerAnimations(vertical, horizontal);
- // Check to see if we don't use AI.
- if (!_useAI) {
- // Manage player rotation.
- ManagePlayerRotation(vertical, horizontal);
- }
- // Check if player is ours.
- if (photonView.isMine) {
- // Regulate player motion.
- RegulateMotion(horizontal, vertical, jump);
- }
- }
- }
- AdjustPlayerCam();
- }
- #region Methods
- private void TriggerAnimations(float vertical, float horizontal) {
- // Go through all our _animators and set appropriate values.
- for (int i = 0; i < _animators.Length; i++) {
- Animator animator = _animators[i];
- if (animator != null) {
- animator.SetBool("Fly", true);
- photonView.RPC("SetFloat", PhotonTargets.All, "Altitude", transform.position.y, i);
- photonView.RPC("SetFloat", PhotonTargets.All, "Speed", Mathf.Abs(Mathf.Abs(horizontal) > Mathf.Abs(vertical) ? horizontal * _force : vertical * _force), i);
- }
- }
- }
- private void CheckForEditorChanges() {
- // Check to see if somebody wants to invoke changes in the editor.
- if (_activateInputChange) {
- InitializePlayer();
- // The change has been done, untrigger the boolean.
- _activateInputChange = false;
- }
- }
- private void DeathOperations() {
- // Check to see if our respawn counter hasn't started yet.
- if (!_deathTimer.IsRunning) {
- // Start the respawn counter.
- _deathTimer.Start();
- }
- // Go through all _animators listed on this script.
- foreach (Animator animator in _animators) {
- // Disable the _animators.
- animator.enabled = false;
- }
- // Freeze the player so they can no longer move.
- GetComponent<Rigidbody>().freezeRotation = false;
- // Destroy our _events so that the play can no longer interact with this script.
- _events = null;
- // Check to see if this server allows respawning.
- if (FindObjectOfType<ServerManager>()._canRespawn) {
- // Check if its time to respawn.
- CheckRespawn();
- } else {
- ReleaseCamera();
- }
- }
- private void TrackInitialPos() {
- // Get our first position and store it in memory.
- _lastPos = _firstPos = transform.position;
- // Mark that we've collected our first position.
- _checkedPos = true;
- }
- private void CheckIfPlayerBelowMap() {
- // Check to see if we've fallen below the map.
- if (transform.position.y < -2) {
- // Kill the player.
- _health = 0;
- // Turn the character upside down.
- transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(180, 90, 0), 0.03f);
- }
- }
- private void ReleaseCamera() {
- // Check to see if the main camera has been claimed by this player.
- if (_claimedCamera) {
- // Unclaim camera.
- Camera.main.GetComponent<CameraClaim>().Claimed = false;
- // Set the camera to follow the person who killed this player.
- Camera.main.GetComponent<CameraFollow>().player = _lastAntagoniser._focalPoint;
- }
- // Destory this script.
- Destroy(this);
- }
- private void ManagePlayerRotation(float vertical, float horizontal) {
- // Do a null check on our _events.
- if (_events != null) {
- // Check to see if we have input.
- if (_events.InputActive) {
- // Rotate the character according to the corresponding vertical and horizontal values.
- Vector3 rotation = GetRelativeRotation(Camera.main.transform.rotation.eulerAngles, vertical, horizontal, 1f, 1f);
- // Set the character rotation.
- if (photonView.isMine) {
- SetRotation(Quaternion.Euler(0, rotation.y, 0));
- }
- }
- }
- }
- private void CheckRespawn() {
- // Check to see if its time to respawn the human player.
- if (!_useAI && _deathTimer.ElapsedMilliseconds >= 5000) {
- // Check to make sure this is our human controlled player.
- if (photonView.isMine) {
- // Check to see if this player ever claimed the main camera.
- if (_claimedCamera) {
- // Unclaim the main camera.
- Camera.main.GetComponent<CameraClaim>().Claimed = false;
- }
- // Disable the ability to rotate the character.
- GetComponent<CameraManager>().enabled = false;
- // Respawn.
- FindObjectOfType<ServerManager>().SpawnPlayer(_faction);
- }
- // Check to see if its time to dispose of the characters dead body.
- if (_deathTimer.ElapsedMilliseconds >= 5000) {
- // Destroy the dead body.
- if (photonView.isMine) {
- PhotonNetwork.Destroy(gameObject);
- }
- }
- }
- // Check to see if its time to respawn the AI player.
- else if (_useAI && _deathTimer.ElapsedMilliseconds >= 5000) {
- // Make sure this is our AI player.
- if (photonView.isMine) {
- // Respawn.
- FindObjectOfType<ServerManager>().SpawnAI(_faction);
- if (photonView.isMine) {
- PhotonNetwork.Destroy(gameObject);
- }
- }
- }
- }
- private void InitializePlayer() {
- // Send an RPC command which sets the PlayerController event type across connected clients.
- photonView.RPC("SetMasterInput", PhotonTargets.All, new object[0]);
- // Send an RPC command that initializes the _events the PlayerController reacts to.
- photonView.RPC("InitializeEvents", PhotonTargets.All, new object[0]);
- }
- private void RegulateMotion(float horizontal, float vertical, float jump) {
- // Add relative force via RPC.
- photonView.RPC("AddRelativeForce", PhotonTargets.All, 0f, 0f, Mathf.Abs(Mathf.Abs(horizontal) > Mathf.Abs(vertical)
- ? horizontal * _force : vertical * _force));
- // Add force via RPC.
- photonView.RPC("AddForce", PhotonTargets.All, 0f, jump, 0f);
- if (vertical == 0) {
- GetComponent<Collider>().material.dynamicFriction = 1;
- } else {
- GetComponent<Collider>().material.dynamicFriction = 0;
- }
- _moveButtonPushCount = 0;
- // Resist movement for faster deceleration.
- ResistForce(_speedCap, 0.2f);
- }
- private Vector3 GetRelativeRotation(Vector3 rotation, float vertical, float horizontal, float xTolerance, float yTolerance) {
- if (vertical >= xTolerance && horizontal >= yTolerance) {
- rotation += new Vector3(0, 45, 0);
- } else if (vertical >= xTolerance && horizontal <= -yTolerance) {
- rotation += new Vector3(0, 315, 0);
- } else if (vertical <= -xTolerance && horizontal >= yTolerance) {
- rotation += new Vector3(0, 135, 0);
- } else if (vertical <= -xTolerance && horizontal <= -yTolerance) {
- rotation += new Vector3(0, 225, 0);
- } else if (vertical <= -xTolerance && horizontal == 0f) {
- rotation += new Vector3(0, 180, 0);
- } else if (vertical == 0f && horizontal <= -yTolerance) {
- rotation += new Vector3(0, 270, 0);
- } else if (vertical == 0f && horizontal >= yTolerance) {
- rotation += new Vector3(0, 90, 0);
- }
- return rotation;
- }
- [RPC]
- public void SetRotation(Quaternion rotation) {
- transform.rotation = rotation;
- }
- [RPC]
- public void SetPosition(Vector3 position) {
- _lastPos = position;
- }
- void AdjustPlayerCam() {
- if (photonView.isMine) {
- if (_syncTimer.ElapsedMilliseconds >= 100) {
- networkView.RPC("SetPosition", PhotonTargets.All, transform.position);
- _syncTimer.Reset();
- _syncTimer.Start();
- }
- if (!_useAI)
- if (!canReceiveInput && Camera.main.fieldOfView < 80) {
- Camera.main.fieldOfView++;
- } else if (_animators[0].GetBool("Run") && Camera.main.fieldOfView < 70 && !_modifyOpponentHealth) {
- Camera.main.fieldOfView++;
- } else if (_modifyOpponentHealth && Camera.main.fieldOfView < 40) {
- Camera.main.fieldOfView--;
- } else if (Camera.main.fieldOfView > 60 && !_animators[0].GetBool("Run")) {
- Camera.main.fieldOfView--;
- }
- }
- }
- [RPC]
- private void SetFloat(string identifier, float value, int index) {
- _animators[index].SetFloat(identifier, value);
- }
- [RPC]
- private void SetMasterInput() {
- if (_events != null) {
- DestroyImmediate(_events);
- }
- if (_useAI) {
- _events = gameObject.AddComponent<AIEvents>();
- (_events as AIEvents).CanRun = true;
- } else {
- _events = gameObject.AddComponent<PlayerEvents>();
- }
- }
- [RPC]
- private void InitializeEvents() {
- _events.Run += delegate {
- foreach (Animator animator in _animators) {
- animator.SetBool("Run", true);
- }
- if (!_running) {
- if (_shiftEnded) {
- _force *= 4;
- _speedCap *= 4;
- GetComponent<Rigidbody>().drag *= 3;
- GetComponent<Rigidbody>().angularDrag *= 3;
- _shiftEnded = false;
- _running = true;
- }
- }
- };
- _events.StopRun += delegate {
- foreach (Animator animator in _animators) {
- animator.SetBool("Run", false);
- }
- if (_running) {
- _force /= 4;
- _speedCap /= 4;
- GetComponent<Rigidbody>().drag /= 3;
- GetComponent<Rigidbody>().angularDrag /= 3;
- _shiftEnded = true;
- _running = false;
- }
- };
- _events.Attack += delegate {
- foreach (Animator animator in _animators) {
- animator.SetTrigger("Attack");
- }
- };
- _events.Defend += delegate {
- foreach (Animator animator in _animators) {
- if (!AbilityActive) {
- AbilityActive = true;
- animator.GetComponent<Interaction>().MagicDeflect();
- }
- }
- };
- _events.Jump += delegate {
- foreach (Animator animator in _animators) {
- animator.SetTrigger("Jump");
- }
- };
- }
- public bool ResistForce(float limit, float counterForce) {
- Vector3 velocity = transform.GetComponent<Rigidbody>().velocity;
- if (Mathf.Abs(velocity.x) > 0 && Mathf.Abs(velocity.x) > limit) {
- AddForce(velocity.x * counterForce * -1f, 0f, 0f);
- }
- if (Mathf.Abs(velocity.y) > 0 && Mathf.Abs(velocity.y) > limit) {
- AddForce(0f, velocity.y * counterForce * -1f, 0f);
- }
- if (Mathf.Abs(velocity.z) > 0 && Mathf.Abs(velocity.z) > limit) {
- AddForce(0f, 0f, velocity.z * counterForce * -1f);
- }
- return Mathf.Max(Mathf.Abs(velocity.x), Mathf.Abs(velocity.y), Mathf.Abs(velocity.x)) < limit;
- }
- void OnCollisionEnter(Collision collision) {
- //if (collision._gameObject.layer == 12) {
- // Vault(false);
- //}
- PerformDamage(collision);
- }
- void OnCollisionStay(Collision collision) {
- //if (collision._gameObject.layer != 12) {
- // Vault(false);
- //}
- PerformDamage(collision);
- }
- void OnCollisionLeave(Collision collision) {
- if (collision.gameObject.layer != 12) {
- Vault(false);
- }
- if (collision.gameObject.GetComponent<PlayerController>() != null) {
- photonView.RPC("ResetDamage", PhotonTargets.All, new object[0]);
- }
- }
- void OnGUI() {
- if (_claimedCamera) {
- GUI.Label(new Rect(0, 0, 200, 200), "Health: " + _health.ToString());
- GUI.Label(new Rect(0, 20, 200, 200), "Mana: " + _mana.ToString());
- }
- }
- [RPC]
- private void ResetDamage() {
- foreach (Animator animator in _animators) {
- animator.GetComponent<Interaction>().Reset();
- }
- }
- [RPC]
- public void Vault(bool value) {
- if (_vaultTimer.ElapsedMilliseconds >= 2000) {
- if (_running) {
- foreach (Animator animator in _animators) {
- animator.SetTrigger("Vault");
- _vaultTimer.Reset();
- _vaultTimer.Start();
- }
- }
- }
- }
- private void PerformDamage(Collision collision) {
- PlayerController controller = collision.gameObject.GetComponent<PlayerController>();
- Projectile projectile = collision.gameObject.GetComponent<Projectile>();
- _lastAntagoniser = controller;
- if (projectile != null) {
- _lastAntagoniser = projectile._master;
- }
- if (controller != null) {
- if (controller._faction != _faction) {
- if (_modifyOpponentHealth) {
- if (!controller._modifyOpponentHealth) {
- if (photonView.isMine) {
- if (!damagePerformed) {
- controller.photonView.RPC("ModifyHealth", PhotonTargets.All, Damage);
- damagePerformed = true;
- _mana += 20;
- }
- }
- } else {
- photonView.RPC("AddRelativeForce", PhotonTargets.All, 0f, 0f, -50f);
- Instantiate(Resources.Load("AirDeformer"), ((transform.position + controller.transform.position) / 2f), Quaternion.identity);
- }
- }
- }
- }
- if (projectile != null) {
- if (projectile._master != null) {
- if (projectile._master._faction != _faction) {
- photonView.RPC("ModifyHealth", PhotonTargets.All, projectile._damage);
- }
- }
- }
- }
- [RPC]
- private void ModifyHealth(int modifer) {
- if (modifer < 0) {
- foreach (Animator animator in _animators) {
- animator.SetTrigger("Injured");
- animator.GetComponent<Interaction>().Reset();
- }
- _health += modifer;
- }
- }
- internal void Jump() {
- photonView.RPC("AddForce", PhotonTargets.All, 0f, 200f, 0f);
- }
- #endregion Methods
- }
Advertisement
Add Comment
Please, Sign In to add comment