Enemy object pooling (class), enemy flight

This commit is contained in:
Norbi Peti 2020-11-07 00:51:23 +01:00
parent 78ed658fce
commit d2750bd344
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
9 changed files with 99 additions and 27 deletions

View file

@ -58,6 +58,7 @@
<Compile Include="Assets\Scripts\CameraController.cs" /> <Compile Include="Assets\Scripts\CameraController.cs" />
<Compile Include="Assets\Scripts\EnemyController.cs" /> <Compile Include="Assets\Scripts\EnemyController.cs" />
<Compile Include="Assets\Scripts\EnemySpawner.cs" /> <Compile Include="Assets\Scripts\EnemySpawner.cs" />
<Compile Include="Assets\Scripts\ObjectPool.cs" />
<Compile Include="Assets\Scripts\OwnCharacterController.cs" /> <Compile Include="Assets\Scripts\OwnCharacterController.cs" />
<Compile Include="Assets\Scripts\Parallaxing.cs" /> <Compile Include="Assets\Scripts\Parallaxing.cs" />
<Compile Include="Assets\Scripts\RocketScript.cs" /> <Compile Include="Assets\Scripts\RocketScript.cs" />

View file

@ -19,7 +19,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 1 m_IsActive: 0
--- !u!4 &7250562544566202698 --- !u!4 &7250562544566202698
Transform: Transform:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -144,4 +144,5 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
target: {fileID: 0} target: {fileID: 0}
speed: 6 speed: 6.5
flyForce: 30

View file

@ -804,7 +804,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 4f400b7976b98bc18866f672d33d506a, type: 3} m_Script: {fileID: 11500000, guid: 4f400b7976b98bc18866f672d33d506a, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
prefab: {fileID: 5991448782009560204, guid: 7debf3c35b28387fdbec1c1c9942afae, type: 3} prefab: {fileID: 6006275050469676560, guid: 7debf3c35b28387fdbec1c1c9942afae, type: 3}
firePoint: {fileID: 1043406434} firePoint: {fileID: 1043406434}
--- !u!114 &2053847427 --- !u!114 &2053847427
MonoBehaviour: MonoBehaviour:
@ -819,8 +819,8 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
spawnPos: {fileID: 1692511764} spawnPos: {fileID: 1692511764}
timeBetweenSpawns: 5 timeBetweenSpawns: 2
maxEnemyCount: 3 maxEnemyCount: 3
enemyPrefab: {fileID: 7250562544566202698, guid: ef6e23d6fe1e28e8c809679081854c6a, enemyPrefab: {fileID: 7250562544566202694, guid: ef6e23d6fe1e28e8c809679081854c6a,
type: 3} type: 3}
target: {fileID: 2053847422} target: {fileID: 2053847422}

View file

@ -6,6 +6,7 @@ public class EnemyController : MonoBehaviour
{ {
public Transform target; public Transform target;
public float speed; public float speed;
public float flyForce;
private Rigidbody2D _rb; private Rigidbody2D _rb;
// Start is called before the first frame update // Start is called before the first frame update
@ -17,8 +18,36 @@ public class EnemyController : MonoBehaviour
// Update is called once per frame // Update is called once per frame
void FixedUpdate() 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(); 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;
}

View file

@ -7,15 +7,17 @@ public class EnemySpawner : MonoBehaviour
public Transform spawnPos; public Transform spawnPos;
public float timeBetweenSpawns; public float timeBetweenSpawns;
public short maxEnemyCount; public short maxEnemyCount;
public Transform enemyPrefab; public GameObject enemyPrefab;
public Transform target; public Transform target;
private Vector3 _diff; private Vector3 _diff;
private float _lastSpawn; private float _lastSpawn;
private readonly Random _random = new Random(); private readonly Random _random = new Random();
private ObjectPool _pool;
private void Start() private void Start()
{ {
_diff = spawnPos.position - transform.position; _diff = spawnPos.position - transform.position;
enemyPrefab.GetComponent<EnemyController>().target = target; enemyPrefab.GetComponent<EnemyController>().target = target;
_pool = new ObjectPool(enemyPrefab.gameObject, 10);
} }
private void FixedUpdate() private void FixedUpdate()
@ -26,7 +28,15 @@ public class EnemySpawner : MonoBehaviour
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
var pos = transform.position + _diff; 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<Rigidbody2D>();
rb.mass = 1f;
rb.gravityScale = 1f;
rb.rotation = 0f;
rb.freezeRotation = true;
enemy.SetActive(true);
rb.rotation = 0f;
} }
_lastSpawn = Time.fixedTime; _lastSpawn = Time.fixedTime;

View file

@ -0,0 +1,37 @@
using System.Collections.Generic;
using UnityEngine;
public class ObjectPool
{
public ObjectPool(GameObject prefab, int initialSize)
{
_prefab = prefab;
_objects = new List<GameObject>(initialSize);
for (int i = 0; i < initialSize; i++)
_objects.Add(Object.Instantiate(_prefab));
}
private List<GameObject> _objects;
private GameObject _prefab;
/// <summary>
/// Visszaad egy új objektumot. Aktiválandó, használat után pedig deaktiválandó.
/// </summary>
/// <returns>Egy objektum a poolból.</returns>
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;
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: fcb2d922d49b4744a998fd60258a9ee5
timeCreated: 1604702138

View file

@ -40,6 +40,9 @@ public class RocketScript : MonoBehaviour
return; return;
gameObject.SetActive(false); gameObject.SetActive(false);
_rb.velocity = Vector2.zero; _rb.velocity = Vector2.zero;
Destroy(other.gameObject); //other.gameObject.SetActive(false);
var ec = other.gameObject.GetComponent<EnemyController>();
if (ec.IsAlive())
ec.Die();
} }
} }

View file

@ -5,15 +5,14 @@ using UnityEngine;
public class WeaponFireController : MonoBehaviour public class WeaponFireController : MonoBehaviour
{ {
public Rigidbody2D prefab; public GameObject prefab;
public Transform firePoint; public Transform firePoint;
private List<Rigidbody2D> rockets = new List<Rigidbody2D>(10); private ObjectPool _pool;
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
for (int i = 0; i < 10; i++) _pool = new ObjectPool(prefab, 10);
rockets.Add(Instantiate(prefab));
} }
// Update is called once per frame // Update is called once per frame
@ -21,25 +20,14 @@ public class WeaponFireController : MonoBehaviour
{ {
if(Input.GetButtonDown("Fire1")) if(Input.GetButtonDown("Fire1"))
{ {
Rigidbody2D theRocket = null; var theRocket = _pool.GetObject();
foreach (var rocket in rockets)
{
if (!rocket.gameObject.activeSelf)
{
theRocket = rocket;
break;
}
}
if (theRocket is null)
rockets.Add(theRocket = Instantiate(prefab));
var rocketTransform = theRocket.transform; var rocketTransform = theRocket.transform;
rocketTransform.position = firePoint.position; rocketTransform.position = firePoint.position;
var scale = rocketTransform.localScale; var scale = rocketTransform.localScale;
if (transform.localScale.x * scale.x < 0) if (transform.localScale.x * scale.x < 0)
scale.x *= -1; scale.x *= -1;
rocketTransform.localScale = scale; rocketTransform.localScale = scale;
theRocket.gameObject.SetActive(true); theRocket.SetActive(true);
} }
} }
} }