Menu
CoddyTech

Récapitulatif : gestionnaire de manche

Fait partie de la section Créer de vrais jeux du Journey Unity de Coddy. Leçon 16 sur 60.

Ce chapitre a doté le jeu d’un seul gestionnaire : un singleton auquel les autres accèdent sans champ, et des états entre lesquels il bascule. Le gestionnaire de manche met les deux à contribution.

challenge icon

Défi

Facile

Termine la collecte des pièces ; SetState est écrit.

  1. Ajoute public static RoundManager Instance;, défini dans Awake().
  2. Dans Update(), utilise state pour choisir l’action : Ready attend Space ; Playing décompte timeLeft, puis passe au Ready suivant, ou à Finished après la dernière manche.
  3. public bool TakeCoin() : uniquement pendant Playing, compte la pièce et affiche Coins: 2, puis renvoie true. Coin.cs détruit une pièce comptabilisée touchée par le Player.

Appuie sur « Jouer » pour les trois manches. Puis appuie sur « Vérifier ».

Essayez vous-même

using UnityEngine;
using TMPro;

public enum RoundState { Ready, Playing, Finished }

public class RoundManager : MonoBehaviour
{
    // le singleton : une Instance statique, définie dans 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()
    {
        // selon state : Ready attend Space ; Playing décompte le temps restant
    }

    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() : une pièce ne compte que pendant Playing
}
quiz iconTestez-vous

Cette leçon comprend un petit quiz. Commencez la leçon pour y répondre et suivre votre progression.

Toutes les leçons de Créer de vrais jeux

Entraînez-vous par vous-même : Playground Unity