Skip to content

Commit 92d560f

Browse files
committed
added a helper "ghost" to preview where Shape will land.
1 parent f4417f9 commit 92d560f

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

Assets/Scenes/Game.unity

+44
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,50 @@ CanvasRenderer:
323323
m_PrefabAsset: {fileID: 0}
324324
m_GameObject: {fileID: 67693222}
325325
m_CullTransparentMesh: 0
326+
--- !u!1 &98321550
327+
GameObject:
328+
m_ObjectHideFlags: 0
329+
m_CorrespondingSourceObject: {fileID: 0}
330+
m_PrefabInstance: {fileID: 0}
331+
m_PrefabAsset: {fileID: 0}
332+
serializedVersion: 6
333+
m_Component:
334+
- component: {fileID: 98321552}
335+
- component: {fileID: 98321551}
336+
m_Layer: 0
337+
m_Name: Ghost
338+
m_TagString: Untagged
339+
m_Icon: {fileID: 0}
340+
m_NavMeshLayer: 0
341+
m_StaticEditorFlags: 0
342+
m_IsActive: 1
343+
--- !u!114 &98321551
344+
MonoBehaviour:
345+
m_ObjectHideFlags: 0
346+
m_CorrespondingSourceObject: {fileID: 0}
347+
m_PrefabInstance: {fileID: 0}
348+
m_PrefabAsset: {fileID: 0}
349+
m_GameObject: {fileID: 98321550}
350+
m_Enabled: 1
351+
m_EditorHideFlags: 0
352+
m_Script: {fileID: 11500000, guid: a92b9357675d39645b0adb97732bc403, type: 3}
353+
m_Name:
354+
m_EditorClassIdentifier:
355+
m_color: {r: 1, g: 1, b: 1, a: 0.2}
356+
--- !u!4 &98321552
357+
Transform:
358+
m_ObjectHideFlags: 0
359+
m_CorrespondingSourceObject: {fileID: 0}
360+
m_PrefabInstance: {fileID: 0}
361+
m_PrefabAsset: {fileID: 0}
362+
m_GameObject: {fileID: 98321550}
363+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
364+
m_LocalPosition: {x: 0, y: 0, z: 0}
365+
m_LocalScale: {x: 1, y: 1, z: 1}
366+
m_Children: []
367+
m_Father: {fileID: 0}
368+
m_RootOrder: 9
369+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
326370
--- !u!1 &174820326
327371
GameObject:
328372
m_ObjectHideFlags: 0

Assets/Scripts/Core/Ghost.cs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class Ghost : MonoBehaviour
6+
{
7+
public Color m_color = new Color(1f, 1f, 1f, 0.2f);
8+
9+
private Shape m_ghostShape = null;
10+
private bool m_hitBottom = false;
11+
12+
public void DrawGhost(Shape originalShape, Board gameBoard)
13+
{
14+
if (!m_ghostShape)
15+
{
16+
m_ghostShape = Instantiate(originalShape, originalShape.transform.position, originalShape.transform.rotation);
17+
m_ghostShape.gameObject.name = "GhostShape";
18+
19+
SpriteRenderer[] allRenderers = m_ghostShape.GetComponentsInChildren<SpriteRenderer>();
20+
21+
foreach (var r in allRenderers)
22+
{
23+
r.color = m_color;
24+
}
25+
}
26+
else
27+
{
28+
m_ghostShape.transform.position = originalShape.transform.position;
29+
m_ghostShape.transform.rotation = originalShape.transform.rotation;
30+
}
31+
32+
m_hitBottom = false;
33+
34+
while (!m_hitBottom)
35+
{
36+
m_ghostShape.MoveDown();
37+
38+
if (!gameBoard.IsValidPosition(m_ghostShape))
39+
{
40+
m_ghostShape.MoveUp();
41+
m_hitBottom = true;
42+
}
43+
}
44+
}
45+
46+
public void Reset()
47+
{
48+
Destroy(m_ghostShape.gameObject);
49+
}
50+
}

Assets/Scripts/Core/Ghost.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/Managers/GameManager.cs

+21
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class GameManager : MonoBehaviour
1111
private Shape m_aciveShape;
1212
private SoundManager m_soundManager;
1313
private ScoreManager m_scoreManager;
14+
private Ghost m_ghost;
1415

1516
private float m_timeToDrop;
1617
public float m_dropInterval = 0.9f;
@@ -56,6 +57,7 @@ void Start()
5657
m_spawner = GameObject.FindObjectOfType<Spawner>();
5758
m_soundManager = FindObjectOfType<SoundManager>();
5859
m_scoreManager = FindObjectOfType<ScoreManager>();
60+
m_ghost = FindObjectOfType<Ghost>();
5961

6062
if (!m_gameBoard)
6163
{
@@ -71,6 +73,11 @@ void Start()
7173
{
7274
Debug.LogWarning("WARNING: There is no score manager defined!");
7375
}
76+
77+
if (!m_ghost)
78+
{
79+
Debug.LogWarning("WARNING: There is no ghost defined!");
80+
}
7481

7582
if (!m_spawner)
7683
{
@@ -110,6 +117,14 @@ void Update()
110117
PlayerInput();
111118
}
112119

120+
private void LateUpdate()
121+
{
122+
if (m_ghost)
123+
{
124+
m_ghost.DrawGhost(m_aciveShape, m_gameBoard);
125+
}
126+
}
127+
113128
private void PlayerInput()
114129
{
115130
if ((Input.GetButton("MoveRight") && (Time.time > m_timeToNextKeyLeftRight)) || Input.GetButtonDown("MoveRight"))
@@ -203,6 +218,12 @@ private void LandShape()
203218

204219
m_aciveShape.MoveUp();
205220
m_gameBoard.StoreShapeInGrid(m_aciveShape);
221+
222+
if (m_ghost)
223+
{
224+
m_ghost.Reset();
225+
}
226+
206227
m_aciveShape = m_spawner.SpawnShape();
207228

208229
m_gameBoard.ClearAllRows();

0 commit comments

Comments
 (0)