From ae09d98a3b572fe47ef950a14388577e2e0bfb67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Nu=C8=9Biu?= Date: Sat, 22 Jun 2019 17:37:26 +0300 Subject: [PATCH] Implement UpgradeManager --- Assets/DevMocks/TestUpgrade.prefab | 121 ++++++++++++++++++ Assets/DevMocks/TestUpgrade.prefab.meta | 7 + Assets/Prefabs/Managers/GameManager.prefab | 13 ++ Assets/Scenes/SampleScene.unity | 23 +++- Assets/Scripts/src/Enemy/DumbEnemy.cs | 14 +- Assets/Scripts/src/Helpers/ArrayExtensions.cs | 6 +- Assets/Scripts/src/Interfaces/IUpgrade.cs | 10 ++ .../Scripts/src/Interfaces/IUpgrade.cs.meta | 3 + Assets/Scripts/src/Managers/GameManager.cs | 7 + Assets/Scripts/src/Managers/UpgradeManager.cs | 16 +++ .../src/Managers/UpgradeManager.cs.meta | 3 + Assets/Scripts/src/Player/PlayerController.cs | 1 + Assets/Scripts/src/Upgrade.meta | 3 + Assets/Scripts/src/Upgrade/TestUpgrade.cs | 23 ++++ .../Scripts/src/Upgrade/TestUpgrade.cs.meta | 3 + Assets/Scripts/src/Wall/DestructibleWall.cs | 11 +- 16 files changed, 247 insertions(+), 17 deletions(-) create mode 100644 Assets/DevMocks/TestUpgrade.prefab create mode 100644 Assets/DevMocks/TestUpgrade.prefab.meta create mode 100644 Assets/Scripts/src/Interfaces/IUpgrade.cs create mode 100644 Assets/Scripts/src/Interfaces/IUpgrade.cs.meta create mode 100644 Assets/Scripts/src/Managers/UpgradeManager.cs create mode 100644 Assets/Scripts/src/Managers/UpgradeManager.cs.meta create mode 100644 Assets/Scripts/src/Upgrade.meta create mode 100644 Assets/Scripts/src/Upgrade/TestUpgrade.cs create mode 100644 Assets/Scripts/src/Upgrade/TestUpgrade.cs.meta diff --git a/Assets/DevMocks/TestUpgrade.prefab b/Assets/DevMocks/TestUpgrade.prefab new file mode 100644 index 0000000..aefc338 --- /dev/null +++ b/Assets/DevMocks/TestUpgrade.prefab @@ -0,0 +1,121 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &4585348266300200282 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 4585348266300200286} + - component: {fileID: 4585348266300200285} + - component: {fileID: 4585348266300200284} + - component: {fileID: 4585348266300200283} + m_Layer: 0 + m_Name: TestUpgrade + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &4585348266300200286 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4585348266300200282} + 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: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!61 &4585348266300200285 +BoxCollider2D: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4585348266300200282} + 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} + m_SpriteTilingProperty: + border: {x: 0, y: 0, z: 0, w: 0} + pivot: {x: 0, y: 0} + oldSize: {x: 1, y: 1} + newSize: {x: 1, y: 1} + adaptiveTilingThreshold: 0.5 + drawMode: 0 + adaptiveTiling: 0 + m_AutoTiling: 0 + serializedVersion: 2 + m_Size: {x: 1, y: 1} + m_EdgeRadius: 0 +--- !u!212 &4585348266300200284 +SpriteRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4585348266300200282} + 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_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 0 + 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: 10 + m_Sprite: {fileID: 21300000, guid: 753e29d30d8044605aede9853c9308c7, type: 3} + m_Color: {r: 0.0509078, g: 0.36098272, b: 0.9811321, a: 1} + m_FlipX: 0 + m_FlipY: 0 + m_DrawMode: 0 + m_Size: {x: 1, y: 1} + m_AdaptiveModeThreshold: 0.5 + m_SpriteTileMode: 0 + m_WasSpriteAssigned: 1 + m_MaskInteraction: 0 + m_SpriteSortPoint: 0 +--- !u!114 &4585348266300200283 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4585348266300200282} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: daf1c278fd544eae9246c4abe6bf611e, type: 3} + m_Name: + m_EditorClassIdentifier: diff --git a/Assets/DevMocks/TestUpgrade.prefab.meta b/Assets/DevMocks/TestUpgrade.prefab.meta new file mode 100644 index 0000000..e9f47c5 --- /dev/null +++ b/Assets/DevMocks/TestUpgrade.prefab.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 2ea1eb2b6b05340a4a99948a6eb818b2 +PrefabImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Prefabs/Managers/GameManager.prefab b/Assets/Prefabs/Managers/GameManager.prefab index aab07ff..539f2dd 100644 --- a/Assets/Prefabs/Managers/GameManager.prefab +++ b/Assets/Prefabs/Managers/GameManager.prefab @@ -11,6 +11,7 @@ GameObject: - component: {fileID: 158905820630581587} - component: {fileID: 158905820630581586} - component: {fileID: 158905820630581596} + - component: {fileID: 6316647355651322741} m_Layer: 0 m_Name: GameManager m_TagString: Untagged @@ -66,3 +67,15 @@ MonoBehaviour: enemiesPrefab: - {fileID: 4346602752014574748, guid: e719de70e335f4e49ae40cc6731309d4, type: 3} - {fileID: 7795087027144463245, guid: fe8fb4e4a7e127648b306469b723d81e, type: 3} +--- !u!114 &6316647355651322741 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 158905820630581597} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: e64481d2e18e4b6ca4f22926a5061450, type: 3} + m_Name: + m_EditorClassIdentifier: diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index ecaf692..b1f0ec3 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -404,7 +404,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 519420028} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 10.72334, y: -4.0430503, z: -10} + m_LocalPosition: {x: 10.75168, y: -4.0430503, z: -10} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} @@ -562,6 +562,17 @@ PrefabInstance: m_Modification: m_TransformParent: {fileID: 0} m_Modifications: + - target: {fileID: 6316647355651322741, guid: fdaa7451ab31b477b930076bbc888bab, + type: 3} + propertyPath: upgradePrefabs.Array.size + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 6316647355651322741, guid: fdaa7451ab31b477b930076bbc888bab, + type: 3} + propertyPath: upgradePrefabs.Array.data[0] + value: + objectReference: {fileID: 4585348266300200282, guid: 2ea1eb2b6b05340a4a99948a6eb818b2, + type: 3} - target: {fileID: 158905820630581597, guid: fdaa7451ab31b477b930076bbc888bab, type: 3} propertyPath: m_Name @@ -719,11 +730,6 @@ PrefabInstance: m_Modification: m_TransformParent: {fileID: 0} m_Modifications: - - target: {fileID: 7054975846833851214, guid: f064c0737a3e54f1fadc75fc09919760, - type: 3} - propertyPath: m_Name - value: Grid - objectReference: {fileID: 0} - target: {fileID: 7054975846833851208, guid: f064c0737a3e54f1fadc75fc09919760, type: 3} propertyPath: m_LocalPosition.x @@ -779,6 +785,11 @@ PrefabInstance: propertyPath: m_LocalEulerAnglesHint.z value: 0 objectReference: {fileID: 0} + - target: {fileID: 7054975846833851214, guid: f064c0737a3e54f1fadc75fc09919760, + type: 3} + propertyPath: m_Name + value: Grid + objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: f064c0737a3e54f1fadc75fc09919760, type: 3} --- !u!1001 &9082059503859367018 diff --git a/Assets/Scripts/src/Enemy/DumbEnemy.cs b/Assets/Scripts/src/Enemy/DumbEnemy.cs index 677ae44..5a41c97 100644 --- a/Assets/Scripts/src/Enemy/DumbEnemy.cs +++ b/Assets/Scripts/src/Enemy/DumbEnemy.cs @@ -11,7 +11,8 @@ public class DumbEnemy : EnemyBase protected new void FixedUpdate() { - if (transform.position.x == Mathf.Floor(transform.position.x) && transform.position.y == Mathf.Floor(transform.position.y)) + if (transform.position.x == Mathf.Floor(transform.position.x) && + transform.position.y == Mathf.Floor(transform.position.y)) { if (RandomChange()) { @@ -19,27 +20,26 @@ public class DumbEnemy : EnemyBase } else { - Rigidbody2d.MovePosition(Rigidbody2d.position + Direction * Speed * Time.deltaTime); + Rigidbody2d.MovePosition(Rigidbody2d.position + Speed * Time.deltaTime * Direction); } } else { - Rigidbody2d.MovePosition(Rigidbody2d.position + Direction * Speed * Time.deltaTime); + Rigidbody2d.MovePosition(Rigidbody2d.position + Speed * Time.deltaTime * Direction); } } private bool RandomChange() { - System.Random random = new System.Random(); - int randomNumber = random.Next(0, 100); + var random = new System.Random(); + var randomNumber = random.Next(0, 100); return randomNumber <= 25; } private void HandleChangeDirection() { - DebugHelper.LogInfo("Direction randomly changed"); Direction = ChooseRandomExceptCertainDirection(Direction); MoveToCenterOfTheCell(); - Rigidbody2d.MovePosition(Rigidbody2d.position + Direction * Speed * Time.deltaTime); + Rigidbody2d.MovePosition(Rigidbody2d.position + Speed * Time.deltaTime * Direction); } } diff --git a/Assets/Scripts/src/Helpers/ArrayExtensions.cs b/Assets/Scripts/src/Helpers/ArrayExtensions.cs index 4ee1c47..91cdeb9 100644 --- a/Assets/Scripts/src/Helpers/ArrayExtensions.cs +++ b/Assets/Scripts/src/Helpers/ArrayExtensions.cs @@ -8,7 +8,7 @@ namespace src.Helpers { public static T ChoseRandom(this T[] arr) { - var randomIndex = Mathf.FloorToInt(Random.Range(0, arr.Length)); + var randomIndex = Random.Range(0, arr.Length); return arr[randomIndex]; } @@ -17,7 +17,7 @@ namespace src.Helpers T value; do { - var randomIndex = Mathf.FloorToInt(Random.Range(0, arr.Length)); + var randomIndex = Random.Range(0, arr.Length); value = arr[randomIndex]; } while (exceptValue.Equals(value)); @@ -29,7 +29,7 @@ namespace src.Helpers T value; do { - var randomIndex = Mathf.FloorToInt(Random.Range(0, arr.Length)); + var randomIndex = Random.Range(0, arr.Length); value = arr[randomIndex]; } while (exceptValue.Contains(value)); diff --git a/Assets/Scripts/src/Interfaces/IUpgrade.cs b/Assets/Scripts/src/Interfaces/IUpgrade.cs new file mode 100644 index 0000000..f92f8f2 --- /dev/null +++ b/Assets/Scripts/src/Interfaces/IUpgrade.cs @@ -0,0 +1,10 @@ +using UnityEngine; + +namespace src.Interfaces +{ + public interface IUpgrade + { + void PerformUpgrade(); + void OnTriggerEnter2D(Collider2D other); + } +} \ No newline at end of file diff --git a/Assets/Scripts/src/Interfaces/IUpgrade.cs.meta b/Assets/Scripts/src/Interfaces/IUpgrade.cs.meta new file mode 100644 index 0000000..ae2ce95 --- /dev/null +++ b/Assets/Scripts/src/Interfaces/IUpgrade.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 527ddc9d1a334e739b995e82c8e96d10 +timeCreated: 1561210723 \ No newline at end of file diff --git a/Assets/Scripts/src/Managers/GameManager.cs b/Assets/Scripts/src/Managers/GameManager.cs index adc3f65..0ab7350 100644 --- a/Assets/Scripts/src/Managers/GameManager.cs +++ b/Assets/Scripts/src/Managers/GameManager.cs @@ -6,6 +6,7 @@ namespace src.Managers { public static GameManager Instance; private LevelManager _levelManager; + private UpgradeManager _upgradeManager; public void Awake() { @@ -22,10 +23,16 @@ namespace src.Managers DontDestroyOnLoad(gameObject); _levelManager = GetComponent(); + _upgradeManager = GetComponent(); InitGame(); } + public UpgradeManager GetUpgradeManager() + { + return _upgradeManager; + } + private void InitGame() { _levelManager.InitLevel(); diff --git a/Assets/Scripts/src/Managers/UpgradeManager.cs b/Assets/Scripts/src/Managers/UpgradeManager.cs new file mode 100644 index 0000000..9bbb09a --- /dev/null +++ b/Assets/Scripts/src/Managers/UpgradeManager.cs @@ -0,0 +1,16 @@ +using src.Base; +using src.Helpers; +using UnityEngine; + +namespace src.Managers +{ + public class UpgradeManager : GameplayComponent + { + public GameObject[] upgradePrefabs; + + public GameObject GetUpgradePrefab() + { + return upgradePrefabs.ChoseRandom(); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/src/Managers/UpgradeManager.cs.meta b/Assets/Scripts/src/Managers/UpgradeManager.cs.meta new file mode 100644 index 0000000..f4e5c60 --- /dev/null +++ b/Assets/Scripts/src/Managers/UpgradeManager.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: e64481d2e18e4b6ca4f22926a5061450 +timeCreated: 1561211937 \ No newline at end of file diff --git a/Assets/Scripts/src/Player/PlayerController.cs b/Assets/Scripts/src/Player/PlayerController.cs index 90a3a5a..f34270d 100644 --- a/Assets/Scripts/src/Player/PlayerController.cs +++ b/Assets/Scripts/src/Player/PlayerController.cs @@ -1,6 +1,7 @@ using src.Ammo; using src.Base; using src.Helpers; +using src.Interfaces; using UnityEngine; namespace src.Player diff --git a/Assets/Scripts/src/Upgrade.meta b/Assets/Scripts/src/Upgrade.meta new file mode 100644 index 0000000..f78cc06 --- /dev/null +++ b/Assets/Scripts/src/Upgrade.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ce5704d616e542aa9e6b98025b958d84 +timeCreated: 1561212967 \ No newline at end of file diff --git a/Assets/Scripts/src/Upgrade/TestUpgrade.cs b/Assets/Scripts/src/Upgrade/TestUpgrade.cs new file mode 100644 index 0000000..55971cc --- /dev/null +++ b/Assets/Scripts/src/Upgrade/TestUpgrade.cs @@ -0,0 +1,23 @@ +using src.Base; +using src.Helpers; +using src.Interfaces; +using UnityEngine; + +namespace src.Upgrade +{ + public class TestUpgrade : GameplayComponent, IUpgrade + { + public void PerformUpgrade() + { + DebugHelper.LogError("Test upgrade works."); + } + + public void OnTriggerEnter2D(Collider2D other) + { + if (!other.CompareTag("Player")) return; + + DebugHelper.LogWarning("TestUpgrade WORKS!"); + Destroy(gameObject); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/src/Upgrade/TestUpgrade.cs.meta b/Assets/Scripts/src/Upgrade/TestUpgrade.cs.meta new file mode 100644 index 0000000..adeba4b --- /dev/null +++ b/Assets/Scripts/src/Upgrade/TestUpgrade.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: daf1c278fd544eae9246c4abe6bf611e +timeCreated: 1561212975 \ No newline at end of file diff --git a/Assets/Scripts/src/Wall/DestructibleWall.cs b/Assets/Scripts/src/Wall/DestructibleWall.cs index cbfd909..240985a 100644 --- a/Assets/Scripts/src/Wall/DestructibleWall.cs +++ b/Assets/Scripts/src/Wall/DestructibleWall.cs @@ -1,7 +1,9 @@ +using System; using UnityEngine; using src.Base; using src.Helpers; +using src.Managers; namespace src.Wall { @@ -9,9 +11,15 @@ namespace src.Wall { private bool _spawnExit; private bool _spawnUpgrade; + private UpgradeManager _upgradeManager; public GameObject explosionPrefab; public GameObject exitDoorPrefab; + private void Start() + { + _upgradeManager = GameManager.Instance.GetUpgradeManager(); + } + public void SpawnsExit() { _spawnExit = true; @@ -38,7 +46,8 @@ namespace src.Wall else if (_spawnUpgrade) { DebugHelper.LogInfo($"Destructible spawned upgrade {transform.position}"); - // TODO: Get and instantiate upgrade from manager + var upgrade = _upgradeManager.GetUpgradePrefab(); + Instantiate(upgrade, currentPosition, Quaternion.identity); } }