refactor: moved a lot of player related scripts to the Reset.Units namespace
This commit is contained in:
@@ -1,50 +1,80 @@
|
||||
using System.Collections;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Threading.Tasks;
|
||||
using Drawing;
|
||||
using Reset;
|
||||
using Reset.Units;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
|
||||
public class Unit : NetworkBehaviour{
|
||||
public virtual void Start(){
|
||||
UnitStart();
|
||||
}
|
||||
|
||||
public virtual void UnitStart(){
|
||||
OnlineStart();
|
||||
}
|
||||
|
||||
protected void OnlineStart(){
|
||||
if (!NetworkManager.Singleton.IsConnectedClient && !NetworkManager.Singleton.IsHost) {
|
||||
Attach();
|
||||
} else {
|
||||
StartCoroutine(WaitForOnline());
|
||||
namespace Reset.Units{
|
||||
public class Unit : NetworkBehaviour{
|
||||
public virtual void Start(){
|
||||
UnitStart();
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator WaitForOnline(){
|
||||
while (!NetworkManager.Singleton.didAwake) {
|
||||
Debug.Log("waiting");
|
||||
yield return null;
|
||||
}
|
||||
|
||||
// Debug.Log($"{IsHost}, {IsClient}, {IsLocalPlayer}");
|
||||
if (IsLocalPlayer){
|
||||
GameManager.Player = gameObject;
|
||||
Attach();
|
||||
}
|
||||
}
|
||||
|
||||
public void Attach(){
|
||||
if (GameManager.Player == gameObject){
|
||||
GameManager.RequestNewController();
|
||||
GetComponent<LockOnManager>().AttachCamera(gameObject);
|
||||
public virtual async void UnitStart(){
|
||||
try {
|
||||
var netWaitResult = await WaitForNetwork();
|
||||
|
||||
if (netWaitResult) {
|
||||
|
||||
}
|
||||
|
||||
// OnlineStart();
|
||||
Debug.Log("Done");
|
||||
} catch {
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Update(){
|
||||
|
||||
}
|
||||
public async Task<bool> WaitForNetwork(){
|
||||
while (!NetworkManager.Singleton.IsConnectedClient) {
|
||||
await Awaitable.NextFrameAsync();
|
||||
}
|
||||
|
||||
protected override void OnNetworkPostSpawn(){
|
||||
// GetComponent<LockOnManager>().AttachCamera(gameObject);
|
||||
return (NetworkManager.Singleton.IsConnectedClient);
|
||||
}
|
||||
|
||||
public bool UnitIsNetworked(){
|
||||
return NetworkManager.Singleton.IsConnectedClient || NetworkManager.Singleton.IsHost;
|
||||
}
|
||||
|
||||
public bool UnitIsLocal(){
|
||||
if (UnitIsNetworked()) {
|
||||
return IsOwner;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected virtual void Update(){
|
||||
UpdateGizmos();
|
||||
|
||||
if (GetComponent<IKillable>() != null) {
|
||||
GetComponent<IKillable>().DrawHealthDebug();
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateGizmos(){
|
||||
string onlineStatus = "Not Online";
|
||||
Color onlineColor = Color.gray;
|
||||
|
||||
if (UnitIsNetworked() && UnitIsLocal()) {
|
||||
onlineStatus = "Online, Owned";
|
||||
onlineColor = Color.mediumSeaGreen;
|
||||
} else if (UnitIsNetworked() && !IsSpawned) {
|
||||
onlineStatus = "Not Spawned";
|
||||
} else if (UnitIsNetworked()) {
|
||||
onlineStatus = "Online, Not Owned";
|
||||
onlineColor = Color.gold;
|
||||
}
|
||||
|
||||
Draw.ingame.Label2D(transform.position + Vector3.up * 2.5f, onlineStatus, onlineColor);
|
||||
}
|
||||
|
||||
protected override void OnNetworkPostSpawn(){
|
||||
// GetComponent<LockOnManager>().AttachCamera(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user