using Photon.Pun; using System.Collections; using System.Collections.Generic; using UnityEngine; public class Connection : MonoBehaviour { //General information public int id; private int actorNumber; public int ActorNumber { get => actorNumber; set { if (value == -1) actorNumber = 0; else actorNumber = value; foreach (Charge c in charges) c.ActorNumber = value; UpdatePlayerMaterial(); } } public bool IsMine { get => ActorNumber == PhotonNetwork.LocalPlayer.ActorNumber; } public Vector3 rootPosition; //Sender private Circuit sender; public Circuit Sender { get => sender; set { sender = value; if (value != null) ActorNumber = value.ActorNumber; UpdatePlayerMaterial(); } } //Receiver private Circuit receiver; public Circuit Receiver { get => receiver; set { receiver = value; if (value != null && Sender != null) if (value.ActorNumber != ActorNumber) value.OnBeingAttacked(); } } public bool ReceiverIsEnemy { get => Receiver.ActorNumber != ActorNumber; } //Gameplay private float chargeTime; public void UpdateConnection(float tickRate) { chargeTime += Time.deltaTime; if (chargeTime >= 1 / (tickRate * Sender.Values.sendRate) && !(!ReceiverIsEnemy && Receiver.IsFullWith(charges)) && Sender.CanSend) { Game.Instance.SpawnConnectionCharge(this); chargeTime = 0; } List delete = new List(); foreach (Charge charge in charges) { charge.UpdateCharge(tickRate); if (charge.HasReached(Receiver)) delete.Add(charge); } foreach (Charge charge in delete) Game.Instance.DestroyCharge(this, charge); } //Charge Management private readonly List charges = new List(); public void AddCharge(Charge charge) { charges.Add(charge); Sender.Charge--; } public void RemoveCharge(Charge charge) { charges.Remove(charge); if (ReceiverIsEnemy) { if (Receiver.Charge < 1) { Receiver.Charge = 1 - Receiver.Charge; Receiver.ActorNumber = ActorNumber; foreach (Connection connection in Receiver.GoingOutConnections) Game.Instance.DestroyConnection(connection); } else Receiver.Charge--; } else Receiver.Charge++; } public int CreateChargeId() { int id; do id = Random.Range(0, 10000); while (charges.Exists(c => c.id == id)); return id; } public Charge GetChargeById(int id) { Charge charge = charges.Find(c => c.id == id); return charge; } //Collisions public readonly List collidedConnections = new List(); public readonly List collidedCircuits = new List(); private void OnCollisionEnter(Collision collision) { Connection connection = collision.gameObject.GetComponent(); BaseTrigger baseTrigger = collision.gameObject.GetComponent(); if (connection != null) collidedConnections.Add(connection); if (baseTrigger != null) collidedCircuits.Add(baseTrigger.Circuit); } private void OnCollisionExit(Collision collision) { Connection connection = collision.gameObject.GetComponent(); BaseTrigger baseTrigger = collision.gameObject.GetComponent(); if (connection != null) collidedConnections.Remove(connection); if (baseTrigger != null) collidedCircuits.Remove(baseTrigger.Circuit); } //Everything Visual public float LengthScale { get => transform.localScale.y; set { float scale = Game.Instance.connectionScale; transform.localScale = new Vector3(scale, value, scale); SetShaderEccentricity(value * 5); } } private Renderer cableRenderer; void Awake() { cableRenderer = GetComponent(); } private void UpdatePlayerMaterial() { cableRenderer.material = ColorManager.GetCableBy(ActorNumber); } private void SetShaderEccentricity(float eccentricity) { cableRenderer.material.SetFloat("_Eccentricity", eccentricity); } }