Beispiel #1
0
        public void Load(bool didDownload = false)
        {
            try
            {
                if (!didDownload)
                {
                    JsonConvert.PopulateObject(File.ReadAllText(ConfigFilePath), this);
                }

                CreateEnumLookups();

                if (CoroutineRunner != null)
                {
                    CoroutineRunner.StartCoroutine(CGS.CardGameManager.Instance.LoadCards());
                }
                LoadSets();

                BackgroundImageSprite = UnityExtensionMethods.CreateSprite(BackgroundImageFilePath);
                CardBackImageSprite   = UnityExtensionMethods.CreateSprite(CardBackImageFilePath);

                IsLoaded = true;

                // Kick off auto-update in the background, even though it won't load until next time the app restarts
                if (AutoUpdate && !didDownload && CoroutineRunner != null)
                {
                    CoroutineRunner.StartCoroutine(Download());
                }
            }
            catch (Exception e)
            {
                Error   += e.Message + e.StackTrace + Environment.NewLine;
                IsLoaded = false;
            }
        }
Beispiel #2
0
        private void CreateBoard(GameBoard board)
        {
            var newBoard           = new GameObject(board.Id, typeof(RectTransform));
            var boardRectTransform = (RectTransform)newBoard.transform;

            boardRectTransform.SetParent(playArea.transform);
            boardRectTransform.anchorMin = Vector2.zero;
            boardRectTransform.anchorMax = Vector2.zero;
            boardRectTransform.offsetMin =
                new Vector2(board.OffsetMin.X, board.OffsetMin.Y) * CardGameManager.PixelsPerInch;
            boardRectTransform.offsetMax =
                new Vector2(board.OffsetMin.X, board.OffsetMin.Y) * CardGameManager.PixelsPerInch +
                boardRectTransform.offsetMin;

            string boardFilepath = CardGameManager.Current.GameBoardsDirectoryPath + "/" + board.Id + "." +
                                   CardGameManager.Current.GameBoardImageFileType;
            Sprite boardImageSprite = File.Exists(boardFilepath)
                ? UnityExtensionMethods.CreateSprite(boardFilepath)
                : null;

            if (boardImageSprite != null)
            {
                newBoard.AddComponent <Image>().sprite = boardImageSprite;
            }

            boardRectTransform.localScale = Vector3.one;
        }
Beispiel #3
0
        public void CreateBoard(GameBoard board)
        {
            if (board == null)
            {
                return;
            }

            GameObject    newBoard = new GameObject(board.Id, typeof(RectTransform));
            RectTransform rt       = (RectTransform)newBoard.transform;

            rt.SetParent(playAreaContent);
            rt.anchorMin = Vector2.zero;
            rt.anchorMax = Vector2.zero;
            rt.offsetMin = board.OffsetMin * CardGameManager.PixelsPerInch;
            rt.offsetMax = board.Size * CardGameManager.PixelsPerInch + rt.offsetMin;

            string boardFilepath = CardGameManager.Current.GameBoardsFilePath + "/" + board.Id + "." +
                                   CardGameManager.Current.GameBoardFileType;
            Sprite boardImageSprite = UnityExtensionMethods.CreateSprite(boardFilepath);

            if (boardImageSprite != null)
            {
                newBoard.AddComponent <Image>().sprite = boardImageSprite;
            }

            rt.localScale = Vector3.one;
        }
        public void Load(CardGameCoroutineDelegate updateCoroutine, CardGameCoroutineDelegate loadCardsCoroutine)
        {
            // We should have already read the *Game:Name*.json, but we need to be sure
            if (!HasReadProperties)
            {
                ReadProperties();
                if (!HasReadProperties)
                {
                    // ReadProperties() should have already populated the Error
                    HasLoaded = false;
                    return;
                }
            }

            // Don't waste time loading if we need to update first
            int daysSinceUpdate = 0;

            try { daysSinceUpdate = (int)DateTime.Today.Subtract(File.GetLastWriteTime(GameFilePath).Date).TotalDays; } catch { };
            if (AutoUpdate >= 0 && daysSinceUpdate >= AutoUpdate && CoroutineRunner != null)
            {
                CoroutineRunner.StartCoroutine(updateCoroutine(this));
                return;
            }

            // These enum lookups need to be initialized before we load cards and sets
            foreach (EnumDef enumDef in Enums)
            {
                enumDef.InitializeLookups();
            }

            // The main load action is to load cards and sets
            if (CoroutineRunner != null)
            {
                CoroutineRunner.StartCoroutine(loadCardsCoroutine(this));
            }
            LoadSets();

            // We also re-load the banner and cardback images now in case they've changed since we ReadProperties
            if (File.Exists(BannerImageFilePath))
            {
                BannerImageSprite = UnityExtensionMethods.CreateSprite(BannerImageFilePath);
            }
            if (File.Exists(CardBackImageFilePath))
            {
                CardBackImageSprite = UnityExtensionMethods.CreateSprite(CardBackImageFilePath);
            }

            // Only considered as loaded if none of the steps failed
            if (string.IsNullOrEmpty(Error))
            {
                HasLoaded = true;
            }
        }
        public void ReadProperties()
        {
            try
            {
                // We need to read the *Game:Name*.json file, but reading it can cause *Game:Name* to change, so account for that
                string gameFilePath      = GameFilePath;
                string gameDirectoryPath = GameDirectoryPath;
                ClearDefinitionLists();
                JsonConvert.PopulateObject(File.ReadAllText(GameFilePath), this);
                if (!gameFilePath.Equals(GameFilePath) && File.Exists(gameFilePath))
                {
                    string tempGameFilePath = gameDirectoryPath + "/" + UnityExtensionMethods.GetSafeFileName(Name) + ".json";
                    File.Move(gameFilePath, tempGameFilePath);
                }
                if (!gameDirectoryPath.Equals(GameDirectoryPath) && Directory.Exists(gameDirectoryPath))
                {
                    Directory.Move(gameDirectoryPath, GameDirectoryPath);
                }

                // We're being greedy about loading these now, since these could be shown before the game is selected
                if (File.Exists(BannerImageFilePath))
                {
                    BannerImageSprite = UnityExtensionMethods.CreateSprite(BannerImageFilePath);
                }
                if (File.Exists(CardBackImageFilePath))
                {
                    CardBackImageSprite = UnityExtensionMethods.CreateSprite(CardBackImageFilePath);
                }

                HasReadProperties = true;
            }
            catch (Exception e)
            {
                Error            += e.Message + e.StackTrace + Environment.NewLine;
                HasReadProperties = false;
            }
        }