From d2750bd3448854bbf85a3ed3567fcd2c1355d850 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 7 Nov 2020 00:51:23 +0100 Subject: [PATCH] Enemy object pooling (class), enemy flight --- Assembly-CSharp.csproj | 1 + Assets/Prefabs/Enemy.prefab | 5 ++-- Assets/Scenes/SampleScene.unity | 6 ++--- Assets/Scripts/EnemyController.cs | 33 +++++++++++++++++++++-- Assets/Scripts/EnemySpawner.cs | 14 ++++++++-- Assets/Scripts/ObjectPool.cs | 37 ++++++++++++++++++++++++++ Assets/Scripts/ObjectPool.cs.meta | 3 +++ Assets/Scripts/RocketScript.cs | 5 +++- Assets/Scripts/WeaponFireController.cs | 22 ++++----------- 9 files changed, 99 insertions(+), 27 deletions(-) create mode 100644 Assets/Scripts/ObjectPool.cs create mode 100644 Assets/Scripts/ObjectPool.cs.meta diff --git a/Assembly-CSharp.csproj b/Assembly-CSharp.csproj index 97e3d5c..eea74b0 100644 --- a/Assembly-CSharp.csproj +++ b/Assembly-CSharp.csproj @@ -58,6 +58,7 @@ + diff --git a/Assets/Prefabs/Enemy.prefab b/Assets/Prefabs/Enemy.prefab index 35f29b6..9a942fe 100644 --- a/Assets/Prefabs/Enemy.prefab +++ b/Assets/Prefabs/Enemy.prefab @@ -19,7 +19,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!4 &7250562544566202698 Transform: m_ObjectHideFlags: 0 @@ -144,4 +144,5 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: target: {fileID: 0} - speed: 6 + speed: 6.5 + flyForce: 30 diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 823fde7..a96b486 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -804,7 +804,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 4f400b7976b98bc18866f672d33d506a, type: 3} m_Name: m_EditorClassIdentifier: - prefab: {fileID: 5991448782009560204, guid: 7debf3c35b28387fdbec1c1c9942afae, type: 3} + prefab: {fileID: 6006275050469676560, guid: 7debf3c35b28387fdbec1c1c9942afae, type: 3} firePoint: {fileID: 1043406434} --- !u!114 &2053847427 MonoBehaviour: @@ -819,8 +819,8 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: spawnPos: {fileID: 1692511764} - timeBetweenSpawns: 5 + timeBetweenSpawns: 2 maxEnemyCount: 3 - enemyPrefab: {fileID: 7250562544566202698, guid: ef6e23d6fe1e28e8c809679081854c6a, + enemyPrefab: {fileID: 7250562544566202694, guid: ef6e23d6fe1e28e8c809679081854c6a, type: 3} target: {fileID: 2053847422} diff --git a/Assets/Scripts/EnemyController.cs b/Assets/Scripts/EnemyController.cs index 49591f8..dd7ec99 100644 --- a/Assets/Scripts/EnemyController.cs +++ b/Assets/Scripts/EnemyController.cs @@ -6,6 +6,7 @@ public class EnemyController : MonoBehaviour { public Transform target; public float speed; + public float flyForce; private Rigidbody2D _rb; // Start is called before the first frame update @@ -17,8 +18,36 @@ public class EnemyController : MonoBehaviour // Update is called once per frame void FixedUpdate() { - var diff = target.position - transform.position; + var tr = transform; + var diff = target.position - tr.position; + if (_rb.mass < 0.01f) + { //Már lelőttük + if (diff.magnitude > 10) + { //Ha már túl messze van + _rb.velocity = Vector2.zero; + gameObject.SetActive(false); + } + + _rb.AddForce(new Vector2(0f, flyForce * _rb.mass * _rb.gravityScale)); //Ne maradjon véletlenül útban + return; + } + diff.Normalize(); - _rb.AddForce(diff * speed); + _rb.AddForce(diff * (speed * _rb.mass * _rb.gravityScale)); + if (diff.x * transform.localScale.x < 0) //Ha másfelé néz, mint amerre megy + { + var scale = tr.localScale; + scale.x *= -1; + tr.localScale = scale; + } } + + public void Die() + { + _rb.mass = 0.00001f; + _rb.gravityScale = 0.01f; + _rb.freezeRotation = false; + } + + public bool IsAlive() => _rb.mass > 0.001f; } diff --git a/Assets/Scripts/EnemySpawner.cs b/Assets/Scripts/EnemySpawner.cs index e43e65f..a79e5bd 100644 --- a/Assets/Scripts/EnemySpawner.cs +++ b/Assets/Scripts/EnemySpawner.cs @@ -7,15 +7,17 @@ public class EnemySpawner : MonoBehaviour public Transform spawnPos; public float timeBetweenSpawns; public short maxEnemyCount; - public Transform enemyPrefab; + public GameObject enemyPrefab; public Transform target; private Vector3 _diff; private float _lastSpawn; private readonly Random _random = new Random(); + private ObjectPool _pool; private void Start() { _diff = spawnPos.position - transform.position; enemyPrefab.GetComponent().target = target; + _pool = new ObjectPool(enemyPrefab.gameObject, 10); } private void FixedUpdate() @@ -26,7 +28,15 @@ public class EnemySpawner : MonoBehaviour for (int i = 0; i < count; i++) { var pos = transform.position + _diff; - Instantiate(enemyPrefab).position = new Vector3(pos.x, spawnPos.position.y, pos.z); + var enemy = _pool.GetObject(); + enemy.transform.position = new Vector3(pos.x, spawnPos.position.y, pos.z); + var rb = enemy.GetComponent(); + rb.mass = 1f; + rb.gravityScale = 1f; + rb.rotation = 0f; + rb.freezeRotation = true; + enemy.SetActive(true); + rb.rotation = 0f; } _lastSpawn = Time.fixedTime; diff --git a/Assets/Scripts/ObjectPool.cs b/Assets/Scripts/ObjectPool.cs new file mode 100644 index 0000000..0d3d419 --- /dev/null +++ b/Assets/Scripts/ObjectPool.cs @@ -0,0 +1,37 @@ +using System.Collections.Generic; +using UnityEngine; + +public class ObjectPool +{ + public ObjectPool(GameObject prefab, int initialSize) + { + _prefab = prefab; + _objects = new List(initialSize); + for (int i = 0; i < initialSize; i++) + _objects.Add(Object.Instantiate(_prefab)); + } + + private List _objects; + private GameObject _prefab; + + /// + /// Visszaad egy új objektumot. Aktiválandó, használat után pedig deaktiválandó. + /// + /// Egy objektum a poolból. + public GameObject GetObject() + { + GameObject theRocket = null; + foreach (var rocket in _objects) + { + if (!rocket.activeSelf) + { + theRocket = rocket; + break; + } + } + + if (theRocket is null) + _objects.Add(theRocket = Object.Instantiate(_prefab)); + return theRocket; + } +} \ No newline at end of file diff --git a/Assets/Scripts/ObjectPool.cs.meta b/Assets/Scripts/ObjectPool.cs.meta new file mode 100644 index 0000000..d4dea81 --- /dev/null +++ b/Assets/Scripts/ObjectPool.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: fcb2d922d49b4744a998fd60258a9ee5 +timeCreated: 1604702138 \ No newline at end of file diff --git a/Assets/Scripts/RocketScript.cs b/Assets/Scripts/RocketScript.cs index d0aa799..c9989ee 100644 --- a/Assets/Scripts/RocketScript.cs +++ b/Assets/Scripts/RocketScript.cs @@ -40,6 +40,9 @@ public class RocketScript : MonoBehaviour return; gameObject.SetActive(false); _rb.velocity = Vector2.zero; - Destroy(other.gameObject); + //other.gameObject.SetActive(false); + var ec = other.gameObject.GetComponent(); + if (ec.IsAlive()) + ec.Die(); } } diff --git a/Assets/Scripts/WeaponFireController.cs b/Assets/Scripts/WeaponFireController.cs index 19ab889..ac03000 100644 --- a/Assets/Scripts/WeaponFireController.cs +++ b/Assets/Scripts/WeaponFireController.cs @@ -5,15 +5,14 @@ using UnityEngine; public class WeaponFireController : MonoBehaviour { - public Rigidbody2D prefab; + public GameObject prefab; public Transform firePoint; - private List rockets = new List(10); + private ObjectPool _pool; // Start is called before the first frame update void Start() { - for (int i = 0; i < 10; i++) - rockets.Add(Instantiate(prefab)); + _pool = new ObjectPool(prefab, 10); } // Update is called once per frame @@ -21,25 +20,14 @@ public class WeaponFireController : MonoBehaviour { if(Input.GetButtonDown("Fire1")) { - Rigidbody2D theRocket = null; - foreach (var rocket in rockets) - { - if (!rocket.gameObject.activeSelf) - { - theRocket = rocket; - break; - } - } - - if (theRocket is null) - rockets.Add(theRocket = Instantiate(prefab)); + var theRocket = _pool.GetObject(); var rocketTransform = theRocket.transform; rocketTransform.position = firePoint.position; var scale = rocketTransform.localScale; if (transform.localScale.x * scale.x < 0) scale.x *= -1; rocketTransform.localScale = scale; - theRocket.gameObject.SetActive(true); + theRocket.SetActive(true); } } }