Menu
CoddyTech

חזרה: מנהל הסיבוב

חלק מהיחידה בניית משחקים אמיתיים במסלול ה-Unity של Coddy. שיעור 16 מתוך 60.

הפרק הזה העניק למשחק מנהל אחד: מופע יחיד שאחרים מגיעים אליו בלי שדה, ומצבים שהוא עובר ביניהם. מנהל הסיבוב מפעיל את שניהם.

challenge icon

אתגר

קל

השלם את איסוף המטבעות; SetState כבר כתובה.

  1. הוסף public static RoundManager Instance;, והגדר אותו ב־Awake().
  2. ב־Update(), בצע החלפה לפי state: מצב Ready ממתין ללחיצה על Space; מצב Playing סופר לאחור את timeLeft, ואז עובר למצב Ready הבא, או למצב Finished אחרי הסיבוב האחרון.
  3. public bool TakeCoin(): רק בזמן Playing, ספור את המטבע, הצג Coins: 2, והחזר true. הקובץ Coin.cs משמיד מטבע שנאסף כשה־Player נוגע בו.

לחץ על "שחק" בכל שלושת הסיבובים. לאחר מכן לחץ על "בדיקה".

נסו בעצמכם

using UnityEngine;
using TMPro;

public enum RoundState { Ready, Playing, Finished }

public class RoundManager : MonoBehaviour
{
    // הסינגלטון: Instance סטטי, שמוגדר ב-Awake()
    public GameObject readyPanel;
    public GameObject playPanel;
    public GameObject finishedPanel;
    public TMP_Text roundText;
    public TMP_Text coinsText;
    public TMP_Text finalText;
    public int rounds = 3;
    public float roundTime = 4f;
    private RoundState state;
    private int round = 1;
    private int coins;
    private float timeLeft;

    void Start()
    {
        SetState(RoundState.Ready);
    }

    void Update()
    {
        // בחירה לפי state: ‏Ready ממתין ללחיצה על Space; ‏Playing סופר את הזמן לאחור
    }

    void SetState(RoundState next)
    {
        state = next;
        readyPanel.SetActive(next == RoundState.Ready);
        playPanel.SetActive(next == RoundState.Playing);
        finishedPanel.SetActive(next == RoundState.Finished);
        if (next == RoundState.Ready) roundText.text = "Round " + round;
        if (next == RoundState.Playing) timeLeft = roundTime;
        if (next == RoundState.Finished) finalText.text = "Coins: " + coins;
    }

    // TakeCoin(): המטבע נספר רק בזמן Playing
}
quiz iconבחנו את עצמכם

השיעור הזה כולל חידון קצר. התחילו את השיעור כדי לענות עליו ולעקוב אחרי ההתקדמות.

כל השיעורים ביחידה בניית משחקים אמיתיים

תרגלו בעצמכם: Unity אונליין