From 9f9bf9a3f5fe2fd79214bbd9fc8654110f5317d4 Mon Sep 17 00:00:00 2001 From: Jesus Castro Date: Wed, 17 Dec 2025 00:28:22 +0800 Subject: [PATCH] Assign GemSprite to BombPrefab --- Assets/GameVariables.asset | 7 +- Assets/Prefabs/Gems/Bomb Variant.prefab | 101 +++++++++++++++++++- Assets/Scripts/Models/Gem.cs | 5 +- Assets/Scripts/Services/GameBoardService.cs | 14 +-- Assets/Scripts/Structs/GemTypeValues.cs | 1 + Assets/Scripts/Views/GemView.cs | 34 +++---- 6 files changed, 136 insertions(+), 26 deletions(-) diff --git a/Assets/GameVariables.asset b/Assets/GameVariables.asset index aef3568..325dc25 100644 --- a/Assets/GameVariables.asset +++ b/Assets/GameVariables.asset @@ -20,41 +20,46 @@ MonoBehaviour: type: 3} explosionPrefab: {fileID: 8904178830182364799, guid: d794be08823edd34da1790efd1739074, type: 3} + gemSprite: {fileID: 21300000, guid: 6a4836de24a0c354f986c800d32b7c86, type: 3} scoreValue: 10 - type: 1 gemPrefab: {fileID: 4490600519223577409, guid: 784323496d719684cb6201b200b95864, type: 3} explosionPrefab: {fileID: 4157327684024108653, guid: e5bba4196a7d43441bf62bd125171449, type: 3} + gemSprite: {fileID: 21300000, guid: d184668e3ca69674d8a20a99d14eb1d2, type: 3} scoreValue: 10 - type: 2 gemPrefab: {fileID: 6213027626580313688, guid: 473855e3d0d3c8143836b678c9a1b8b5, type: 3} explosionPrefab: {fileID: 5902279534240010874, guid: 072620c89288cc14e9872d103c2b2fbe, type: 3} + gemSprite: {fileID: 21300000, guid: 15913fd90bd479a45a568c3067a75c5d, type: 3} scoreValue: 10 - type: 3 gemPrefab: {fileID: 1845825807271331471, guid: 91ba2370328500d4db689dad894b1602, type: 3} explosionPrefab: {fileID: 4897855320600288343, guid: 7fcedfb0407bb5d438de37da2d61df30, type: 3} + gemSprite: {fileID: 21300000, guid: 6bb7c00a68de1634cbee8574d8499804, type: 3} scoreValue: 10 - type: 4 gemPrefab: {fileID: 745607475630438949, guid: 93bd623174244c047af9ce43cc254c32, type: 3} explosionPrefab: {fileID: 1865769399618337668, guid: 986dd822e2e6d4b45b2a054259f1241b, type: 3} + gemSprite: {fileID: 21300000, guid: fa67839330996764683a123cb35a1ea8, type: 3} scoreValue: 10 - type: 5 gemPrefab: {fileID: 5652386976359944012, guid: aa0291e650b382941875040db9e8a232, type: 3} explosionPrefab: {fileID: 8968486364681163996, guid: 05c754e3d4f9fd349ac1def58d17670f, type: 3} + gemSprite: {fileID: 21300000, guid: d1c82e015b38d4448bc1ba5e3eff90fa, type: 3} scoreValue: 10 width: 7 height: 7 bombDelay: 2 - bombSelfDelay: 1 bombRadius: 2 dropHeight: 2 gemSpeed: 0.00625 diff --git a/Assets/Prefabs/Gems/Bomb Variant.prefab b/Assets/Prefabs/Gems/Bomb Variant.prefab index da9cc14..880c360 100644 --- a/Assets/Prefabs/Gems/Bomb Variant.prefab +++ b/Assets/Prefabs/Gems/Bomb Variant.prefab @@ -1,5 +1,89 @@ %YAML 1.1 %TAG !u! tag:unity3d.com,2011: +--- !u!1 &1833905315907890653 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 6673945018952926847} + - component: {fileID: 8819328447377349610} + m_Layer: 0 + m_Name: Square + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &6673945018952926847 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1833905315907890653} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: -0.3, y: -0.4, z: 0} + m_LocalScale: {x: 0.4, y: 0.4, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 1210587634127288451} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!212 &8819328447377349610 +SpriteRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1833905315907890653} + m_Enabled: 1 + m_CastShadows: 0 + m_ReceiveShadows: 0 + m_DynamicOccludee: 1 + m_StaticShadowCaster: 0 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RayTracingMode: 0 + m_RayTraceProcedural: 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: -1529445301 + m_SortingLayer: 1 + m_SortingOrder: 1 + m_Sprite: {fileID: 21300000, guid: 6a4836de24a0c354f986c800d32b7c86, 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, y: 1} + m_AdaptiveModeThreshold: 0.5 + m_SpriteTileMode: 0 + m_WasSpriteAssigned: 1 + m_MaskInteraction: 0 + m_SpriteSortPoint: 0 --- !u!1001 &8839428272799928612 PrefabInstance: m_ObjectHideFlags: 0 @@ -29,6 +113,11 @@ PrefabInstance: propertyPath: m_SpriteTilingProperty.oldSize.y value: 1 objectReference: {fileID: 0} + - target: {fileID: 3808538059049426536, guid: 724e93e48c6cc0b4ab3d44e5ea34f2ec, + type: 3} + propertyPath: childSpriteRenderer + value: + objectReference: {fileID: 8819328447377349610} - target: {fileID: 7667143225876670874, guid: 724e93e48c6cc0b4ab3d44e5ea34f2ec, type: 3} propertyPath: m_Name @@ -129,6 +218,16 @@ PrefabInstance: m_RemovedComponents: - {fileID: -7932498854989450506, guid: 724e93e48c6cc0b4ab3d44e5ea34f2ec, type: 3} m_RemovedGameObjects: [] - m_AddedGameObjects: [] + m_AddedGameObjects: + - targetCorrespondingSourceObject: {fileID: 7667143225876670887, guid: 724e93e48c6cc0b4ab3d44e5ea34f2ec, + type: 3} + insertIndex: -1 + addedObject: {fileID: 6673945018952926847} m_AddedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 724e93e48c6cc0b4ab3d44e5ea34f2ec, type: 3} +--- !u!4 &1210587634127288451 stripped +Transform: + m_CorrespondingSourceObject: {fileID: 7667143225876670887, guid: 724e93e48c6cc0b4ab3d44e5ea34f2ec, + type: 3} + m_PrefabInstance: {fileID: 8839428272799928612} + m_PrefabAsset: {fileID: 0} diff --git a/Assets/Scripts/Models/Gem.cs b/Assets/Scripts/Models/Gem.cs index cbcefe8..f118c86 100644 --- a/Assets/Scripts/Models/Gem.cs +++ b/Assets/Scripts/Models/Gem.cs @@ -1,4 +1,5 @@ using Enums; +using Structs; using UnityEngine; namespace Services { @@ -14,10 +15,10 @@ namespace Services { private GemType colorType; public GemType MatchColor => this.type == GemType.Bomb ? this.colorType : this.type; - public Gem(GemType type, Vector2Int position, int scoreValue, GemType? colorType = null) { + public Gem(GemType type, Vector2Int position, GemTypeValues gemValue, GemType? colorType = null) { this.type = type; this.position = position; - this.scoreValue = scoreValue; + this.scoreValue = gemValue.scoreValue; this.colorType = colorType ?? type; } diff --git a/Assets/Scripts/Services/GameBoardService.cs b/Assets/Scripts/Services/GameBoardService.cs index a2c9575..2372b06 100644 --- a/Assets/Scripts/Services/GameBoardService.cs +++ b/Assets/Scripts/Services/GameBoardService.cs @@ -83,11 +83,12 @@ namespace Services { GemView gemView = this.objectPool.Get(gemType, position, this.gameVariables.dropHeight); gemView.name = "Gem - " + position.x + ", " + position.y + ' ' + gemType; + GemTypeValues gemValue = GemUtils.GetGemValues(gemType, this.gameVariables.gemsPrefabs); + // If we randomly spawned a bomb, give it a random color group (so it can match by color). - int scoreValue = GemUtils.GetGemValues(gemType, this.gameVariables.gemsPrefabs).scoreValue; - Gem gem = new Gem(gemType, position, scoreValue); + Gem gem = new Gem(gemType, position, gemValue); - gemView.Bind(gem); + gemView.Bind(gem, gemValue); this.gemPresenters.Add(new GemPresenter(gem, gemView)); SetGem(new Vector2Int(position.x, position.y), gem); @@ -99,10 +100,11 @@ namespace Services { GemView gemView = this.objectPool.Get(GemType.Bomb, position, 0); gemView.name = "Bomb - " + position.x + ", " + position.y + ' ' + GemType.Bomb; + + GemTypeValues gemValue = GemUtils.GetGemValues(color, this.gameVariables.gemsPrefabs); - int scoreValue = GemUtils.GetGemValues(color, this.gameVariables.gemsPrefabs).scoreValue; - Gem bombGem = new Gem(GemType.Bomb, position, scoreValue, color); - gemView.Bind(bombGem, isBomb: true); + Gem bombGem = new Gem(GemType.Bomb, position, gemValue, color); + gemView.Bind(bombGem, gemValue, isBomb: true); this.gemPresenters.Add(new GemPresenter(bombGem, gemView)); SetGem(position, bombGem); diff --git a/Assets/Scripts/Structs/GemTypeValues.cs b/Assets/Scripts/Structs/GemTypeValues.cs index 9b54b04..01bff47 100644 --- a/Assets/Scripts/Structs/GemTypeValues.cs +++ b/Assets/Scripts/Structs/GemTypeValues.cs @@ -9,6 +9,7 @@ namespace Structs { public GemType type; public GemView gemPrefab; public GameObject explosionPrefab; + public Sprite gemSprite; public int scoreValue; } } \ No newline at end of file diff --git a/Assets/Scripts/Views/GemView.cs b/Assets/Scripts/Views/GemView.cs index 625d158..61a71bc 100644 --- a/Assets/Scripts/Views/GemView.cs +++ b/Assets/Scripts/Views/GemView.cs @@ -2,45 +2,47 @@ using System.Threading; using Cysharp.Threading.Tasks; using Enums; using Services; +using Structs; using UnityEngine; using Utils; namespace Views { public class GemView : MonoBehaviour { + [SerializeField] + private SpriteRenderer childSpriteRenderer; + private Gem gem; public Gem Gem => this.gem; - private SpriteRenderer spriteRenderer; - private bool isFalling; private const float SPAWN_SCALE_FROM = 0f; private const float SPAWN_SCALE_DURATION = 0.12f; private CancellationTokenSource spawnScaleCts; - public void Bind(Gem gem, bool isBomb = false) { + public void Bind(Gem gem, GemTypeValues gemvalue, bool isBomb = false) { this.gem = gem; this.gameObject.SetActive(true); - SetupGem(isBomb); + SetupGem(isBomb, gemvalue.gemSprite); PlaySpawnScale(); } - private void SetupGem(bool isBomb) { - this.spriteRenderer ??= GetComponent(); + private void SetupGem(bool isBomb, Sprite gemSprite = null) { + if (!isBomb) + return; - if (isBomb) { - this.spriteRenderer.color = this.gem.MatchColor switch { - GemType.Blue => Color.blue, - GemType.Green => Color.green, - GemType.Red => Color.red, - GemType.Yellow => Color.yellow, - GemType.Purple => Color.magenta, - _ => this.spriteRenderer.color - }; + if (this.childSpriteRenderer is null) + return; + + if (gemSprite != null) { + this.childSpriteRenderer.enabled = true; + this.childSpriteRenderer.sprite = gemSprite; } else { - this.spriteRenderer.color = Color.white; + this.childSpriteRenderer.enabled = false; + this.childSpriteRenderer.color = Color.white; } + } public void Unbind() {