예제 #1
0
    /// <summary>
    /// [COROUTINE] loads premade decks on web builds.  PC builds do it in Awake().
    /// </summary>
    /// <returns></returns>
    private IEnumerator loadPremadeDecksWeb()
    {
        //form the web request
        string filePath = Application.streamingAssetsPath + '/' + premadeDeckPath;
        //while (filePath.StartsWith("/")) filePath = filePath.Substring(1); //remove any leading /'s
        WWW request = new WWW(filePath);

        //wait for the request to load
        yield return(request);

        //show error if there was one
        if (request.error != null)
        {
            Debug.LogError("Error loading premade decks:\n" + request.error);
            yield break;
        }

        //or, if we were successful, create a new stream and fill it with the contents of the web request:
        using (MemoryStream premadeDecksStream = new MemoryStream())    //create the stream
        {
            StreamWriter writer = new StreamWriter(premadeDecksStream); //used to write to it
            writer.Write(request.text);                                 //write contents of the request
            writer.Flush();                                             //make sure it gets processed
            premadeDecksStream.Position = 0;                            //send the stream back to the start

            //now we can finally load the decks
            premadeDecks = DeckCollection.Load(premadeDecksStream, filePath);
        }
    }
예제 #2
0
 public static void PrintDeckCollectionKeys(DeckCollection collection)
 {
     foreach (string key in collection.MyDeck.Keys)
     {
         Console.WriteLine(key);
     }
 }
예제 #3
0
    /// <summary>
    /// returns a new DeckCollection loaded from the given file
    /// </summary>
    public static DeckCollection Load(Stream stream, string filePath)
    {
        XmlSerializer  serializer = new XmlSerializer(typeof(DeckCollection));
        DeckCollection result     = serializer.Deserialize(stream) as DeckCollection;

        result.curPath = filePath;
        return(result);
    }
예제 #4
0
파일: IDealCards.cs 프로젝트: Bawaw/Whist
 public void DealCards(Player[] players)
 {
     DeckCollection cardCollection = new DeckCollection();
     cardCollection.initialise();
     cardCollection.shuffle();
     int nCards = cardCollection.Count / players.Length;
     foreach (var player in players)
         player.hand.AddCards(cardCollection.Draw(nCards));
 }
예제 #5
0
    }                                 //total number of cards in the deck

    // Use this for initialization
    private void Awake()
    {
        instance = this;

        //premade decks
        if (Application.platform == RuntimePlatform.WebGLPlayer)
        {
            StartCoroutine(loadPremadeDecksWeb()); //web player has to use a coroutine for this because it waits for a web request
        }
        else
        {
            //PC build, however, can load them right here
            string filePath = Path.Combine(Application.streamingAssetsPath, premadeDeckPath);
            using (FileStream stream = new FileStream(filePath, FileMode.Open))
                premadeDecks = DeckCollection.Load(stream, filePath);
        }

        //load player decks if the file exists, or create an empty collection if not.
        //This file is local even on web builds, so it doesn't need special handling
        try
        {
            string filePath = Path.Combine(Application.persistentDataPath, "playerDecks.xml");
            using (FileStream stream = new FileStream(filePath, FileMode.Open))
                playerDecks = DeckCollection.Load(stream, filePath);
        }
        catch (Exception e)
        {
            Debug.Log("no deck save file found. (" + e.Message + ")");
            playerDecks = new DeckCollection();
        }

        currentDeck    = new List <PlayerCard>();
        deckSize       = 0;
        curDeckCharges = 0;
        maxDeckCharges = 0;
    }
예제 #6
0
    /// <summary>
    /// reloads the deck lists, empties out the deck, and then reloads it
    /// </summary>
    private void Reset()
    {
        //premade decks
        if (Application.platform == RuntimePlatform.WebGLPlayer)
        {
            StartCoroutine(loadPremadeDecksWeb()); //web player has to use a coroutine for this because it waits for a web request
        }
        else
        {
            //PC build, however, can load them right here
            string filePath = Path.Combine(Application.streamingAssetsPath, premadeDeckPath);
            using (FileStream stream = new FileStream(filePath, FileMode.Open))
                premadeDecks = DeckCollection.Load(stream, filePath);
        }

        //load player decks if the file exists, or create an empty collection if not
        try
        {
            string filePath = Path.Combine(Application.persistentDataPath, "playerDecks.xml");
            using (FileStream stream = new FileStream(filePath, FileMode.Open))
                playerDecks = DeckCollection.Load(stream, filePath);
        }
        catch (Exception e)
        {
            Debug.Log("no deck save file found. (" + e.Message + ")");
            playerDecks = new DeckCollection();
        }

        //clear out the current deck
        currentDeck.Clear();

        if (LevelManagerScript.instance.data.usingLevelDeck)
        {
            //if we were using the level deck, reload that
            LevelManagerScript.instance.loadLevelDeck(); //this delegates to level manager so that it can retain control over the level data
        }
        else
        {
            //otherwise, the player picked a deck.  Search the deck lists for a deck of the same name as what we're using and load that.

            XMLDeck target = null; //deck to reload

            if (premadeDecks.getNames().Contains(currentDeckName))
            {
                target = premadeDecks.getDeckByName(currentDeckName);
            }

            if (playerDecks.getNames().Contains(currentDeckName))
            {
                if (target != null)
                {
                    Debug.LogError("both deck lists contain a deck of the same name!  Unsure which to reload.");
                }
                else
                {
                    target = playerDecks.getDeckByName(currentDeckName);
                }
            }

            if (target == null)
            {
                Debug.LogError("could not find the deck to reload it!");
            }

            SetDeck(target);
            Shuffle();
        }
    }