Tính quả bóng cuối cùng
Bài học 18 trên 19 trong khóa học Trò chơi Unity: phá gạch của Coddy.
Khi có ba quả bóng đang chơi, mất bóng chỉ nên khiến bạn mất mạng khi đó là quả cuối cùng. Trước lúc đó, bóng sẽ bị xóa khỏi danh sách và bị hủy. Các con slime phân tách cũng hoạt động tương tự:
public void SlimeDied(Slime slime)
{
slimes.Remove(slime);
Destroy(slime.gameObject);
if (slimes.Count == 0) Debug.Log("Room cleared");
}Quả bóng ban đầu có thể bị mất trong khi các bản sao tiếp tục bay, nên quả bóng đầu tiên trong danh sách có thể là một bản sao. Code cần dùng đến «quả bóng», chẳng hạn như khi màn chơi được dọn sạch, sẽ dùng balls[0], không dùng trường Ball trong cảnh.
Thử thách
KhóMất thêm một quả bóng không làm mất mạng.
- Ở đầu
BallLost: nếuballs.Count > 1, hãy gọiballs.Remove(lost);, hủy đối tượng của quả bóng đó rồi trả về. - Khi hoàn thành một màn, hãy gọi
balls[0].ResetToPaddle()thay choball.ResetToPaddle().
Nhấn Chơi, tách bóng rồi để bóng rơi mất hai lần: số mạng vẫn giữ nguyên cho đến khi quả bóng cuối cùng rơi. Sau đó nhấn Kiểm tra.
Tự làm thử
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
public class GameManager : MonoBehaviour
{
public static GameManager Instance;
public Brick brickPrefab;
public Transform wall;
public string[] levels;
public float spacingX = 1.75f;
public float spacingY = 0.75f;
public float topY = 6f;
public Ball ball;
public int lives = 3;
public TMP_Text scoreText;
public TMP_Text livesText;
public GameObject gameOverPanel;
public GameObject[] powerUps;
public float dropChance = 0.15f;
int score;
int level;
int bricksLeft;
List<Ball> balls = new List<Ball>();
void Awake()
{
Instance = this;
}
void Start()
{
balls.Add(ball);
BuildLevel(0);
ShowHud();
}
void BuildLevel(int index)
{
level = index;
string[] rows = levels[index].Split('/');
for (int row = 0; row < rows.Length; row++)
{
string line = rows[row];
float left = -(line.Length - 1) * spacingX / 2f;
for (int col = 0; col < line.Length; col++)
{
char c = line[col];
if (c == '.') continue;
Vector3 pos = new Vector3(left + col * spacingX, topY - row * spacingY, 0f);
Brick brick = Instantiate(brickPrefab, pos, Quaternion.identity, wall);
brick.SetHits(c - '0');
bricksLeft++;
}
}
}
public void BrickBroken(Brick brick)
{
score += brick.points;
bricksLeft--;
ShowHud();
if (Random.value < dropChance)
{
GameObject prefab = powerUps[Random.Range(0, powerUps.Length)];
Instantiate(prefab, brick.transform.position, Quaternion.identity);
}
if (bricksLeft == 0)
{
ball.ResetToPaddle();
BuildLevel((level + 1) % levels.Length);
}
}
public void BallLost(Ball lost)
{
lives--;
ShowHud();
if (lives == 0)
{
gameOverPanel.SetActive(true);
lost.gameObject.SetActive(false);
}
else lost.ResetToPaddle();
}
public void SplitBall()
{
Ball source = balls[0];
Ball left = Instantiate(source, source.transform.position, Quaternion.identity);
left.Launch(new Vector3(-0.6f, 1f, 0f));
balls.Add(left);
Ball right = Instantiate(source, source.transform.position, Quaternion.identity);
right.Launch(new Vector3(0.6f, 1f, 0f));
balls.Add(right);
}
public void PlayAgain()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
void ShowHud()
{
scoreText.text = "Score: " + score;
livesText.text = "Lives: " + lives;
}
}
Tất cả bài học trong Trò chơi Unity: phá gạch
4Vật phẩm tăng sức mạnh
Vật phẩm rơi xuốngTự luyện tập: Playground Unity