PlayerManager

mail@pastecode.io avatar
unknown
plain_text
3 years ago
4.6 kB
4
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;

public class PlayerManager : NetworkBehaviour
{
    public GameObject Card1HighJab;
    public GameObject Card2MidJab;
    public GameObject Card3LowJab;
    public GameObject Card4HighStreaight;
    public GameObject Card5MidStraight;
    public GameObject Card6HighHook;
    public GameObject Card7MidHook;
    public GameObject Card8LowHook;
    public GameObject Card101BobAndWeave;
    public GameObject Card102Duck;
    public GameObject Card103HighGuard;
    public GameObject Card104MidGuard;
    public GameObject Card105SquareUp;
    public GameObject Card106LeanBack;
    public GameObject Card107PhillyShell;
    public GameObject Card108SideStep;
    public GameObject PlayerArea;
    public GameObject EnemyArea;
    public GameObject DropZone;

    List<GameObject> atkcards = new List<GameObject>();
    List<GameObject> defcards = new List<GameObject>();

    public override void OnStartClient()
    {
        base.OnStartClient();

        PlayerArea = GameObject.Find("PlayerArea");
        EnemyArea = GameObject.Find("EnemyArea");
        DropZone = GameObject.Find("DropZone");
    }

    [Server]
    public override void OnStartServer()
    {
        base.OnStartServer();

        atkcards.Add(Card1HighJab);
        atkcards.Add(Card2MidJab);
        atkcards.Add(Card3LowJab);
        atkcards.Add(Card4HighStreaight);
        atkcards.Add(Card5MidStraight);
        atkcards.Add(Card6HighHook);
        atkcards.Add(Card7MidHook);
        atkcards.Add(Card8LowHook);
        defcards.Add(Card101BobAndWeave);
        defcards.Add(Card102Duck);
        defcards.Add(Card103HighGuard);
        defcards.Add(Card104MidGuard);
        defcards.Add(Card105SquareUp);
        defcards.Add(Card106LeanBack);
        defcards.Add(Card107PhillyShell);
        defcards.Add(Card108SideStep);
        Debug.Log(atkcards);
        Debug.Log(defcards);
    }

    [Command]
    public void CmdDealCards()
    {
        for (int i = 0; i < 2; i++)
        {
            GameObject atkcard = Instantiate(atkcards[Random.Range(0, atkcards.Count)], new Vector2(0, 0), Quaternion.identity);
            NetworkServer.Spawn(atkcard, connectionToClient);
            RpcShowatkCard(atkcard, "Dealt");
        }

        for (int i = 0; i < 2; i++)
        {
            GameObject defcard = Instantiate(defcards[Random.Range(0, defcards.Count)], new Vector2(0, 0), Quaternion.identity);
            NetworkServer.Spawn(defcard, connectionToClient);
            RpcShowdefCard(defcard, "Dealt");
        }
    }

    public void PlaydefCard(GameObject defcard)
    {
        CmdPlaydefCard(defcard);
    }

    [Command]
    void CmdPlaydefCard(GameObject defcard)
    {
        RpcShowdefCard(defcard, "Played");
    }

    [ClientRpc]
    void RpcShowdefCard(GameObject defcard, string type)
    {
        if (type == "Dealt")
        {
            if (hasAuthority)
            {
                defcard.transform.SetParent(PlayerArea.transform, false);
            }
            else
            {
                defcard.transform.SetParent(EnemyArea.transform, false);
                defcard.GetComponent<CardFlipper>().defFlip();
            }
        }
        else if (type == "Played")
        {
            defcard.transform.SetParent(DropZone.transform, false);
            if (!hasAuthority)
            {
                defcard.GetComponent<CardFlipper>().defFlip();
            }
            
        }

    }

    public void PlayatkCard(GameObject atkCard)
    {
        CmdPlayatkCard(atkCard);
    }

    [Command]
    void CmdPlayatkCard(GameObject atkCard)
    {
        RpcShowatkCard(atkCard, "Played");
    }

    [ClientRpc]
    void RpcShowatkCard(GameObject atkCard, string type)
    {
        if (type == "Dealt")
        {
            if (hasAuthority)
            {
                atkCard.transform.SetParent(PlayerArea.transform, false);
            }
            else
            {
                atkCard.transform.SetParent(EnemyArea.transform, false);
                atkCard.GetComponent<CardFlipper>().atkFlip();
            }
        }
        else if (type == "Played")
        {
            atkCard.transform.SetParent(DropZone.transform, false);
            if (!hasAuthority)
            {
                atkCard.GetComponent<CardFlipper>().atkFlip();
            }

        }

    }
}