/* * Function triggers writing of current game status into XML file */ public void SaveGame(int saveFileIndex) { if (saveFileIndex < 0 || saveFileIndex > 9) { return; } Savegame savegame = new Savegame(_environment, _assets.variables, currentScriptIndex, _scripts[currentScriptIndex].currentLine - 1); string saveGameLocation = ".\\saves\\save_" + saveFileIndex.ToString("D2"); XmlSerializer serializer = new XmlSerializer(savegame.GetType()); using (StreamWriter writer = new StreamWriter(saveGameLocation)) { serializer.Serialize(writer, savegame); writer.Close(); } ReportSavedGame(); }
/* * Returns the save game with the latest date */ private Savegame FindLastSave() { Savegame mostRecentSave = new Savegame(); DateTime mostRecentDate = DateTime.MinValue; bool saveFound = false; for (int i = 0; i < 10; i++) { Savegame save = Savegame.DeserializeSaveGame(i); if (save != null && save.currentTime > mostRecentDate) { mostRecentSave = save; saveFound = true; } } return(saveFound ? mostRecentSave : null); }
/* * Triggers loading or deleting of chosen slot on click */ private void LoadSlotOnClick(object sender, RoutedEventArgs e) { if (Settings.deleteGamesOnLoadScreen) { Button btn = (Button)sender; if (Int32.TryParse(btn.Name.Substring(btn.Name.Length - 1, 1), out int saveIndex)) { DeleteSavedGame(saveIndex); Settings.deletedSaveSlot = saveIndex; ClearViewport(true); ShowLoadGameScreen(); } } else { Button btn = (Button)sender; if (Int32.TryParse(btn.Name.Substring(btn.Name.Length - 1, 1), out int saveIndex)) { LoadGame(Savegame.DeserializeSaveGame(saveIndex)); } } }
private void LoadGame(Savegame save) { // Set required gameplay elements currentScriptIndex = save.currentScriptIndex; _scripts[currentScriptIndex].currentLine = save.currentScriptLine; _scripts[currentScriptIndex].currentPositionInLine = 0; _environment = save.currentEnvironment; _environment.currentLanguage = UILanguage.createLanguage(Settings.language); _assets.variables = save.currentVariables; // Start game StopSound(false); StopMusic(false); ClearViewport(false); NewGame(false); // Load environment from save LoadSurroundingsFromEnvironment(_environment); Settings.inGame = true; Settings.allowProgress = true; Settings.afterLoad = true; }
/* * Show save game screen */ private void SaveButtonOnClick(object sender, RoutedEventArgs e) { Settings.inGame = false; AllowResize(false); Border darkOverlay = new Border() { Name = "darkOverlay", Width = Settings.windowWidth, Height = Settings.windowHeight, Background = new SolidColorBrush(Color.FromArgb(160, 0, 0, 0)) }; ViewportContainer.Children.Add(darkOverlay); Panel.SetZIndex(darkOverlay, 6); _environment.temporaryUIlayer1.Add(darkOverlay.Name); TextBlock questionTextBlock = new TextBlock { Name = "questionTextBlock", Text = _environment.currentLanguage.UI_saveMenu_instructions, TextAlignment = TextAlignment.Center, FontSize = 21, FontWeight = FontWeights.Bold, Foreground = new SolidColorBrush(Colors.White), Margin = new Thickness(10, 10, 10, 20) }; _environment.temporaryUIlayer1.Add(questionTextBlock.Name); // Create grid for save slots Grid slotGrid = new Grid { Name = "slotGrid", Margin = new Thickness(10, 10, 10, 10), }; slotGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); slotGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); slotGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); slotGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }); slotGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }); slotGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }); _environment.temporaryUIlayer1.Add(slotGrid.Name); // Create save slots for (var i = 0; i < 3; i++) { for (var j = 1; j <= 3; j++) { // Gets the save file for this slot Savegame save = Savegame.DeserializeSaveGame(3 * i + j); // Creates UI content Button slot = new Button() { Name = "save_0" + (3 * i + j), Margin = new Thickness(10, 10, 10, 10), Background = new SolidColorBrush(Colors.White), Width = 300, Height = 100 }; slotGrid.Children.Add(slot); Grid.SetColumn(slot, j - 1); Grid.SetRow(slot, i); _environment.temporaryUIlayer1.Add(slot.Name); if (save == null) { TextBlock emptySlotText = new TextBlock { Name = "emptySlotText", Text = _environment.currentLanguage.UI_emptySlot, FontSize = 21, FontWeight = FontWeights.Bold }; slot.Content = emptySlotText; slot.Click += SlotOnClick; _environment.temporaryUIlayer1.Add(emptySlotText.Name); } else { Background selBackground = _assets.backgrounds.Find(bg => bg.name == save.currentEnvironment.currentBackgroundName); Image slotImage = new Image { Source = new BitmapImage(selBackground.imageUri) }; TextBlock slotText = new TextBlock { Name = "slotText", Text = save.currentEnvironment.fullText, FontSize = 14, FontWeight = FontWeights.DemiBold, MaxHeight = 70, TextWrapping = TextWrapping.Wrap, TextTrimming = TextTrimming.CharacterEllipsis }; _environment.temporaryUIlayer1.Add(slotText.Name); TextBlock slotDate = new TextBlock { Name = "slotDate", Text = save.currentTime.ToString(CultureInfo.CurrentCulture), FontSize = 14, TextTrimming = TextTrimming.CharacterEllipsis, Margin = new Thickness(0, 10, 0, 0) }; _environment.temporaryUIlayer1.Add(slotDate.Name); Grid innerRightSlotGrid = new Grid { Name = "innerRightSlotGrid", Margin = new Thickness(5, 0, 0, 0) }; innerRightSlotGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(5, GridUnitType.Star) }); innerRightSlotGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(2, GridUnitType.Star) }); innerRightSlotGrid.Children.Add(slotText); innerRightSlotGrid.Children.Add(slotDate); Grid.SetRow(slotText, 0); Grid.SetRow(slotDate, 1); Grid innerSlotGrid = new Grid { Name = "innerSlotGrid", }; innerSlotGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); innerSlotGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); innerSlotGrid.Children.Add(slotImage); innerSlotGrid.Children.Add(innerRightSlotGrid); Grid.SetColumn(slotImage, 0); Grid.SetColumn(innerRightSlotGrid, 1); slot.Content = innerSlotGrid; slot.Click += SlotOverwriteOnClick; _environment.temporaryUIlayer1.Add(innerSlotGrid.Name); } } } TextBlock cancelTextBlock = new TextBlock { Name = "cancelTextBlock", Text = _environment.currentLanguage.UI_cancel, FontSize = 21, FontWeight = FontWeights.Bold }; _environment.temporaryUIlayer1.Add(cancelTextBlock.Name); Button cancelButton = new Button { Name = "cancelButton", IsCancel = true, Content = cancelTextBlock, Margin = new Thickness(5, 5, 5, 5), Padding = new Thickness(5, 5, 5, 5), Width = 240 }; cancelButton.Click += CancelButtonOnClick; _environment.temporaryUIlayer1.Add(cancelButton.Name); StackPanel verticalStackPanel = new StackPanel { Name = "verticalStackPanel", Orientation = Orientation.Vertical, }; verticalStackPanel.Children.Add(questionTextBlock); verticalStackPanel.Children.Add(slotGrid); verticalStackPanel.Children.Add(cancelButton); _environment.temporaryUIlayer1.Add(verticalStackPanel.Name); Border saveMenuBorder = new Border { Name = "saveMenuBorder", BorderThickness = new Thickness(5, 5, 5, 5), BorderBrush = new SolidColorBrush(Colors.White), Background = new SolidColorBrush(Color.FromArgb(148, 0, 0, 0)), Child = verticalStackPanel, MinWidth = 960, MinHeight = 540 }; ViewportContainer.Children.Add(saveMenuBorder); Canvas.SetLeft(saveMenuBorder, 160); Canvas.SetTop(saveMenuBorder, 90); Panel.SetZIndex(saveMenuBorder, 7); _environment.temporaryUIlayer1.Add(saveMenuBorder.Name); }
/* * Show screen containing all saved games found, allow user to load a game */ private void ShowLoadGameScreen() { AllowResize(false); TextBlock questionTextBlock = new TextBlock { Name = "questionTextBlock", Text = Settings.deleteGamesOnLoadScreen ? _environment.currentLanguage.UI_loadMenu_deleteInstructions : _environment.currentLanguage.UI_loadMenu_loadInstructions, TextAlignment = TextAlignment.Center, FontSize = 21, FontWeight = FontWeights.Bold, Foreground = new SolidColorBrush(Colors.White), Margin = new Thickness(10, 10, 10, 20) }; // Create grid for save slots Grid slotGrid = new Grid { Name = "slotGrid", Margin = new Thickness(10, 10, 10, 10), }; slotGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); slotGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); slotGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); slotGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }); slotGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }); slotGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }); // Create save slots for (var i = 0; i < 3; i++) { for (var j = 1; j <= 3; j++) { // Gets the save file for this slot Savegame save = Savegame.DeserializeSaveGame(3 * i + j); // Creates UI content Button slot = new Button() { Name = "save_0" + (3 * i + j), Margin = new Thickness(10, 10, 10, 10), Width = 300, Height = 100 }; slotGrid.Children.Add(slot); Grid.SetColumn(slot, j - 1); Grid.SetRow(slot, i); if (save == null) { TextBlock emptySlotText = new TextBlock { Name = "emptySlotText", Text = _environment.currentLanguage.UI_emptySlot, FontSize = 21, FontWeight = FontWeights.Bold }; slot.Content = emptySlotText; } else if (Settings.deletedSaveSlot == (3 * i + j)) { TextBlock emptySlotText = new TextBlock { Name = "emptySlotText", Text = _environment.currentLanguage.UI_emptySlot, FontSize = 21, FontWeight = FontWeights.Bold }; slot.Content = emptySlotText; Settings.deletedSaveSlot = -1; } else { slot.Click += LoadSlotOnClick; Background selBackground = _assets.backgrounds.Find(bg => bg.name == save.currentEnvironment.currentBackgroundName); Image slotImage = new Image { Source = new BitmapImage(selBackground.imageUri) }; TextBlock slotText = new TextBlock { Name = "slotText", Text = save.currentEnvironment.fullText, TextWrapping = TextWrapping.Wrap, TextTrimming = TextTrimming.CharacterEllipsis, MaxHeight = 70, FontSize = 14, FontWeight = FontWeights.DemiBold }; TextBlock slotDate = new TextBlock { Name = "slotDate", Text = save.currentTime.ToString(CultureInfo.CurrentCulture), TextTrimming = TextTrimming.CharacterEllipsis, FontSize = 12, Margin = new Thickness(0, 10, 0, 0) }; Grid innerRightSlotGrid = new Grid { Name = "innerRightSlotGrid", Margin = new Thickness(5, 0, 0, 0) }; innerRightSlotGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(5, GridUnitType.Star) }); innerRightSlotGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(2, GridUnitType.Star) }); innerRightSlotGrid.Children.Add(slotText); innerRightSlotGrid.Children.Add(slotDate); Grid.SetRow(slotText, 0); Grid.SetRow(slotDate, 1); Grid innerSlotGrid = new Grid { Name = "innerSlotGrid", }; innerSlotGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); innerSlotGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); innerSlotGrid.Children.Add(innerRightSlotGrid); innerSlotGrid.Children.Add(slotImage); Grid.SetColumn(slotImage, 0); Grid.SetColumn(innerRightSlotGrid, 1); slot.Content = innerSlotGrid; } } } TextBlock changeModeTextBlock = new TextBlock { Name = "changeModeTextBlock", FontSize = 21, FontWeight = FontWeights.Bold, Text = Settings.deleteGamesOnLoadScreen ? _environment.currentLanguage.UI_loadMenu_load : _environment.currentLanguage.UI_loadMenu_delete }; Button changeModeButton = new Button { Name = "cancelButton", IsCancel = true, Content = changeModeTextBlock, Margin = new Thickness(5, 5, 5, 5), Padding = new Thickness(5, 5, 5, 5), Width = 240 }; changeModeButton.Click += (sender, args) => { Settings.deleteGamesOnLoadScreen = !Settings.deleteGamesOnLoadScreen; ClearViewport(true); ShowLoadGameScreen(); }; TextBlock cancelTextBlock = new TextBlock { Name = "cancelTextBlock", Text = _environment.currentLanguage.UI_cancel, FontSize = 21, FontWeight = FontWeights.Bold }; Button cancelButton = new Button { Name = "cancelButton", IsCancel = true, Content = cancelTextBlock, Margin = new Thickness(5, 5, 5, 5), Padding = new Thickness(5, 5, 5, 5), Width = 240 }; cancelButton.Click += (sender, args) => { MainMenu(false, false); }; Grid buttonGrid = new Grid { Name = "buttonGrid", Margin = new Thickness(10, 10, 10, 10), }; buttonGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); buttonGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); buttonGrid.Children.Add(changeModeButton); buttonGrid.Children.Add(cancelButton); Grid.SetColumn(changeModeButton, 0); Grid.SetColumn(cancelButton, 1); StackPanel verticalStackPanel = new StackPanel { Name = "verticalStackPanel", Orientation = Orientation.Vertical, }; verticalStackPanel.Children.Add(questionTextBlock); verticalStackPanel.Children.Add(slotGrid); verticalStackPanel.Children.Add(buttonGrid); Border saveMenuBorder = new Border { Name = "saveMenuBorder", BorderThickness = new Thickness(5, 5, 5, 5), BorderBrush = new SolidColorBrush(Colors.White), Background = new SolidColorBrush(Color.FromArgb(148, 0, 0, 0)), Child = verticalStackPanel, MinWidth = 960, MinHeight = 540 }; ViewportContainer.Children.Add(saveMenuBorder); Canvas.SetLeft(saveMenuBorder, 160); Canvas.SetTop(saveMenuBorder, 90); Panel.SetZIndex(saveMenuBorder, 7); // Background blur BlurEffect blur = new BlurEffect { Radius = 0 }; DoubleAnimation blurAnimation = new DoubleAnimation(0.0, 5.0, new Duration(TimeSpan.FromMilliseconds(300))); _backgroundImage.Effect = blur; blur.BeginAnimation(BlurEffect.RadiusProperty, blurAnimation); }
/* * Loads main menu elements */ private void MainMenu(bool restartBackground, bool restartMusic) { ClearViewport(true); TextBlock gameNameTextBlock = new TextBlock { Name = "gameNameTextBlock", Text = Settings.gameName, FontSize = 48, FontWeight = FontWeights.Bold, Foreground = new SolidColorBrush(Colors.AliceBlue) }; ViewportContainer.Children.Add(gameNameTextBlock); Canvas.SetLeft(gameNameTextBlock, 100); Canvas.SetTop(gameNameTextBlock, 50); Panel.SetZIndex(gameNameTextBlock, 2); Savegame lastSave = FindLastSave(); if (lastSave != null) { Button continueButton = new Button { Name = "continueButton", Width = 320, Height = 48, Background = new SolidColorBrush(Color.FromArgb(192, 16, 16, 16)) }; TextBlock continueTextBlock = new TextBlock { Name = "continueTextBlock", Text = _environment.currentLanguage.UI_mainMenu_continue, FontSize = 20, FontWeight = FontWeights.Bold, Foreground = new SolidColorBrush(Colors.White) }; continueButton.Content = continueTextBlock; continueButton.Click += (sender, args) => { LoadGame(lastSave); }; ViewportContainer.Children.Add(continueButton); Canvas.SetLeft(continueButton, 100); Canvas.SetTop(continueButton, 200); Panel.SetZIndex(continueButton, 2); } Button newGameButton = new Button { Name = "newGameButton", Width = 320, Height = 48, Background = new SolidColorBrush(Color.FromArgb(192, 16, 16, 16)) }; TextBlock newGameTextBlock = new TextBlock { Name = "newGameTextBlock", Text = _environment.currentLanguage.UI_mainMenu_newGame, FontSize = 20, FontWeight = FontWeights.Bold, Foreground = new SolidColorBrush(Colors.White) }; newGameButton.Content = newGameTextBlock; newGameButton.AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(NewGameButtonClick)); ViewportContainer.Children.Add(newGameButton); Canvas.SetLeft(newGameButton, 100); Canvas.SetTop(newGameButton, 280); Panel.SetZIndex(newGameButton, 2); Button loadGameButton = new Button { Name = "newGameButton", Width = 320, Height = 48, Background = new SolidColorBrush(Color.FromArgb(192, 16, 16, 16)) }; TextBlock loadGameTextBlock = new TextBlock { Name = "loadGameTextBlock", Text = _environment.currentLanguage.UI_mainMenu_loadGame, FontSize = 20, FontWeight = FontWeights.Bold, Foreground = new SolidColorBrush(Colors.White) }; loadGameButton.Content = loadGameTextBlock; loadGameButton.AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(LoadGameButtonClick)); ViewportContainer.Children.Add(loadGameButton); Canvas.SetLeft(loadGameButton, 100); Canvas.SetTop(loadGameButton, 360); Panel.SetZIndex(loadGameButton, 2); Button optionsButton = new Button { Name = "optionsButton", Width = 320, Height = 48, Background = new SolidColorBrush(Color.FromArgb(192, 16, 16, 16)) }; TextBlock optionsTextBlock = new TextBlock { Name = "optionsTextBlock", Text = _environment.currentLanguage.UI_mainMenu_options, FontSize = 20, FontWeight = FontWeights.Bold, Foreground = new SolidColorBrush(Colors.White) }; optionsButton.Content = optionsTextBlock; optionsButton.AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(OptionsButtonClick)); ViewportContainer.Children.Add(optionsButton); Canvas.SetLeft(optionsButton, 100); Canvas.SetTop(optionsButton, 440); Panel.SetZIndex(optionsButton, 2); Button exitButton = new Button { Name = "exitButton", Width = 320, Height = 48, Background = new SolidColorBrush(Color.FromArgb(192, 16, 16, 16)) }; TextBlock exitTextBlock = new TextBlock { Name = "exitTextBlock", Text = _environment.currentLanguage.UI_mainMenu_exit, FontSize = 20, FontWeight = FontWeights.Bold, Foreground = new SolidColorBrush(Colors.White) }; exitButton.Content = exitTextBlock; exitButton.AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(ExitButtonClick)); ViewportContainer.Children.Add(exitButton); Canvas.SetLeft(exitButton, 100); Canvas.SetTop(exitButton, 520); Panel.SetZIndex(exitButton, 2); Button languageButton = new Button { Name = "languageButton", Width = 48, Height = 48, Background = new SolidColorBrush(Color.FromArgb(192, 16, 16, 16)) }; TextBlock languageTextBlock = new TextBlock { Name = "languageTextBlock", Text = _environment.currentLanguage.initial, FontSize = 20, FontWeight = FontWeights.ExtraBold, Foreground = new SolidColorBrush(Colors.White) }; languageButton.Content = languageTextBlock; languageButton.Click += (sender, args) => { int langIndex = Settings.LanguageInitialList.FindIndex(s => s == Settings.language); Settings.language = langIndex == Settings.LanguageInitialList.Count - 1 ? Settings.LanguageInitialList[0] : Settings.LanguageInitialList[langIndex + 1]; _environment.currentLanguage = UILanguage.createLanguage(Settings.language); MainMenu(false, false); SaveSettings(); }; ViewportContainer.Children.Add(languageButton); Canvas.SetLeft(languageButton, Settings.windowWidth - 98); Canvas.SetTop(languageButton, Settings.windowHeight - 98); Panel.SetZIndex(languageButton, 2); AllowResize(true); // Menu background image and music if (restartBackground) { ShowBackground("menu_background", 10); } if (restartMusic) { _backgroundMusicPlayer.Stop(); _soundEffectPlayer.Stop(); PlaySound("menu_music", 1, true); } }