Skip to content

Commit

Permalink
[Add/Update] Add Explosion with pooling, Update RecyclableObject
Browse files Browse the repository at this point in the history
  • Loading branch information
SeungHuLee committed Dec 1, 2019
1 parent d30a0cc commit 79a67a6
Show file tree
Hide file tree
Showing 9 changed files with 271 additions and 26 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions MissileCommander/Assets/Game/Core/BulletLauncher.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -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
167 changes: 167 additions & 0 deletions MissileCommander/Assets/Game/Core/Explosion.prefab
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions MissileCommander/Assets/Game/Core/Explosion.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 4 additions & 20 deletions MissileCommander/Assets/_Scripts/Bullet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bullet> 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;
}
}
Expand Down
22 changes: 18 additions & 4 deletions MissileCommander/Assets/_Scripts/BulletLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
}
}
}
44 changes: 44 additions & 0 deletions MissileCommander/Assets/_Scripts/Explosion.cs
Original file line number Diff line number Diff line change
@@ -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<Rigidbody2D>();
_circleCollider2D = GetComponent<CircleCollider2D>();

_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!");
}
}
}
3 changes: 3 additions & 0 deletions MissileCommander/Assets/_Scripts/Explosion.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 24 additions & 1 deletion MissileCommander/Assets/_Scripts/RecyclableObject.cs
Original file line number Diff line number Diff line change
@@ -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<RecyclableObject> 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;
}
}
}

0 comments on commit 79a67a6

Please sign in to comment.