maint: moving around methods to work better with Unit/Player/Enemy classes
This commit is contained in:
@@ -1,11 +1,51 @@
|
||||
using Reset.Core;
|
||||
using System;
|
||||
using Reset.Core;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Reset.Units{
|
||||
public class Enemy : Unit, ILockOnTarget{
|
||||
public class Enemy : Unit, ILockOnTarget, IKillable {
|
||||
public float lockonTargetRadius{ get; set; } = 10f;
|
||||
|
||||
public Animator testModelAnimator;
|
||||
|
||||
public override void UnitStart(){
|
||||
base.UnitStart();
|
||||
((IKillable)this).IKillableInitialize();
|
||||
}
|
||||
|
||||
public void OnTargetDelete(){
|
||||
GetComponent<ILockOnTarget>().SafelyDeleteTarget();
|
||||
}
|
||||
|
||||
public void TakeDamage(DamageSource[] sources){
|
||||
foreach (DamageSource source in sources) {
|
||||
TakeDamage(source);
|
||||
}
|
||||
}
|
||||
|
||||
public void TakeDamage(DamageSource source){
|
||||
try {
|
||||
((IKillable)this).currentHealth -= source.damageDealt;
|
||||
|
||||
testModelAnimator.SetTrigger("Hit");
|
||||
|
||||
if (((IKillable)this).currentHealth <= 0) {
|
||||
Kill();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Debug.LogError($"Failed TakeDamage on {this.name}: {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public void Kill(){
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
void Update(){
|
||||
GetComponent<IKillable>().DrawHealthDebug();
|
||||
}
|
||||
|
||||
public float maxHealth{ get; set; }
|
||||
public float currentHealth{ get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using Reset.Core;
|
||||
using Drawing;
|
||||
using Reset.Core;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Reset.Units{
|
||||
public interface IKillable : IDamageable{
|
||||
@@ -6,5 +8,29 @@ namespace Reset.Units{
|
||||
|
||||
public float maxHealth{ get; set; }
|
||||
public float currentHealth{ get; set; }
|
||||
|
||||
void IKillableInitialize(){
|
||||
SetMaxHealth();
|
||||
}
|
||||
|
||||
private void SetMaxHealth(){
|
||||
if (maxHealth == 0f) {
|
||||
Debug.LogError($"Max health is not set for type of <b>{((object)this)}</b>. Setting to 10000.");
|
||||
currentHealth = 10000f;
|
||||
} else {
|
||||
currentHealth = maxHealth;
|
||||
}
|
||||
}
|
||||
|
||||
internal void DrawHealthDebug(){
|
||||
using (Draw.WithColor(Color.blue)) {
|
||||
Draw.ingame.Label2D(((MonoBehaviour)this).transform.position + Vector3.up * 2.2f, ((IKillable)this).currentHealth.ToString(),
|
||||
Color.blue);
|
||||
}
|
||||
}
|
||||
|
||||
private void InternalUpdate(){
|
||||
Debug.Log("is this possible");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,53 +21,14 @@ public class Player : Unit, IKillable{
|
||||
controls = GetComponent<PlayerControls>();
|
||||
}
|
||||
|
||||
void Start(){
|
||||
if (((IKillable)this).maxHealth == 0f) {
|
||||
Debug.LogError($"Max health is not set for type of <b>{name}</b>. Setting to 100.");
|
||||
((IKillable)this).currentHealth = 10000f;
|
||||
} else {
|
||||
((IKillable)this).currentHealth = ((IKillable)this).maxHealth;
|
||||
}
|
||||
|
||||
|
||||
if (!NetworkManager.Singleton.IsConnectedClient && !NetworkManager.Singleton.IsHost) {
|
||||
Attach();
|
||||
} else {
|
||||
StartCoroutine(WaitForOnline());
|
||||
}
|
||||
public override void UnitStart(){
|
||||
base.UnitStart();
|
||||
((IKillable)this).IKillableInitialize();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnNetworkPostSpawn(){
|
||||
// GetComponent<LockOnManager>().AttachCamera(gameObject);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Update is called once per frame
|
||||
void Update(){
|
||||
using (Draw.WithColor(Color.blue)) {
|
||||
Draw.ingame.Label2D(transform.position + Vector3.up * 2.2f, ((IKillable)this).currentHealth.ToString(),
|
||||
Color.blue);
|
||||
}
|
||||
GetComponent<IKillable>().DrawHealthDebug();
|
||||
}
|
||||
|
||||
public void TakeDamage(DamageSource[] sources){
|
||||
|
||||
@@ -1,4 +1,50 @@
|
||||
using Unity.Netcode;
|
||||
using System.Collections;
|
||||
using Reset;
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
void Update(){
|
||||
|
||||
}
|
||||
|
||||
protected override void OnNetworkPostSpawn(){
|
||||
// GetComponent<LockOnManager>().AttachCamera(gameObject);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user