From 79a67a69e0d134379a4fb84675964127734eef25 Mon Sep 17 00:00:00 2001 From: SEUNGHU LEE Date: Sun, 1 Dec 2019 18:21:48 +0900 Subject: [PATCH] [Add/Update] Add Explosion with pooling, Update RecyclableObject --- .../.idea/contentModel.xml | 3 +- .../Assets/Game/Core/BulletLauncher.prefab | 2 + .../Assets/Game/Core/Explosion.prefab | 167 ++++++++++++++++++ .../Assets/Game/Core/Explosion.prefab.meta | 7 + MissileCommander/Assets/_Scripts/Bullet.cs | 24 +-- .../Assets/_Scripts/BulletLauncher.cs | 22 ++- MissileCommander/Assets/_Scripts/Explosion.cs | 44 +++++ .../Assets/_Scripts/Explosion.cs.meta | 3 + .../Assets/_Scripts/RecyclableObject.cs | 25 ++- 9 files changed, 271 insertions(+), 26 deletions(-) create mode 100644 MissileCommander/Assets/Game/Core/Explosion.prefab create mode 100644 MissileCommander/Assets/Game/Core/Explosion.prefab.meta create mode 100644 MissileCommander/Assets/_Scripts/Explosion.cs create mode 100644 MissileCommander/Assets/_Scripts/Explosion.cs.meta diff --git a/MissileCommander/.idea/.idea.MissileCommander/.idea/contentModel.xml b/MissileCommander/.idea/.idea.MissileCommander/.idea/contentModel.xml index ddd7215..f6c847d 100644 --- a/MissileCommander/.idea/.idea.MissileCommander/.idea/contentModel.xml +++ b/MissileCommander/.idea/.idea.MissileCommander/.idea/contentModel.xml @@ -1,4 +1,4 @@ - + @@ -9,6 +9,7 @@ + diff --git a/MissileCommander/Assets/Game/Core/BulletLauncher.prefab b/MissileCommander/Assets/Game/Core/BulletLauncher.prefab index 3f59951..ebaecb7 100644 --- a/MissileCommander/Assets/Game/Core/BulletLauncher.prefab +++ b/MissileCommander/Assets/Game/Core/BulletLauncher.prefab @@ -158,5 +158,7 @@ MonoBehaviour: m_EditorClassIdentifier: bulletPrefab: {fileID: 649290798713983650, guid: f0b0296c96dfd8b46b8397a3a4160464, type: 3} + explosionPrefab: {fileID: 8426078496689282687, guid: d401f0d58b518e64dbe56122d51a2f31, + type: 3} firePosition: {fileID: 787914998351899525} fireDelay: 0.5 diff --git a/MissileCommander/Assets/Game/Core/Explosion.prefab b/MissileCommander/Assets/Game/Core/Explosion.prefab new file mode 100644 index 0000000..35748fc --- /dev/null +++ b/MissileCommander/Assets/Game/Core/Explosion.prefab @@ -0,0 +1,167 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &2187720768327935215 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1153937608142943101} + - component: {fileID: 8552799194955677352} + m_Layer: 0 + m_Name: Explosion_0 + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &1153937608142943101 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2187720768327935215} + 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: 2650854474031818298} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!212 &8552799194955677352 +SpriteRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2187720768327935215} + m_Enabled: 1 + m_CastShadows: 0 + m_ReceiveShadows: 0 + m_DynamicOccludee: 1 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + 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: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 + m_Sprite: {fileID: 5986030973748356752, guid: 32fd08a7ced0c4831a4dc8b738dbd1d0, + type: 3} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_FlipX: 0 + m_FlipY: 0 + m_DrawMode: 0 + m_Size: {x: 1.4, y: 1.4} + m_AdaptiveModeThreshold: 0.5 + m_SpriteTileMode: 0 + m_WasSpriteAssigned: 1 + m_MaskInteraction: 0 + m_SpriteSortPoint: 0 +--- !u!1 &6469101167713022457 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 2650854474031818298} + - component: {fileID: 336636678138189459} + - component: {fileID: 1962218832182758602} + - component: {fileID: 8426078496689282687} + m_Layer: 0 + m_Name: Explosion + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &2650854474031818298 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 6469101167713022457} + 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: + - {fileID: 1153937608142943101} + m_Father: {fileID: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!50 &336636678138189459 +Rigidbody2D: + serializedVersion: 4 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 6469101167713022457} + m_BodyType: 1 + m_Simulated: 1 + m_UseFullKinematicContacts: 0 + m_UseAutoMass: 0 + m_Mass: 1 + m_LinearDrag: 0 + m_AngularDrag: 0.05 + m_GravityScale: 1 + m_Material: {fileID: 0} + m_Interpolate: 0 + m_SleepingMode: 1 + m_CollisionDetection: 0 + m_Constraints: 0 +--- !u!58 &1962218832182758602 +CircleCollider2D: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 6469101167713022457} + m_Enabled: 1 + m_Density: 1 + m_Material: {fileID: 0} + m_IsTrigger: 1 + m_UsedByEffector: 0 + m_UsedByComposite: 0 + m_Offset: {x: 0, y: 0} + serializedVersion: 2 + m_Radius: 0.5 +--- !u!114 &8426078496689282687 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 6469101167713022457} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: f169c4ff18df452ea48010f9ab81b821, type: 3} + m_Name: + m_EditorClassIdentifier: + _timeToRemove: 1 diff --git a/MissileCommander/Assets/Game/Core/Explosion.prefab.meta b/MissileCommander/Assets/Game/Core/Explosion.prefab.meta new file mode 100644 index 0000000..f2cc085 --- /dev/null +++ b/MissileCommander/Assets/Game/Core/Explosion.prefab.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: d401f0d58b518e64dbe56122d51a2f31 +PrefabImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/MissileCommander/Assets/_Scripts/Bullet.cs b/MissileCommander/Assets/_Scripts/Bullet.cs index ed0ba96..ff781c7 100644 --- a/MissileCommander/Assets/_Scripts/Bullet.cs +++ b/MissileCommander/Assets/_Scripts/Bullet.cs @@ -6,40 +6,24 @@ namespace MissileCommander public class Bullet : RecyclableObject { [SerializeField] private float moveSpeed = 5f; - - private Transform _cachedTransform; - private Transform CachedTransform => _cachedTransform ? _cachedTransform : (_cachedTransform = transform); - private Vector3 _targetPosition; [SerializeField] private float sqrApproximateOffset = 0.01f; - private bool _isActivated = false; - public event Action OnDestroyed = delegate { }; private void Update() { - if (!_isActivated) { return; } + if (!isActivated) { return; } CachedTransform.position += Time.deltaTime * moveSpeed * transform.up; if (IsArrivedToTarget()) { - _isActivated = false; - OnDestroyed(this); + isActivated = false; + onDestroyed?.Invoke(this); } } - public void Activate(Vector3 startPos, Vector3 targetPos) - { - this._targetPosition = targetPos; - - Vector3 direction = (targetPos - startPos).normalized; - CachedTransform.SetPositionAndRotation(startPos, - Quaternion.LookRotation(CachedTransform.forward, direction)); - _isActivated = true; - } - private bool IsArrivedToTarget() { - float sqrDist = (_targetPosition - CachedTransform.position).sqrMagnitude; + float sqrDist = (targetPosition - CachedTransform.position).sqrMagnitude; return sqrDist < sqrApproximateOffset; } } diff --git a/MissileCommander/Assets/_Scripts/BulletLauncher.cs b/MissileCommander/Assets/_Scripts/BulletLauncher.cs index 8184890..84ed67e 100644 --- a/MissileCommander/Assets/_Scripts/BulletLauncher.cs +++ b/MissileCommander/Assets/_Scripts/BulletLauncher.cs @@ -6,16 +6,19 @@ namespace MissileCommander public class BulletLauncher : MonoBehaviour { [SerializeField] private Bullet bulletPrefab; + [SerializeField] private Explosion explosionPrefab; [SerializeField] private Transform firePosition; [SerializeField] private float fireDelay = 0.5f; private float _elapsedFireTime; private bool _canShoot = true; private Factory _bulletFactory; + private Factory _explosionFactory; private void Awake() { _bulletFactory = new Factory(bulletPrefab); + _explosionFactory = new Factory(explosionPrefab); } private void Update() @@ -36,17 +39,28 @@ public void OnFireButtonPressed(Vector3 mousePosition) if (!_canShoot) { return; } // Instantiate Bullet - Bullet bullet = _bulletFactory.Get() as Bullet; + RecyclableObject bullet = _bulletFactory.Get(); bullet.Activate(firePosition.position, mousePosition); - bullet.OnDestroyed += OnBulletDestroyed; + bullet.onDestroyed += OnBulletExplode; _canShoot = false; } - public void OnBulletDestroyed(Bullet usedBullet) + public void OnBulletExplode(RecyclableObject usedBullet) { - usedBullet.OnDestroyed -= OnBulletDestroyed; + Vector3 lastBulletPos = usedBullet.CachedTransform.position; + usedBullet.onDestroyed -= OnBulletExplode; _bulletFactory.ReturnToPool(usedBullet); + + RecyclableObject explosion = _explosionFactory.Get(); + explosion.Activate((lastBulletPos)); + explosion.onDestroyed += OnExplosionDestroyed; + } + + private void OnExplosionDestroyed(RecyclableObject explosion) + { + explosion.onDestroyed -= OnExplosionDestroyed; + _explosionFactory.ReturnToPool(explosion); } } } \ No newline at end of file diff --git a/MissileCommander/Assets/_Scripts/Explosion.cs b/MissileCommander/Assets/_Scripts/Explosion.cs new file mode 100644 index 0000000..6b58b82 --- /dev/null +++ b/MissileCommander/Assets/_Scripts/Explosion.cs @@ -0,0 +1,44 @@ +using System; +using UnityEngine; + +namespace MissileCommander +{ + [RequireComponent(typeof(Rigidbody2D), typeof(CircleCollider2D))] + public class Explosion : RecyclableObject + { + private Rigidbody2D _rigidbody2D; + private CircleCollider2D _circleCollider2D; + + [SerializeField] private float timeToRemove = 1f; + private float _elapsedTime; + + private void Awake() + { + _rigidbody2D = GetComponent(); + _circleCollider2D = GetComponent(); + + _rigidbody2D.bodyType = RigidbodyType2D.Kinematic; + _circleCollider2D.isTrigger = true; + } + + private void Update() + { + if (isActivated) + { + _elapsedTime += Time.deltaTime; + if (_elapsedTime >= timeToRemove) + { + _elapsedTime = 0f; + DestroySelf(); + } + } + } + + private void DestroySelf() + { + isActivated = false; + onDestroyed?.Invoke(this); + Debug.Log($"{gameObject.name} is Destroyed!"); + } + } +} \ No newline at end of file diff --git a/MissileCommander/Assets/_Scripts/Explosion.cs.meta b/MissileCommander/Assets/_Scripts/Explosion.cs.meta new file mode 100644 index 0000000..4ed2807 --- /dev/null +++ b/MissileCommander/Assets/_Scripts/Explosion.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f169c4ff18df452ea48010f9ab81b821 +timeCreated: 1575019860 \ No newline at end of file diff --git a/MissileCommander/Assets/_Scripts/RecyclableObject.cs b/MissileCommander/Assets/_Scripts/RecyclableObject.cs index b5b0540..f588253 100644 --- a/MissileCommander/Assets/_Scripts/RecyclableObject.cs +++ b/MissileCommander/Assets/_Scripts/RecyclableObject.cs @@ -1,9 +1,32 @@ -using UnityEngine; +using System; +using UnityEngine; namespace MissileCommander { public class RecyclableObject : MonoBehaviour { + protected bool isActivated = false; + protected Vector3 targetPosition; + public Action onDestroyed; + private Transform _cachedTransform; + public Transform CachedTransform => _cachedTransform ? _cachedTransform : (_cachedTransform = transform); + + + public virtual void Activate(Vector3 position) + { + isActivated = true; + transform.position = position; + } + + public virtual void Activate(Vector3 startPos, Vector3 targetPos) + { + this.targetPosition = targetPos; + + Vector3 direction = (targetPos - startPos).normalized; + CachedTransform.SetPositionAndRotation(startPos, + Quaternion.LookRotation(CachedTransform.forward, direction)); + isActivated = true; + } } } \ No newline at end of file