Avoid FindObjectsOfType

This commit is contained in:
Jason Durand 01 2022-04-02 22:36:08 -04:00
parent cc5507a2a5
commit c19966a4d2
9 changed files with 238 additions and 113 deletions

View File

@ -134,17 +134,36 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 56c3e06ea5db48a40a7f55e72291cb0d, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &344214379 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 344214379, guid: e1dac4f28fe75a547b919b7aa8240fed, type: 3}
m_PrefabInstance: {fileID: 1551362086}
--- !u!1 &615088692
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a53670f9cd399ea4a8ae3ed81b923aa8, type: 3}
m_Name:
m_EditorClassIdentifier:
serializedVersion: 6
m_Component:
- component: {fileID: 615088693}
m_Layer: 0
m_Name: Gladiators
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &615088693
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 615088692}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &647315810
PrefabInstance:
m_ObjectHideFlags: 0
@ -215,11 +234,47 @@ Transform:
m_CorrespondingSourceObject: {fileID: 1214567908930553594, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
m_PrefabInstance: {fileID: 1942557562}
m_PrefabAsset: {fileID: 0}
--- !u!1 &1186739821 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 1844274959428875625, guid: e1dac4f28fe75a547b919b7aa8240fed, type: 3}
--- !u!114 &836972483 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 344214379, guid: e1dac4f28fe75a547b919b7aa8240fed, type: 3}
m_PrefabInstance: {fileID: 1551362086}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a53670f9cd399ea4a8ae3ed81b923aa8, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &851716833
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 851716834}
m_Layer: 0
m_Name: Minions
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &851716834
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 851716833}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &1359990804
PrefabInstance:
m_ObjectHideFlags: 0
@ -227,18 +282,26 @@ PrefabInstance:
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: -7596782781093632548, guid: 581322f036f3ff1448d4d2ec70f295a4, type: 3}
propertyPath: <graveyard>k__BackingField
value:
objectReference: {fileID: 1621960505}
- target: {fileID: -7596782781093632548, guid: 581322f036f3ff1448d4d2ec70f295a4, type: 3}
propertyPath: <minionParent>k__BackingField
value:
objectReference: {fileID: 851716834}
- target: {fileID: -7596782781093632548, guid: 581322f036f3ff1448d4d2ec70f295a4, type: 3}
propertyPath: <gladiatorParent>k__BackingField
value:
objectReference: {fileID: 615088693}
- target: {fileID: 1359990806, guid: 581322f036f3ff1448d4d2ec70f295a4, type: 3}
propertyPath: gameTimer
value:
objectReference: {fileID: 344214379}
objectReference: {fileID: 836972483}
- target: {fileID: 1359990806, guid: 581322f036f3ff1448d4d2ec70f295a4, type: 3}
propertyPath: startPrompt
value:
objectReference: {fileID: 1186739821}
- target: {fileID: 4164153230343464235, guid: 581322f036f3ff1448d4d2ec70f295a4, type: 3}
propertyPath: globalCamera
value:
objectReference: {fileID: 1557338110}
objectReference: {fileID: 1551362088}
- target: {fileID: 9196727425507610130, guid: 581322f036f3ff1448d4d2ec70f295a4, type: 3}
propertyPath: m_RootOrder
value: 1
@ -289,7 +352,7 @@ PrefabInstance:
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 581322f036f3ff1448d4d2ec70f295a4, type: 3}
--- !u!114 &1359990809 stripped
--- !u!114 &1359990806 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 1359990806, guid: 581322f036f3ff1448d4d2ec70f295a4, type: 3}
m_PrefabInstance: {fileID: 1359990804}
@ -427,16 +490,62 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 81a29b049c6380f4abb3c18ed121efcd, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &1551362088 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 1844274959428875625, guid: e1dac4f28fe75a547b919b7aa8240fed, type: 3}
m_PrefabInstance: {fileID: 1551362086}
m_PrefabAsset: {fileID: 0}
--- !u!1 &1551362089 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 763564871199756608, guid: e1dac4f28fe75a547b919b7aa8240fed, type: 3}
m_PrefabInstance: {fileID: 1551362086}
m_PrefabAsset: {fileID: 0}
--- !u!1 &1557338110 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 3328484984159178892, guid: f7f5d2b1228d13f4d9015073aced3e81, type: 3}
m_PrefabInstance: {fileID: 647315810}
m_PrefabAsset: {fileID: 0}
--- !u!1 &1596616199 stripped
--- !u!1 &1621960504
GameObject:
m_CorrespondingSourceObject: {fileID: 763564871199756608, guid: e1dac4f28fe75a547b919b7aa8240fed, type: 3}
m_PrefabInstance: {fileID: 1551362086}
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1621960505}
m_Layer: 0
m_Name: Graveyard
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1621960505
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1621960504}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 6
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1692771147 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: -7596782781093632548, guid: 581322f036f3ff1448d4d2ec70f295a4, type: 3}
m_PrefabInstance: {fileID: 1359990804}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 72794012913ccd840a73788b90573212, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1001 &1942557562
PrefabInstance:
m_ObjectHideFlags: 0
@ -519,11 +628,11 @@ PrefabInstance:
- target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: m_ActionEvents.Array.data[14].m_PersistentCalls.m_Calls.Array.data[0].m_Target
value:
objectReference: {fileID: 1359990809}
objectReference: {fileID: 1359990806}
- target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: m_ActionEvents.Array.data[15].m_PersistentCalls.m_Calls.Array.data[0].m_Target
value:
objectReference: {fileID: 1359990809}
objectReference: {fileID: 1359990806}
- target: {fileID: 1214567908930553477, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: m_ActionEvents.Array.data[16].m_PersistentCalls.m_Calls.Array.data[0].m_Target
value:
@ -599,11 +708,11 @@ PrefabInstance:
- target: {fileID: 1214567908930553592, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: safeZonePrompt
value:
objectReference: {fileID: 1596616199}
objectReference: {fileID: 1551362089}
- target: {fileID: 1214567908930553592, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: gameFlowManager
value:
objectReference: {fileID: 1359990809}
objectReference: {fileID: 1359990806}
- target: {fileID: 1214567908930553593, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: m_Name
value: Vampire
@ -652,10 +761,22 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1214567908930553595, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: m_SortingOrder
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1967503440015794769, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: arena
value:
objectReference: {fileID: 1692771147}
- target: {fileID: 1967503440015794769, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: minionParent
value:
objectReference: {fileID: 851716834}
- target: {fileID: 1967503440015794769, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: gameFlowManager
value:
objectReference: {fileID: 1359990809}
objectReference: {fileID: 1359990806}
- target: {fileID: 3126145803593047825, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: healthBar
value:
@ -667,7 +788,7 @@ PrefabInstance:
- target: {fileID: 3126145803593047825, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: gameFlowManager
value:
objectReference: {fileID: 1359990809}
objectReference: {fileID: 1359990806}
- target: {fileID: 3126145803593047825, guid: 3e0aae8cda56aef44af9598dc5471020, type: 3}
propertyPath: <Health>k__BackingField
value: 100

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 6a79b85d54b2044408b5b5a2ad3ae57d
guid: f81c8dca006ae164792c46bced318c31
DefaultImporter:
externalObjects: {}
userData:

View File

@ -1,6 +1,4 @@
#nullable enable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
@ -9,7 +7,8 @@ public class AIEntity : Entity
[FormerlySerializedAs("stats")] [SerializeField]
public AIStats AIStats = null!;
BaseState currentState = null!;
public string[] enemies {get; protected set; }
public EntityFlag enemies {get; protected set; }
override protected void Start(){
base.Start();
@ -31,6 +30,11 @@ public class AIEntity : Entity
void OnDrawGizmos() => currentState?.OnDrawGizmos();
protected override void OnDied() {
base.OnDied();
transform.SetParent(arena.graveyard);
}
void SwitchState(BaseState newState) {
currentState.LeaveState();
currentState = newState;
@ -41,21 +45,14 @@ public class AIEntity : Entity
//Looks into enemy name list to see if the other is targetable
virtual protected bool IsTargetable(Entity other) {
foreach (string name in enemies){
if(other.entityName == name && other.IsAlive()){
return true;
}
}
return false;
return enemies.HasFlag(other.entityType) && other.IsAlive();
}
override public bool TakeDamage(float amount, Entity other){
Entity currTargetEntity = GetTarget().GetComponent<Entity>();
if(!(currTargetEntity is null)){
if(currTargetEntity.entityName == "Vampire")
SetTarget(other.transform);
}
//TODO Should we warn if target is null here?
if (target != null && target.GetComponent<VampireEntity>() is {})
target = other.transform;
return base.TakeDamage(amount, other);
}
@ -75,7 +72,7 @@ public class AIEntity : Entity
if(!entity.IsAlive()){
return new DeadState(entity);
}
Entity targetEntity = entity.GetTarget().GetComponent<Entity>();
Entity targetEntity = entity.target.GetComponent<Entity>();
if(targetEntity != null){
if(targetEntity.IsAlive()){//target is alive, keep chasing it
return null;
@ -87,9 +84,8 @@ public class AIEntity : Entity
}
public override BaseState? FixedUpdateState(){
Transform target = entity.GetTarget();
entity.direction = Vector3.RotateTowards(entity.direction, (target.position - entity.transform.position), entity.rotSpeed*Time.fixedDeltaTime, 0.0f);
if(entity.IsTargetable(entity.GetTarget().GetComponent<Entity>())){
entity.direction = Vector3.RotateTowards(entity.direction, (entity.target.position - entity.transform.position), entity.rotSpeed*Time.fixedDeltaTime, 0.0f);
if(entity.IsTargetable(entity.target.GetComponent<Entity>())){
if(!entity.IsInAttackRange()){
entity.rb.MovePosition(entity.transform.position + entity.direction * entity.movementSpeed * Time.fixedDeltaTime);
}else{
@ -112,21 +108,21 @@ public class AIEntity : Entity
if(!entity.IsAlive()){
return new DeadState(entity);
}
Entity[] entities = FindObjectsOfType<Entity>();
Transform entityParent = entity.entityType == EntityFlag.Gladiator ? entity.arena.minionParent : entity.arena.gladiatorParent;
float lastDist = float.MaxValue;
Entity chosenEntity = null!;
foreach (Entity other in entities){// Find the closest entity
if(entity.IsTargetable(other)){
float distance = Vector2.Distance(other.transform.position, entity.transform.position);
Transform chosenEntity = null!;
foreach (Transform other in entityParent){// Find the closest entity
float distance = Vector2.Distance(other.position, entity.transform.position);
if(distance < lastDist){
lastDist = distance;
chosenEntity = other;
if(lastDist <= entity.AIStats.closeEnough)break;
}
}
}
if(chosenEntity != null){
entity.SetTarget(chosenEntity.transform);
entity.target = chosenEntity;
return new SeekState(entity);
}else{
if(roamPosition == new Vector3()) roamPosition = entity.AIStats.getRandomRoamPositon();
@ -172,7 +168,7 @@ public class AIEntity : Entity
}
private BaseState? Attack(){
Entity targetEntity = entity.GetTarget().GetComponent<Entity>();
Entity targetEntity = entity.target.GetComponent<Entity>();
if(targetEntity != null){
targetEntity.TakeDamage(entity.attackDmg, entity);
bool isTargetAlive = targetEntity.IsAlive();

View File

@ -15,7 +15,6 @@ public class Arena : MonoBehaviour {
[SerializeField] [Required]
GameFlowManager gameFlowManager = null!;
//TODO probably add initial direction too
//TODO Add some kind of "MinLength(1)" attribute
[SerializeField]
GladiatorEntrance[] spawners = null!;
@ -24,6 +23,15 @@ public class Arena : MonoBehaviour {
[SerializeField] [Required]
GameObject entityPrefab = null!;
[field: SerializeField] [field: Required]
public Transform gladiatorParent { get; private set; } = null!;
[field: SerializeField] [field: Required]
public Transform minionParent { get; private set; } = null!;
[field: SerializeField] [field: Required]
public Transform graveyard { get; private set; } = null!;
SafeZone safeZone = null!;
void Awake() => safeZone = GetComponentInChildren<SafeZone>();
@ -34,9 +42,11 @@ public class Arena : MonoBehaviour {
if (!gameFlowManager.CanDoAction)
return;
var entity = Instantiate(entityPrefab, spawners[spawnerIndex].position, Quaternion.identity).GetComponent<Entity>();
entity.direction = spawners[spawnerIndex].direction;
entity.gameFlowManager = gameFlowManager;
var gladiator = Instantiate(entityPrefab, gladiatorParent).GetComponent<Gladiator>();
gladiator.arena = this;
gladiator.transform.position = spawners[spawnerIndex].position;
gladiator.direction = spawners[spawnerIndex].direction;
gladiator.gameFlowManager = gameFlowManager;
}
IEnumerator SpawnEnemies() {

View File

@ -1,10 +1,21 @@
using System;
using System.Collections;
using System.Collections.Generic;
using JetBrains.Annotations;
using NaughtyAttributes;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class Entity : MonoBehaviour {
[Flags]
public enum EntityFlag {
Vampire = 0,
Monster = 1,
Gladiator = 2,
}
public Arena arena = null!;
[SerializeField]
[Required]
public GameFlowManager gameFlowManager = null!;
@ -22,13 +33,12 @@ public class Entity : MonoBehaviour {
[field: SerializeField] public float attackDmg { get; private set; }
[SerializeField] protected float attackCooldown;
protected float attackTimer;
[SerializeField] private Transform target;
public string entityName { get; protected set; }
public Transform target;
public EntityFlag entityType { get; protected set; }
public Vector3 direction { get; set; }
public Rigidbody2D rb { get; private set; }
public Collider2D collider { get; private set; }
public SpriteRenderer renderer { get; private set; }
bool beingPushed;
[SerializeField] GameObject halo;
[SerializeField] Color deadColor = Color.red;
[SerializeField] Color emptyColor = Color.grey;
@ -71,21 +81,12 @@ public class Entity : MonoBehaviour {
}
public void SetTarget(Transform newTarget) {
target = newTarget;
}
public Transform GetTarget() {
return target;
}
//Apply damage to the entity, returns true if it is still alive
public virtual bool TakeDamage(float amount, Entity other) {
Health -= amount;
healthBar.SetHealthFraction(Health / initialHealth);
if (Health <= 0) {
isAlive = false;
OnDied();
return false;
}
@ -115,12 +116,8 @@ public class Entity : MonoBehaviour {
return isAlive;
}
protected void AddImpulse(Vector3 impulse) {
beingPushed = true;
rb.AddForce(impulse, ForceMode2D.Impulse);
}
protected virtual void OnDied() {
isAlive = false;
if(!(collider is null)){
collider.isTrigger = true;
}

View File

@ -4,12 +4,13 @@ using UnityEngine;
public class Gladiator : AIEntity
{
// Start is called before the first frame update
override protected void Start()
{
base.Start();
base.entityName = "Gladiator";
base.enemies = new string[]{"Monster", "Vampire"};
base.entityType = EntityFlag.Gladiator;
base.enemies = EntityFlag.Monster | EntityFlag.Vampire;
}
override protected void Update(){
@ -20,23 +21,14 @@ public class Gladiator : AIEntity
}
override protected bool IsTargetable(Entity other){
foreach (string name in enemies){
if(other.entityName == name && other.IsAlive()){
//If it is the vampire: check if it is in safezone
if(other.entityName == "Vampire"){
VampireEntity vampireEntity = other.GetComponent<VampireEntity>();
if(vampireEntity is null){
if (!other.IsAlive() || other is Gladiator)
return false;
}
if(vampireEntity.IsInSafeZone()){
return false;
}
}
//Is targetable if it's the vampire but not in safe zone
if (other is VampireEntity vampireEntity)
return !vampireEntity.IsInSafeZone();
//If monster
return true;
}
}
return false;
}
}

View File

@ -6,9 +6,14 @@ using UnityEngine;
using UnityEngine.InputSystem;
public class MinionThrower : MonoBehaviour {
[SerializeField] [Required]
Arena arena = null!;
[SerializeField] [Required]
GameFlowManager gameFlowManager = null!;
[SerializeField] [Required]
Transform minionParent = null!;
public Entity[] minionPrefabs;
public GameObject aimArrow;
@ -74,15 +79,17 @@ public class MinionThrower : MonoBehaviour {
currentCooldownTimer = currentInitialCooldown;
minionBar.UpdateReload(currentCooldownTimer / currentInitialCooldown);
var newMinion = Instantiate(minionBar.GetCurrentMinion().gameObject, transform.position + new Vector3(throwDirection.x, throwDirection.y, 0f) * 1f, Quaternion.identity)
var newMinion = Instantiate(minionBar.GetCurrentMinion().gameObject, minionParent)
.GetComponent<Monster>();
newMinion.arena = arena;
newMinion.transform.position = transform.position + new Vector3(throwDirection.x, throwDirection.y, 0f) * 1f;
newMinion.direction = throwDirection;
newMinion.gameFlowManager = vampireEntity.gameFlowManager;
if (movement.GetSafeZoneIfImmobile() is {} safeZone) {
newMinion.thrownFromSafeZone = true;
newMinion.thrownTargetPosition = safeZone.GetOutsidePosition(throwDirection);
}
newMinion.direction = throwDirection;
newMinion.gameFlowManager = vampireEntity.gameFlowManager;
}
}

View File

@ -9,8 +9,8 @@ public class Monster : AIEntity {
override protected void Start()
{
base.Start();
base.entityName = "Monster";
base.enemies = new string[]{"Gladiator"};
base.entityType = EntityFlag.Monster;
base.enemies = EntityFlag.Gladiator;
}
protected override BaseState CreateInitialState()
@ -18,6 +18,8 @@ public class Monster : AIEntity {
? (BaseStateAI)new NonPhysicThrownState(this, thrownTargetPosition)
: new ThrownState(this);
protected override bool IsTargetable(Entity other) => other.IsAlive() && other is Gladiator;
//Basically a copy of JumpingMovementState
protected class NonPhysicThrownState : BaseStateAI {
readonly Vector3 target;

View File

@ -11,7 +11,7 @@ public class VampireEntity : Entity {
protected override void Start() {
base.Start();
base.entityName = "Vampire";
base.entityType = EntityFlag.Vampire;
playerMovement = GetComponent<PlayerMovement>();
}