WIP EnemyHandler

This commit is contained in:
Stedd 2022-09-17 23:53:34 +02:00
parent c1a576768f
commit 2275195932
10 changed files with 136 additions and 238 deletions

View File

@ -1,78 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyHandler : MonoBehaviour
{
[Header("Parameters")]
[SerializeField] [Range(0.1f, 60f)] float spawnRate = 60f;
[SerializeField] int objectPoolSize = 15;
[Header("Prefabs")]
[SerializeField] GameObject objectPool;
[SerializeField] List<GameObject> enemyPrefabs = new List<GameObject>();
[Header("Lists")]
[SerializeField] List<GameObject> enemyPools = new List<GameObject>();
[SerializeField] List<GameObject> allEnemies = new List<GameObject>();
private void Start()
{
//gridManager.CalculateNewPath();
PopulateObjectPools();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.N))
{
}
}
void PopulateObjectPools()
{
foreach (GameObject enemy in enemyPrefabs)
{
GameObject newPool = Instantiate(objectPool, transform);
newPool.transform.name = $"ObjectPool:{enemy.name}";
ObjectPool poolScript = newPool.GetComponent<ObjectPool>();
enemyPools.Add(newPool);
for (int i = 0; i < objectPoolSize; i++)
{
enemy.SetActive(false);
poolScript.AddObject(enemy);
}
}
}
public void AddEnemyToAllEnemies(GameObject _enemy)
{
allEnemies.Add(_enemy);
}
public void RemoveEnemy(GameObject _enemy)
{
allEnemies.Remove(_enemy);
}
public List<GameObject> ReturnAllEnemies()
{
return allEnemies;
}
public Vector2Int GetCoordinatesFromPosition(Vector3 position)
{
Vector2Int coordinates = new Vector2Int();
coordinates.x = Mathf.RoundToInt(position.x / UnityEditor.EditorSnapSettings.move.x);
coordinates.y = Mathf.RoundToInt(position.z / UnityEditor.EditorSnapSettings.move.z);
return coordinates;
}
public void NotifyEnemiesOfNewPath()
{
BroadcastMessage("RecalculatePath", SendMessageOptions.DontRequireReceiver);
}
}

View File

@ -1,4 +1,5 @@
using AsteroidGame.Entities.Structures.Tower;
using AsteroidGame.Handlers;
using UnityEngine;
namespace AsteroidGame.Entities.Enemies.Scripts

View File

@ -19,7 +19,6 @@ namespace AsteroidGame.Entities
}
}
public void SetHealth(int newHealth)
{
health = newHealth;

View File

@ -676,12 +676,12 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: d3a16cf44e28f02409c23498ed14acf5, type: 3}
m_Name:
m_EditorClassIdentifier:
health: 0
maxHealth: 0
health: 50
maxHealth: 50
isInvulnerable: 0
name:
cost: 0
buildTimer: 0
cost: 20
buildTimer: 1
weaponRange: 40
damage: 1
fireRate: 1
@ -690,4 +690,5 @@ MonoBehaviour:
score: 0
_turretHead: {fileID: 5103935544653627402}
_barrel: {fileID: 5103935545559248087}
targetEnemy: {fileID: 0}
buildingParts: []

View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using AsteroidGame.Entities.Structures.Scripts;
using UnityEngine;
@ -15,53 +16,32 @@ namespace AsteroidGame.Handlers
[Header("Prefabs")] [SerializeField] private List<StructureBase> buildings = new List<StructureBase>();
private void OnClick(InputValue value)
{
Vector3 tempVec = new Vector3();
Plane plane = new Plane(Vector3.up, Vector3.zero);
Ray ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue());
if (plane.Raycast(ray, out float distance))
{
tempVec = ray.GetPoint(distance);
}
//var tempVec = new Vector3(Mouse.current.position.ReadValue().x, Mouse.current.position.ReadValue().y, -_camera.transform.position.z);
// var point = _camera.ScreenToWorldPoint(tempVec);
// var ray = _camera.ScreenPointToRay(tempVec);
// print($"Mouse:{Mouse.current.position.ReadValue()} world:{point}");
// if (!Physics.Raycast(ray, out var hit)) return;
// var pos = hit.transform.position;
// print($"{hit.transform.name} was hit! world:{pos}");
Instantiate(buildings[buildingSelector], tempVec, Quaternion.identity, transform);
//var spawnPoint = new Vector3(1, 1, 0);
//Instantiate(buildings[buildingSelector], spawnPoint, Quaternion.identity, transform);
#region Private
// print($"BuildManagerClick");
[SerializeField] private List<StructureBase> activeBuildings;
private Vector3 _tempVec;
private Plane _buildPlane;
private StructureBase _tempSB;
#endregion
private void OnEnable()
{
activeBuildings.Clear();
}
// public void PlaceStructure(GameObject _tileGO)
// {
// Tile _tile = _tileGO.GetComponentInChildren<Tile>();
//
//
// Debug.Log($"Placing tower on Tile: {_tileGO.transform.position}");
// Debug.Log($"Placing tower on Node: {_node.coordinates}");
//
// if (scoreHandler.CurrentBalance - buildings[buildingSelector].Cost < 0)
// {
// print("Insufficient Funds!");
// return;
// }
// else
// {
// scoreHandler.ModifyWealth(-buildings[buildingSelector].Cost);
// }
// }
private void OnClick(InputValue value)
{
_buildPlane = new Plane(Vector3.up, Vector3.zero);
Ray ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue());
if (_buildPlane.Raycast(ray, out float distance))
{
_tempVec = ray.GetPoint(distance);
}
_tempSB = Instantiate(buildings[buildingSelector], _tempVec, Quaternion.identity, transform);
// public Vector2Int GetVector2(GameObject _o)
// {
// return new Vector2Int((Mathf.RoundToInt(_o.transform.position.x) / 10),
// (Mathf.RoundToInt(_o.transform.position.z / 10)));
// }
activeBuildings.Add(_tempSB);
}
}
}

View File

@ -1,49 +0,0 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &8724214045091603641
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 6508452906343485956}
- component: {fileID: 3682645643528836357}
m_Layer: 0
m_Name: BuildingHandler
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &6508452906343485956
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8724214045091603641}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 46.822308, y: -168.60272, z: 143.78717}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &3682645643528836357
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8724214045091603641}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 51bf353b43f7b2448bbb3088d78ee8a6, type: 3}
m_Name:
m_EditorClassIdentifier:
scoreHandler: {fileID: 0}
buildings:
- {fileID: 5722631354108134187, guid: 8a965e088bae16643aab3d07ccfd5e2e, type: 3}
- {fileID: 6261550781972269396, guid: dcf90521dd3b06548b2a43a02a5f6dc3, type: 3}

View File

@ -1,7 +0,0 @@
fileFormatVersion: 2
guid: cbeeac603faf0334b8a47a73094ea1c1
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,55 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Pool;
namespace AsteroidGame.Handlers
{
public class EnemyHandler : MonoBehaviour
{
[Header("Parameters")]
[SerializeField] [Range(0.1f, 60f)] float spawnRate = 60f;
[SerializeField] int objectPoolSize = 15;
[Header("Prefabs")]
[SerializeField] GameObject objectPool;
[SerializeField] List<GameObject> enemyPrefabs = new List<GameObject>();
[Header("Lists")]
[SerializeField] List<GameObject> enemyPools = new List<GameObject>();
[SerializeField] List<GameObject> allEnemies = new List<GameObject>();
private void Start()
{
}
public void AddEnemyToAllEnemies(GameObject _enemy)
{
allEnemies.Add(_enemy);
}
public void RemoveEnemy(GameObject _enemy)
{
allEnemies.Remove(_enemy);
}
public List<GameObject> ReturnAllEnemies()
{
return allEnemies;
}
public Vector2Int GetCoordinatesFromPosition(Vector3 position)
{
Vector2Int coordinates = new Vector2Int();
coordinates.x = Mathf.RoundToInt(position.x / UnityEditor.EditorSnapSettings.move.x);
coordinates.y = Mathf.RoundToInt(position.z / UnityEditor.EditorSnapSettings.move.z);
return coordinates;
}
public void NotifyEnemiesOfNewPath()
{
BroadcastMessage("RecalculatePath", SendMessageOptions.DontRequireReceiver);
}
}
}

View File

@ -236,6 +236,7 @@ Transform:
m_Children:
- {fileID: 436649171}
- {fileID: 1158682046}
- {fileID: 1047643964}
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@ -244,6 +245,56 @@ Transform:
m_CorrespondingSourceObject: {fileID: 8083988910661828847, guid: c2b4fe01faa847f4b976b5539477e1ec, type: 3}
m_PrefabInstance: {fileID: 1191794244}
m_PrefabAsset: {fileID: 0}
--- !u!1 &1047643963
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1047643964}
- component: {fileID: 1047643965}
m_Layer: 0
m_Name: EnemyHandler
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1047643964
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1047643963}
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_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 142965241}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1047643965
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1047643963}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 62a3dc5f11a44cb4d9faccb4fda8599f, type: 3}
m_Name:
m_EditorClassIdentifier:
spawnRate: 60
objectPoolSize: 15
objectPool: {fileID: 0}
enemyPrefabs: []
enemyPools: []
allEnemies: []
--- !u!1 &1158682045
GameObject:
m_ObjectHideFlags: 0
@ -292,6 +343,7 @@ MonoBehaviour:
buildingSelector: 0
buildings:
- {fileID: 8787361557661825162, guid: 534f8d15e0c83c646887bebfda2bdfd6, type: 3}
activeBuildings: []
--- !u!1001 &1191794244
PrefabInstance:
m_ObjectHideFlags: 0
@ -307,34 +359,6 @@ PrefabInstance:
propertyPath: m_RootOrder
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8083988910661828847, guid: c2b4fe01faa847f4b976b5539477e1ec, type: 3}
propertyPath: m_LocalPosition.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8083988910661828847, guid: c2b4fe01faa847f4b976b5539477e1ec, type: 3}
propertyPath: m_LocalPosition.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8083988910661828847, guid: c2b4fe01faa847f4b976b5539477e1ec, type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8083988910661828847, guid: c2b4fe01faa847f4b976b5539477e1ec, type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 8083988910661828847, guid: c2b4fe01faa847f4b976b5539477e1ec, type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8083988910661828847, guid: c2b4fe01faa847f4b976b5539477e1ec, type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8083988910661828847, guid: c2b4fe01faa847f4b976b5539477e1ec, type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8083988910661828847, guid: c2b4fe01faa847f4b976b5539477e1ec, type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
@ -587,34 +611,6 @@ PrefabInstance:
propertyPath: m_RootOrder
value: 2
objectReference: {fileID: 0}
- target: {fileID: 8324879816836607384, guid: 534f8d15e0c83c646887bebfda2bdfd6, type: 3}
propertyPath: m_LocalPosition.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8324879816836607384, guid: 534f8d15e0c83c646887bebfda2bdfd6, type: 3}
propertyPath: m_LocalPosition.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8324879816836607384, guid: 534f8d15e0c83c646887bebfda2bdfd6, type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8324879816836607384, guid: 534f8d15e0c83c646887bebfda2bdfd6, type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 8324879816836607384, guid: 534f8d15e0c83c646887bebfda2bdfd6, type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8324879816836607384, guid: 534f8d15e0c83c646887bebfda2bdfd6, type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8324879816836607384, guid: 534f8d15e0c83c646887bebfda2bdfd6, type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8324879816836607384, guid: 534f8d15e0c83c646887bebfda2bdfd6, type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0