/// <summary> /// Creates a keyboard manager /// </summary> /// <param name="game">Game manager</param> public KeyboardManager(Game game) { // Gets all the keys from the enumeration and how many there are of them Key[] keys = (Key[])Enum.GetValues(typeof(Key)); int keysLength = keys.Length; // Creates a collection of keyboard key managers to manage every key // NOTE: it's keyLength - 1 because it doesn't allow you to manage the NULL key, so we can skip it _Keys = new KeyboardKey[keysLength - 1]; Key key; // Iterates through all the keys for (int i = 1; i < keysLength; i++) { // And sets up every keyboard key manager key = keys[i]; _Keys[i - 1] = new KeyboardKey(game, key, Keyboard.IsKeyDown(key)); } }
/// <summary> /// Creates a keyboard key state manager /// </summary> /// <param name="game">Game manager</param> /// <param name="key">Key from the keyboard</param> /// <param name="isDown">Is the button being pressed</param> public KeyboardKey(Game game, Key key, bool isDown) : base(game, isDown) { Key = key; }
/// <summary> /// Handles the drawing part of the scene /// </summary> /// <param name="game">Game manager</param> public void Draw(Game game) { // Game is a generic class, so make sure to convert it to ConsoleGame ConsoleGame consoleGame = (ConsoleGame)game; // ConsoleGame has a screen to draw whereas Game doesn't ConsoleScreen consoleScreen = consoleGame.Screen; // Saves the animation value for later use float animationValue = _Animation.Value; // Clears the whole screen consoleScreen.Clear(); // Checks what step we are to draw the right thing switch (_Step) { // This step is the one that the ASCII text art goes from right to left case 0: // It draws the art on the screen // The X offset is the animation value because only that axis is the one that's going to move // The Y offset (3) is constant // This animation value in this case goes from 32 to -360 consoleScreen.DrawText((int)Math.Round(animationValue), 3, _TitleArt); break; // This step happens when the text is fading in case 1: // This step happens when the text is fading out case 3: // The fade animation is actually a little trick ConsoleColor titleColor; // The animation value in this part goes from 0 to 1 or 1 to 0 // So we check the value to use an appropriate color if (animationValue < 0.25f) // If the value is too low, we set the text color to black titleColor = ConsoleColor.Black; // If it's reaching the middle of the animation else if (animationValue < 0.5f) // Sets it to dark gray titleColor = ConsoleColor.DarkGray; // If it's almost done else if (animationValue < 0.75f) // Sets it to gray titleColor = ConsoleColor.Gray; // When it's really almost there else // Just set it to white titleColor = ConsoleColor.White; // The standard draw text function uses black background and white foreground by default // So if the fourth argument is given, you can make something different happen consoleScreen.DrawText(7, 4, "Tamagottagettemall\n by Bruno Tamer", (x, y, i, block, a, b, c, chr) => { // On this case, we use the color that we 'calculated' before return new ConsoleBlock(ConsoleColor.Black, titleColor, chr); }); break; // If it's on the "PRESS START" part default: // Draws the fully opaque title consoleScreen.DrawText(7, 4, "Tamagottagettemall\n by Bruno Tamer"); // The animation value in this case means wether the press start is visible or not // If it is if (animationValue == 1.0f) // Then just draw it consoleScreen.DrawText(11, 12, "PRESS START"); break; } // When we're done drawing everything we needed, updates the screen consoleScreen.Show(); }
/// <summary> /// Handles the logical part of the scene /// </summary> /// <param name="game">Game manager</param> public void Update(Game game) { // Gets if any key was 'just pressed' KeyboardKey key = game.KeyboardManager.AnyTriggered; // If no key was 'just pressed' if (key == null) // Just updates the animation flow _Animation.Update(game.DeltaMilliseconds); // If Esc was pressed else if (key.Key == Key.Escape) // Closes the game Environment.Exit(0); // If it's not showing "PRESS START" and you pressed some key else if (_Step != 2) // Skips the current animation by forcing it to progress by its duration _Animation.Update(_Animation.Time); // If it's showing "PRESS START" and you pressed Enter else if (key.Key == Key.Enter) { // Goes to the step where it fades out the title and move on to the game ++_Step; // Changes the current animation to the title fade out animation // This animation goes from 1 (totally opaque) to 0 (totally transparent) // It takes 500 milliseconds and starts slow, goes through fast and arrives fast _Animation.Reset(1.0f, 0.0f, 500, Smoothness.Start, () => { // When it's completed, move on to the naming scene game.SceneManager.NextScene = new SceneName(); }); } }
/// <summary> /// Disposes of all scene elements /// </summary> /// <param name="game">Game manager</param> public void Terminate(Game game) { }
/// <summary> /// Starts the scene elements /// </summary> /// <param name="game">Game manager</param> public void Start(Game game) { // Sets the game frame rate to 60 game.DesiredFrameRate = 60; // Changes the title of the game Console.Title = "Tamagottagettemall"; // Loads the ASCII art from a file _TitleArt = Utils.StringReadFile("Title.txt"); // Sets the current step of the scene to 0 _Step = 0; // This first animation handles the ASCII title animation that goes from right to left // This animation will represent the X-coordinate of the art position // It goes from 32 (right limit) and goes all the way to -360 (the text is 360 characters wide, so -360 would be the left limit) // The animation will take 8000 milliseconds and it will start slow, go through fast and arrives slow _Animation = new SimpleAnimation(32, -360, 8000, Smoothness.StartArrival, () => { // When it's done, goes to the next step of the scene ++_Step; // Another animation happens now // This animation represents the fade in of the small title from the title screen // It goes from 0 (totally transparent) to 1 (totally opaque) // It takes 500 milliseconds and starts fast, goes through fast and arrives slow _Animation.Reset(0.0f, 1.0f, 500, Smoothness.Arrival, () => { // When it's done, goes to the next step of the scene ++_Step; // We will set up the "PRESS START" animation now // As it's gonna flash, we have to make a show animation and a hide animation Action hideAction = null; Action showAction = null; // The show animation will make the "PRESS START" visible for 500 milliseconds // This animation goes from 1 to 1 because the text is meant to be totally opaque this whole time // No need for smoothness, because the values are not varying showAction = () => { // And when the animation is done, calls the hide animation _Animation.Reset(1.0f, 1.0f, 500, Smoothness.None, hideAction); }; // The hide animation will make the "PRESS START" invisible for 1000 milliseconds // This animation goes from 0 to 0 because the text is meant to be totally transparent this whole time // No need for smoothness, because the values are not varying hideAction = () => { // And when the animation is done, calls the show animation // You may notice by now that the animation calls another after it's done - that's how it loops _Animation.Reset(0.0f, 0.0f, 1000, Smoothness.None, showAction); }; // First it starts with the show animation showAction(); }); }); }
/// <summary> /// Updates all the keyboard key state managers /// </summary> /// <param name="game">Game manager</param> public void Update(Game game) { // Iterates through all the key managers foreach (KeyboardKey keyboardKey in _Keys) // And update their states keyboardKey.Update( game, Keyboard.IsKeyDown(keyboardKey.Key) ); }
/// <summary> /// Handles the drawing part of the scene /// </summary> /// <param name="game">Game manager</param> public void Draw(Game game) { // Game is a generic class, so make sure to convert it to ConsoleGame ConsoleGame consoleGame = (ConsoleGame)game; // ConsoleGame has a screen to draw whereas Game doesn't ConsoleScreen consoleScreen = consoleGame.Screen; // Saves the animation value for later use float animationValue = _Animation.Value; // Clears the whole screen consoleScreen.Clear(); // Checks at what scene step it is switch (_Step) { // This is the part where the current day is displayed case 0: // Builds the day string // It's GameGlobals.Days + 1 because that variable starts at 0 string dayText = string.Format("Day {0}", GameGlobals.Days + 1); ConsoleColor dayTextColor; // Gets the right color for the fade animation if (animationValue < 0.25f) dayTextColor = ConsoleColor.Black; else if (animationValue < 0.5f) dayTextColor = ConsoleColor.DarkGray; else if (animationValue < 0.75f) dayTextColor = ConsoleColor.Gray; else dayTextColor = ConsoleColor.White; // Centers and draws the text on the screen consoleScreen.DrawText((32 - dayText.Length) / 2, 8, dayText, (x, y, i, block, a, b, c, chr) => { return new ConsoleBlock(ConsoleColor.Black, dayTextColor, chr); }); break; // This is the part where you select an action case 1: // Draws the pet art on the screen consoleScreen.DrawText(1, 1, _PetArt); // Draws "Please select an action" and the amount of actions you have left consoleScreen.DrawText(5, 10, string.Format("Please select an action\nYou have {0} actions left", _ActionsLeft)); // Draws the action box consoleScreen.DrawText(5, 12, _ActionsBox); // Draws the current selected action // Animation value in this case points to the string index of the selected action consoleScreen.DrawText(8, 13, Utils.StringRoulette(_ActionsString, 16, (int)Math.Round(animationValue * 10))); break; // This is the part where you see the pet feedback case 2: // This is the part where it's moving to the report scene case 3: // This is the part where it's moving to the gameover scene case 4: ConsoleColor textColor; Func<int, int, int, ConsoleBlock, int, int, int, char, ConsoleBlock> func = null; // If it is on the the feedback step if (_Step == 2) // Then just uses white for the texts textColor = ConsoleColor.White; else { // If it's on any other step, it means that it's fading out // Then we have to get the appropriate color for the texts if (animationValue < 0.25f) textColor = ConsoleColor.Black; else if (animationValue < 0.5f) textColor = ConsoleColor.DarkGray; else if (animationValue < 0.75f) textColor = ConsoleColor.Gray; else textColor = ConsoleColor.White; // We have to prepare a function to color the texts to later user func = (x, y, i, block, a, b, c, chr) => { return new ConsoleBlock(ConsoleColor.Black, textColor, chr); }; } // Draws the pet art on the screen consoleScreen.DrawText(1, 1, _PetArt, func); // Gets the pet name and status string petName = GameGlobals.Pet.Name; string petStatus = GameGlobals.Pet.Status; // And their length int petNameLength = petName.Length; int petStatusLength = petStatus.Length; // And adjust them on the screen in order to center both if (petNameLength > petStatusLength) consoleScreen.DrawText( (32 - petNameLength) / 2, 9, petName + "\n" + Utils.StringPadBoth(petStatus, petNameLength), func ); else consoleScreen.DrawText( (32 - petStatusLength) / 2, 9, Utils.StringPadBoth(petName, petStatusLength) + "\n" + petStatus, func ); // If it's on the feedback part, tells the player that they can proceed to select another action if (_Step == 2) consoleScreen.DrawText(10, 14, "PRESS ANY KEY"); break; } // When we're done drawing everything we needed, updates the screen consoleScreen.Show(); }
/// <summary> /// Handles the logical part of the scene /// </summary> /// <param name="game">Game manager</param> public void Update(Game game) { // Updates the animation flow _Animation.Update(game.DeltaMilliseconds); // Gets if any keyboard key was just 'pressed' KeyboardKey keyboardKey = game.KeyboardManager.AnyTriggered; // If no key was pressed if (keyboardKey == null) // Then does nothing return; // If Escape was pressed if (keyboardKey.Key == Key.Escape) // Moves back to the title scene game.SceneManager.NextScene = new SceneTitle(); // Checks at what scene step it is switch (_Step) { // This step is when you're selecting what action you're taking case 1: bool actionChanged = false; switch (keyboardKey.Key) { // If the Left Arrow was pressed case Key.Left: // Moves to the left option --_ActionSelected; actionChanged = true; break; // If Right Arrow was pressed case Key.Right: // Moves to the right option ++_ActionSelected; actionChanged = true; break; // If Enter was pressed case Key.Enter: // Runs the action switch (_ActionSelected) { case 0: GameGlobals.Pet.Eat(); break; case 1: GameGlobals.Pet.Sleep(); break; case 2: GameGlobals.Pet.Exercise(); break; default: GameGlobals.Pet.Play(); break; } // If the pet is fine if ( (GameGlobals.Pet.Hunger < 1.0f) && (GameGlobals.Pet.Energy > -1.0f) && (GameGlobals.Pet.Fat < 1.0f) && (GameGlobals.Pet.Happiness > -1.0f) ) { // Subtracts one from the actions left --_ActionsLeft; // Moves to the feedback screen ++_Step; } // If anything about the pet is too critical else { // Fades out to the gameover scene _Animation = new SimpleAnimation(1.0f, 0.0f, 500, Smoothness.Start, () => { game.SceneManager.NextScene = new SceneGameover(); }); _Step = 4; } break; } // If you moved to a different action if (actionChanged) { _ActionSelected %= 4; while (_ActionSelected < 0) _ActionSelected += 4; // Do a scrolling animation to the right item _Animation.Reset( _Animation.Value, _ActionSelected, 250, Smoothness.StartArrival ); } break; // This is the feedback part case 2: // If there's still an action left if (_ActionsLeft == 1) // Goes back to action selection part --_Step; // Otherwise else { // Fades out to the report scene _Animation.Reset(1.0f, 0.0f, 500, Smoothness.Start, () => game.SceneManager.NextScene = new SceneReport()); ++_Step; } break; } }
/// <summary> /// Starts the scene elements /// </summary> /// <param name="game">Game manager</param> public void Start(Game game) { // Loads the pet art from a file _PetArt = Utils.StringReadFile("Pet.txt"); // Sets the selected action to the first one _ActionSelected = 0; // This first animation is the "Day #" fadein // It goes from 0 (totally transparent) and goes to 1 (totally opaque) // It takes 500 milliseconds to complete the animation and starts fast, goes through fast and arrives slow _Animation = new SimpleAnimation(0.0f, 1.0f, 500, Smoothness.Arrival, () => { // When its done, it waits for 1000 milliseconds _Animation.Reset(1.0f, 1.0f, 1000, Smoothness.None, () => { // Then it fades out in 500 milliseconds _Animation.Reset(1.0f, 0.0f, 500, Smoothness.Start, () => { // When it's done it goes to the next step ++_Step; }); }); }); // Sets the amount of actions left to 2 _ActionsLeft = 2; }