int GetFreeSlotOrOldestExisting() { using (new Util.ProfileBlock("GetFreeSlotOrOldestExisting")) { int oldest = -1; System.DateTime oldestWriteTime = System.DateTime.Now; for (int i = 0; i < maxSlots; i++) { string bundleId = GetSlotBundleId(i); GameBundle bundle = bundleLibrary.GetBundle(bundleId); string voosPath = bundle.GetVoosPath(); if (!File.Exists(voosPath)) { return(i); } System.DateTime writeTime = System.IO.File.GetLastWriteTime(voosPath); if (oldest == -1 || writeTime < oldestWriteTime) { oldest = i; oldestWriteTime = writeTime; } } return(oldest); } }
public ActionResult HandleLoad() { try { // instantiate business service GameService service = new GameService(); // pass control to service and catch return value GameStorageModel businessLayerResponseModel = service.Load(Bundle.User); // deserialize game state and save to bundle Bundle = JsonConvert.DeserializeObject <GameBundle>(businessLayerResponseModel.GameState); // configure timer Bundle.StartTime = DateTime.Now - Bundle.Timer; // log successful load Logger.Info("Game loaded successfully!"); // return board view with bundle return(View("gameBoard", Bundle)); } catch (Exception e) { Logger.Error("Failed to load game: " + e.Message); return(View("exception")); } }
public void SetDependens(List <GameBundle> bundles) { for (int i = 0; i < bundles.Count; i++) { GameBundle bundle = bundles[i]; mDependens.Add(bundle.mName, bundle); } }
// determines if there are unvisted safe cells in the grid public bool hasUnvistedSafeCells(GameBundle bundle) { for (int i = 0; i < bundle.Board.Size; i++) { for (int j = 0; j < bundle.Board.Size; j++) { // any cell that is neither live or visited means the game goes on if (bundle.Board.Grid[i, j].Visited == false && bundle.Board.Grid[i, j].Live == false) { return(true); } } } // there are no unvisited safe cells the game is over return(false); }
// safety method to prevent crashes private bool isValid(int row, int col, GameBundle bundle) { bool validRow = false; bool validColumn = false; // determine if given row is valid if (row >= 0 && (row < bundle.Board.Size)) { validRow = true; } // determine if given column is valid if (col >= 0 && col < bundle.Board.Size) { validColumn = true; } // if both the given row and the given column are valid return true otherwise return false return(validRow && validColumn); }
/// <summary> /// 加载ab包 /// </summary> private GameBundle LoadAB(string resName) { string abName = mResConfig.GetCfg(resName).abName; GameBundle resBundle = null; if (mBundleDic.ContainsKey(abName)) { resBundle = mBundleDic[abName]; } else { //获取依赖包的相关信息 string[] strs = mManifest.GetAllDependencies(abName); List <GameBundle> refList = new List <GameBundle>(); for (int i = 0; i < strs.Length; i++) { if (!mBundleDic.ContainsKey(strs[i])) { AssetBundle ab = LoadABFile(strs[i]); var bundle = new GameBundle(strs[i], ab); refList.Add(bundle); mBundleDic.Add(strs[i], bundle); } else { var bundle = mBundleDic[strs[i]]; bundle.AddRef(); refList.Add(bundle); } } //加载主包来获取资源 if (!mBundleDic.ContainsKey(abName)) { AssetBundle ab = LoadABFile(abName); resBundle = new GameBundle(abName, ab); resBundle.SetDependens(refList); mBundleDic.Add(abName, resBundle); } } return(resBundle); }
// determines what happens when a cell is selected by the user // a return value of -1 indicates invalid arguments // a return value of 0 indicates the given cell is not a bomb and the game may continue // a return value of 1 indicates a bomb // a return value of 2 indicates the player has won public int Update(GameBundle bundle) { if (bundle.Board.gameOver == false) { if (isValid(bundle.Row, bundle.Column, bundle)) { // mark appropriate cells as visited floodFill(bundle.Row, bundle.Column, bundle); // the cell is a bomb if (bundle.Board.Grid[bundle.Row, bundle.Column].Live == true) { // game over bundle.Board.gameOver = true; // return update result return(1); } else { // the cell is not a bomb the game may continue if (hasUnvistedSafeCells(bundle)) { // return update result return(0); } // the player wins else { bundle.Board.gameOver = true; // return update result return(2); } } } } // an error has occured either invalid Grid coordinates or the game is already over return(-1); }
// lay the mines public GameBundle SetupLiveNeighbors(GameBundle bundle) { int rand = -1; // create random object Random random = new Random(); // randomely determine if each Cell is live for (int i = 0; i < bundle.Board.Size; i++) { for (int j = 0; j < bundle.Board.Size; j++) { // generate random number between 1 and 100 rand = random.Next(1, 100); // if the ranodm number is less than or equal to the difficulty the Cell is live if (rand <= bundle.Difficulty * 5) { bundle.Board.Grid[i, j].Live = true; } } } return(bundle); }
public void SaveFromShortcut() { if (HasCurrentBeenSaved()) { userMain.AddDebugMessage("Project saved!"); if (!IsOpen()) { string existingBundleId = GetActiveBundleId(); if (existingBundleId != null) { GameBundle bundle = gameBundleLibrary.GetBundle(existingBundleId); saveUI.nameInput.text = bundle.GetMetadata().name; saveUI.descriptionInput.text = bundle.GetMetadata().description; currentSaveImage = bundle.GetThumbnail(); } } SaveOverwrite(); } else { Open(); } }
public void SaveBeforeExit(System.Action quitCallback) { this.quitAfterSave = true; this.quitCallback = quitCallback; if (HasCurrentBeenSaved()) { if (!IsOpen()) { string existingBundleId = GetActiveBundleId(); if (existingBundleId != null) { GameBundle bundle = gameBundleLibrary.GetBundle(existingBundleId); saveUI.nameInput.text = bundle.GetMetadata().name; saveUI.descriptionInput.text = bundle.GetMetadata().description; currentSaveImage = bundle.GetThumbnail(); } } SaveOverwrite(); } else { Open(); } }
public ActionResult StartGame(string difficulty) { // use try/catch block to handle exceptions try { // the difficulty the player selected on the previous form int chosenDifficulty = Int32.Parse(difficulty); // instaniate game bundle. The bundle model includes all models and primitives related to the game. Bundle = new GameBundle(chosenDifficulty); Bundle.User = Cache.AccessCache().Get("activeAccount"); // instantiate game service // the game service is a Business Logic Layer class that enforces rules & logic GameService gameService = new GameService(); // deploy mines gameService.SetupLiveNeighbors(Bundle); // calculate neighbor counts for each cell gameService.CalculateLiveNeighbors(Bundle); // return the game board view return(View("gameBoard", Bundle)); } // handle exceptions catch (Exception e) { // log error Logger.Error("Failed to start game: " + e.Message); // return generic error page return(View("Exception")); } }
// recursive method used to mark cells as visited public void floodFill(int row, int col, GameBundle bundle) { // mark the current cell as visited bundle.Board.Grid[row, col].Visited = true; // unflag visited cells bundle.Board.Grid[row, col].isFlagged = false; // if there are no live neighbors call floodFill on all eight neighbors if (bundle.Board.Grid[row, col].liveNeighbors == 0) { if (isValid(row + 1, col, bundle)) { if (bundle.Board.Grid[row + 1, col].Visited == false) { floodFill(row + 1, col, bundle); } } if (isValid(row - 1, col, bundle)) { if (bundle.Board.Grid[row - 1, col].Visited == false) { floodFill(row - 1, col, bundle); } } if (isValid(row, col + 1, bundle)) { if (bundle.Board.Grid[row, col + 1].Visited == false) { floodFill(row, col + 1, bundle); } } if (isValid(row, col - 1, bundle)) { if (bundle.Board.Grid[row, col - 1].Visited == false) { floodFill(row, col - 1, bundle); } } if (isValid(row + 1, col + 1, bundle)) { if (bundle.Board.Grid[row + 1, col + 1].Visited == false) { floodFill(row + 1, col + 1, bundle); } } if (isValid(row + 1, col - 1, bundle)) { if (bundle.Board.Grid[row + 1, col - 1].Visited == false) { floodFill(row + 1, col - 1, bundle); } } if (isValid(row - 1, col + 1, bundle)) { if (bundle.Board.Grid[row - 1, col + 1].Visited == false) { floodFill(row - 1, col + 1, bundle); } } if (isValid(row - 1, col - 1, bundle)) { if (bundle.Board.Grid[row - 1, col - 1].Visited == false) { floodFill(row - 1, col - 1, bundle); } } } }
// determine how many of a Cell's neighbors are live public void CalculateLiveNeighbors(GameBundle bundle) { int liveNeighborCount; // iterate once for every Cell use i and j to keep track of which cell is being examined for (int i = 0; i < bundle.Board.Size; i++) { for (int j = 0; j < bundle.Board.Size; j++) { // reset liveNeighborCount to zero for each Cell liveNeighborCount = 0; // check if neighbor is a valid cell location if (isValid(bundle.Board.Grid[i, j].Row + 1, bundle.Board.Grid[i, j].Column + 1, bundle)) { // increment liveNeiborCount if the neighbor is live if (bundle.Board.Grid[bundle.Board.Grid[i, j].Row + 1, bundle.Board.Grid[i, j].Column + 1].Live == true) { liveNeighborCount++; } } // check if neighbor is a valid cell location if (isValid(bundle.Board.Grid[i, j].Row + 1, bundle.Board.Grid[i, j].Column, bundle)) { // increment liveNeiborCount if the neighbor is live if (bundle.Board.Grid[bundle.Board.Grid[i, j].Row + 1, bundle.Board.Grid[i, j].Column].Live == true) { liveNeighborCount++; } } // check if neighbor is a valid cell location if (isValid(bundle.Board.Grid[i, j].Row + 1, bundle.Board.Grid[i, j].Column - 1, bundle)) { // increment liveNeiborCount if the neighbor is live if (bundle.Board.Grid[bundle.Board.Grid[i, j].Row + 1, bundle.Board.Grid[i, j].Column - 1].Live == true) { liveNeighborCount++; } } // check if neighbor is a valid cell location if (isValid(bundle.Board.Grid[i, j].Row, bundle.Board.Grid[i, j].Column + 1, bundle)) { // increment liveNeiborCount if the neighbor is live if (bundle.Board.Grid[bundle.Board.Grid[i, j].Row, bundle.Board.Grid[i, j].Column + 1].Live == true) { liveNeighborCount++; } } // check if neighbor is a valid cell location if (isValid(bundle.Board.Grid[i, j].Row, bundle.Board.Grid[i, j].Column, bundle)) { // increment liveNeiborCount if the neighbor is live if (bundle.Board.Grid[bundle.Board.Grid[i, j].Row, bundle.Board.Grid[i, j].Column].Live == true) { liveNeighborCount++; } } // check if neighbor is a valid cell location if (isValid(bundle.Board.Grid[i, j].Row, bundle.Board.Grid[i, j].Column - 1, bundle)) { // increment liveNeiborCount if the neighbor is live if (bundle.Board.Grid[bundle.Board.Grid[i, j].Row, bundle.Board.Grid[i, j].Column - 1].Live == true) { liveNeighborCount++; } } // check if neighbor is a valid cell location if (isValid(bundle.Board.Grid[i, j].Row - 1, bundle.Board.Grid[i, j].Column + 1, bundle)) { // increment liveNeiborCount if the neighbor is live if (bundle.Board.Grid[bundle.Board.Grid[i, j].Row - 1, bundle.Board.Grid[i, j].Column + 1].Live == true) { liveNeighborCount++; } } // check if neighbor is a valid cell location if (isValid(bundle.Board.Grid[i, j].Row - 1, bundle.Board.Grid[i, j].Column, bundle)) { // increment liveNeiborCount if the neighbor is live if (bundle.Board.Grid[bundle.Board.Grid[i, j].Row - 1, bundle.Board.Grid[i, j].Column].Live == true) { liveNeighborCount++; } } // check if neighbor is a valid cell location if (isValid(bundle.Board.Grid[i, j].Row - 1, bundle.Board.Grid[i, j].Column - 1, bundle)) { // increment liveNeiborCount if the neighbor is live if (bundle.Board.Grid[bundle.Board.Grid[i, j].Row - 1, bundle.Board.Grid[i, j].Column - 1].Live == true) { liveNeighborCount++; } } bundle.Board.Grid[i, j].liveNeighbors = liveNeighborCount; } } }
public void Open() { if (IsOpen()) { return; } UpdateSaveButtonTextAndVisibility(); saveUI.feedbackTextPrimary.gameObject.SetActive(false); saveUI.feedbackTextSecondary.gameObject.SetActive(false); gameObject.SetActive(true); Update(); string existingBundleId = GetActiveBundleId(); if (existingBundleId != null) { GameBundle bundle = gameBundleLibrary.GetBundle(existingBundleId); saveUI.nameInput.text = bundle.GetMetadata().name; saveUI.descriptionInput.text = bundle.GetMetadata().description; currentSaveImage = bundle.GetThumbnail(); #if USE_STEAMWORKS WorkshopItemUpdate uploadItem = null; //see if the xml file is there if (File.Exists(Path.Combine(gameBundleLibrary.GetBundleDirectory(existingBundleId), SteamUtil.WorkshopItemInfoFileName))) { uploadItem = SteamWorkshopMain.Instance.GetItemUpdateFromFolder(gameBundleLibrary.GetBundleDirectory(existingBundleId)); } if (uploadItem != null) { string url = $"https://steamcommunity.com/sharedfiles/filedetails/?id={uploadItem.SteamNative.m_nPublishedFileId.ToString()}"; saveUI.feedbackTextSecondary.SetText($"Workshop URL (must be logged in):\n{url}"); saveUI.feedbackTextSecondaryButton.onClick.AddListener(() => Application.OpenURL(url)); saveUI.feedbackTextSecondary.gameObject.SetActive(true); fileOnSteamWorkshop = true; } else #endif { saveUI.feedbackTextSecondary.gameObject.SetActive(false); fileOnSteamWorkshop = false; } } else if (GameBuilderApplication.IsRecoveryMode) { saveUI.feedbackTextPrimary.gameObject.SetActive(true); saveUI.feedbackTextPrimary.text = "Recovering will create a new save, and you can continue building normally from it. Your old project will still exist in your game library in case you need to go back to it."; saveUI.feedbackTextPrimaryButton.onClick.RemoveAllListeners(); // Fill in these fields for convenience string autosaveBundleId = GameBuilderApplication.CurrentGameOptions.bundleIdToLoad; GameBundle bundle = gameBundleLibrary.GetBundle(autosaveBundleId); saveUI.nameInput.text = bundle.GetMetadata().name; saveUI.descriptionInput.text = bundle.GetMetadata().description; currentSaveImage = bundle.GetThumbnail(); } if (currentSaveImage != null) { saveUI.screenshotImage.gameObject.SetActive(true); saveUI.screenshotImage.sprite = Sprite.Create(currentSaveImage, new Rect(0, 0, currentSaveImage.width, currentSaveImage.height), new Vector2(.5f, .5f), 100); } }