You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
153 lines
3.8 KiB
153 lines
3.8 KiB
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<Charge> delete = new List<Charge>();
|
|
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<Charge> charges = new List<Charge>();
|
|
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<Connection> collidedConnections = new List<Connection>();
|
|
public readonly List<Circuit> collidedCircuits = new List<Circuit>();
|
|
private void OnCollisionEnter(Collision collision) {
|
|
Connection connection = collision.gameObject.GetComponent<Connection>();
|
|
BaseTrigger baseTrigger = collision.gameObject.GetComponent<BaseTrigger>();
|
|
if (connection != null)
|
|
collidedConnections.Add(connection);
|
|
if (baseTrigger != null)
|
|
collidedCircuits.Add(baseTrigger.Circuit);
|
|
}
|
|
private void OnCollisionExit(Collision collision) {
|
|
Connection connection = collision.gameObject.GetComponent<Connection>();
|
|
BaseTrigger baseTrigger = collision.gameObject.GetComponent<BaseTrigger>();
|
|
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<Renderer>();
|
|
}
|
|
private void UpdatePlayerMaterial() {
|
|
cableRenderer.material = ColorManager.GetCableBy(ActorNumber);
|
|
}
|
|
private void SetShaderEccentricity(float eccentricity) {
|
|
cableRenderer.material.SetFloat("_Eccentricity", eccentricity);
|
|
}
|
|
}
|
|
|