From eef0a307d0967687f086f94054eab55b6dcc878e Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 7 Nov 2020 02:21:50 +0100 Subject: [PATCH] Remove enemy on many hits, multiple rocket hits Also added an object pool limit but buffing the rockets seemed to be better --- Assets/Prefabs/Enemy.prefab | 1 + Assets/Scenes/SampleScene.unity | 87 +++++++++++++++++++++++++++++-- Assets/Scripts/EnemyController.cs | 35 +++++++++++-- Assets/Scripts/EnemySpawner.cs | 1 + Assets/Scripts/ObjectPool.cs | 7 ++- Assets/Scripts/RocketScript.cs | 12 +++-- 6 files changed, 132 insertions(+), 11 deletions(-) diff --git a/Assets/Prefabs/Enemy.prefab b/Assets/Prefabs/Enemy.prefab index 9a942fe..9227edd 100644 --- a/Assets/Prefabs/Enemy.prefab +++ b/Assets/Prefabs/Enemy.prefab @@ -146,3 +146,4 @@ MonoBehaviour: target: {fileID: 0} speed: 6.5 flyForce: 30 + finalHealth: 3 diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index a96b486..4280c2b 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -121,6 +121,87 @@ NavMeshSettings: debug: m_Flags: 0 m_NavMeshData: {fileID: 0} +--- !u!1 &416234157 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 416234159} + - component: {fileID: 416234158} + m_Layer: 0 + m_Name: cloudLayer1 + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!212 &416234158 +SpriteRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 416234157} + m_Enabled: 1 + m_CastShadows: 0 + m_ReceiveShadows: 0 + m_DynamicOccludee: 1 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RayTracingMode: 0 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 1 + m_SelectedEditorRenderState: 0 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 1324642093 + m_SortingLayer: -3 + m_SortingOrder: 0 + m_Sprite: {fileID: 21300000, guid: 8039604243b9466ab89d4474e2290aed, type: 3} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_FlipX: 0 + m_FlipY: 0 + m_DrawMode: 0 + m_Size: {x: 10.24, y: 4} + m_AdaptiveModeThreshold: 0.5 + m_SpriteTileMode: 0 + m_WasSpriteAssigned: 1 + m_MaskInteraction: 0 + m_SpriteSortPoint: 0 +--- !u!4 &416234159 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 416234157} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: -5.46, y: 3.54, 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!1 &996257474 GameObject: m_ObjectHideFlags: 0 @@ -419,9 +500,9 @@ SpriteRenderer: m_LightmapParameters: {fileID: 0} m_SortingLayerID: 1324642093 m_SortingLayer: -3 - m_SortingOrder: 0 + m_SortingOrder: 1 m_Sprite: {fileID: 21300000, guid: 945baed7357a5d9c69e6f65181575af1, type: 3} - m_Color: {r: 0, g: 0.5, b: 0.06972909, a: 1} + m_Color: {r: 0.5707547, g: 1, b: 0.63111734, a: 1} m_FlipX: 0 m_FlipY: 0 m_DrawMode: 0 @@ -820,7 +901,7 @@ MonoBehaviour: m_EditorClassIdentifier: spawnPos: {fileID: 1692511764} timeBetweenSpawns: 2 - maxEnemyCount: 3 + maxEnemyCount: 6 enemyPrefab: {fileID: 7250562544566202694, guid: ef6e23d6fe1e28e8c809679081854c6a, type: 3} target: {fileID: 2053847422} diff --git a/Assets/Scripts/EnemyController.cs b/Assets/Scripts/EnemyController.cs index dd7ec99..51a40c1 100644 --- a/Assets/Scripts/EnemyController.cs +++ b/Assets/Scripts/EnemyController.cs @@ -1,13 +1,18 @@ -using System.Collections; +using System; +using System.Collections; using System.Collections.Generic; using UnityEngine; +using Random = System.Random; public class EnemyController : MonoBehaviour { public Transform target; public float speed; public float flyForce; + public short finalHealth = 3; private Rigidbody2D _rb; + private short _hitsToRemove; + private Random _random = new Random(); // Start is called before the first frame update void Start() @@ -15,6 +20,11 @@ public class EnemyController : MonoBehaviour _rb = GetComponent(); } + private void OnEnable() + { + _hitsToRemove = finalHealth; + } + // Update is called once per frame void FixedUpdate() { @@ -33,7 +43,8 @@ public class EnemyController : MonoBehaviour } diff.Normalize(); - _rb.AddForce(diff * (speed * _rb.mass * _rb.gravityScale)); + float sp = ((float) _random.NextDouble() / 2f + 1f) * speed; //1 és 1.5 közötti szorzó + _rb.AddForce(diff * (sp * _rb.mass * _rb.gravityScale)); if (diff.x * transform.localScale.x < 0) //Ha másfelé néz, mint amerre megy { var scale = tr.localScale; @@ -42,7 +53,7 @@ public class EnemyController : MonoBehaviour } } - public void Die() + private void Die() { _rb.mass = 0.00001f; _rb.gravityScale = 0.01f; @@ -50,4 +61,22 @@ public class EnemyController : MonoBehaviour } public bool IsAlive() => _rb.mass > 0.001f; + + private void HitWhileFlying() + { + _hitsToRemove--; + if (_hitsToRemove == 0) + { + _rb.velocity = Vector2.zero; + gameObject.SetActive(false); + } + } + + public void Hit() + { + if (IsAlive()) + Die(); + else + HitWhileFlying(); + } } diff --git a/Assets/Scripts/EnemySpawner.cs b/Assets/Scripts/EnemySpawner.cs index a79e5bd..ea545d7 100644 --- a/Assets/Scripts/EnemySpawner.cs +++ b/Assets/Scripts/EnemySpawner.cs @@ -29,6 +29,7 @@ public class EnemySpawner : MonoBehaviour { var pos = transform.position + _diff; var enemy = _pool.GetObject(); + if (enemy is null) break; enemy.transform.position = new Vector3(pos.x, spawnPos.position.y, pos.z); var rb = enemy.GetComponent(); rb.mass = 1f; diff --git a/Assets/Scripts/ObjectPool.cs b/Assets/Scripts/ObjectPool.cs index 0d3d419..4a6bfb3 100644 --- a/Assets/Scripts/ObjectPool.cs +++ b/Assets/Scripts/ObjectPool.cs @@ -18,9 +18,10 @@ public class ObjectPool /// Visszaad egy új objektumot. Aktiválandó, használat után pedig deaktiválandó. /// /// Egy objektum a poolból. - public GameObject GetObject() + public GameObject GetObject(short maxCount = 0) { GameObject theRocket = null; + int c = 0; foreach (var rocket in _objects) { if (!rocket.activeSelf) @@ -28,6 +29,10 @@ public class ObjectPool theRocket = rocket; break; } + + c++; + if (c == maxCount) + return null; } if (theRocket is null) diff --git a/Assets/Scripts/RocketScript.cs b/Assets/Scripts/RocketScript.cs index c9989ee..625e845 100644 --- a/Assets/Scripts/RocketScript.cs +++ b/Assets/Scripts/RocketScript.cs @@ -8,6 +8,9 @@ public class RocketScript : MonoBehaviour private float _fired; private Rigidbody2D _rb; private bool _goingRight; + private byte _hitCount; + + public byte maxHits = 3; // Start is called before the first frame update void Start() @@ -19,6 +22,7 @@ public class RocketScript : MonoBehaviour { _fired = Time.fixedTime; _goingRight = transform.localScale.x > 0; + _hitCount = 0; } // Update is called once per frame @@ -38,11 +42,11 @@ public class RocketScript : MonoBehaviour { if (!other.gameObject.CompareTag("Enemy")) return; - gameObject.SetActive(false); + if (_hitCount >= maxHits) + gameObject.SetActive(false); _rb.velocity = Vector2.zero; - //other.gameObject.SetActive(false); var ec = other.gameObject.GetComponent(); - if (ec.IsAlive()) - ec.Die(); + ec.Hit(); + _hitCount++; } }