330 likes | 461 Views
Developing the Game Functionality. Lesson 6. Exam Objective Matrix. Programming the Components. A significant part of game development goes into creating and programming the game functionality. The game development process comprises of the following elements: Creating tools
E N D
Developing the Game Functionality Lesson 6
Programming the Components • A significant part of game development goes into creating and programming the game functionality. • The game development process comprises of the following elements: • Creating tools • Programming the game • Incorporating AI
Understanding Tool Creation • Almost every game involves the use of game tools for tasks such as importing or converting art, building levels, and so on. • Tool creation is the process when a game programmer creates game tools. • Tools created might include: • Asset conversion tools, level editors or map editors.
Tools • Asset conversion tools are programs that convert the artwork such as 3D models into formats required by the game. • Level editor tools allow the game player to customize or modify levels within a game. • Map editor tools allow the game player to create custom maps with no knowledge of programming skills.
Programming the Game • After you load or add game components, such as sprites, textures, game models, and images in your game, you need to make the components functional. • To make the components functional, as per the player’s input or as per the game’s progress, you need to further program the components.
Programming the Game: Showing Changes Full health Low health
Programming the Game: Health Change • The code sample in the textbook provides an easy way to show changes in the player character’s health by pressing keyboard keys. • In the actual game, you would need to map these changes in variable value to events such as taking damage or picking up a health kit. • Notice the color change as an extra indicator to the player of the character's health state.
Programming the Game: Showing Changes Full magazine Partially full magazine
Programming the Game: Using Bullets • The code sample in the textbook provides an easy way to show changes in the number of bullets in the character’s magazine by pressing keyboard keys. • In the actual game, you would need to map these changes in variable value to events such as shooting the gun (pressing a button) or picking up ammunition.
Programming the Game: Working the Deltas • Notice that in both of the previous examples, the majority of the game assets were unchanged. • As the player’s health changed, or the number of remaining bullets changed, only those items required an update. • You’ll want to change as little as possible by using drawable components.
Incorporating Artificial Intelligence (AI) • Incorporating AI involves programming the behavior of the nonplayer character (NPC) as close to a human as possible. • You can add AI for various game scenarios, as each technique indicates. • Popular AI techniques: • Evading AI • Chasing AI • Flocking or Grouping AI • Path finding AI
Evading AI • One of the scenarios that present an opportunity to incorporate AI is to make the movement of an object or NPC intelligent. • Simple sample of evading AI: • Get the player character's position on the screen. • If the player character's position is in the defined range, then assign a random wait time to the sprite. • Do not move the sprite until the waiting time is over; allows the player to catch the sprite if he reaches the sprite before the wait time is over. • As soon as the wait time is over, make the sprite appear in some other place on the scene. • Continue steps 2 to 4 until the player catches the sprite.
Chasing AI • The player must evade the AI instead of chasing (or catching the AI). • The chasing AI is used when the player must battle the computer controlled NPC. • Simplified process: • Calculate the difference between the player position and the AI position and decide on the AI direction of travel. • Normalize the direction, then add randomness to the direction. • Move towards the calculated player position.
Flocking or Group AI • Certain NPCs, such as a group of soldiers, should to move together without walking over each other. • Based on the Craig Reynolds algorithm. • This algorithm follows three simple rules: • Steer and avoid colliding into other members of the group. • Align towards the average heading of the members of the group. • Steer or move toward the average position of the members of the group.
Path Finding AI • This AI involves moving a game object in an effort to find a path around an obstacle. • Most common techniques: • Breadcrumb path following: The player character progresses through the game marking some invisible markers or "breadcrumbs" on her path unknowingly. • Waypoint navigation: Allows you to place reference points in the game world which allows the use of these precalculated paths in the appropriate path finding algorithms.
Other Pathfinding AI Techniques • Terrain analysis helps to increase the capabilities of an AI opponent by providing information about the game world. For example, information about hills, ambush points, water bodies, and so on can be used by the AI opponent to its advantage. • An influence map is a technique that warns the computer-controlled opponent when its enemy is spotted. • Visibility graphs break down larger areas into smaller areas that are interconnected. This technique is also used to give the game AI an advantage over the player.
Handling Game Data • Game data means information about every element in a game. It includes the player or user data that encompasses the AI data and the level data. The AI data includes information about the position of the NPC and the position of the player character on the game screen. • Handling game data involves capturing/retrieving the game data back and forth from a disk file and managing the state of the game.
Primary Reasons to Save Game Data • Allows storing the player’s progress in the current game session and reloading the game from the last saved point in the next session. • To allure customers, game manufacturers today provide the player with the flexibility of saving the game at any point of time during the gameplay, at the end of each game level, or at the specific designated areas within the game.
Capturing User Data • You can capture the game data by using the XmlSerializer and the StorageContainer classes in XNA 4.0. • The XmlSerializer class serializes objects into XML documents and deserializes the objects from XML documents.
Serializing Data • Serializing an object means translating its public properties and fields into a serial format, such as an XML document for storage or transport. • It is the way of saving an object's state into a stream or buffer. • Deserialization is the process of getting back the object in its original state from the serialized form.
Storage of Game Data • The StorageContainer class is a logical set of storage files. • You can create files to store the state of the various objects and components in the game and store the files in a StorageContainerobject.
Storing the Game Data • To store the game data, you need to write code to perform the following tasks: • Define all of the data that is to be stored (level, score, character name chosen, etc.). • Serialize the game data into the required game file. • Use the FileExistsmethod of the StorageContainer class to check if a save file exists, or use the DeleteFile method to delete an existing save file.
Defining Game Data to Save public structPlayerData { public string PlayerName; public Vector2PlayerPosition; public intLevel; public intScore; public List<string> completedAchievements; /* If game is having 24 hr day night system we need to save that.*/ public System.TimeSpancurrentGameTime; /* If your game has some weather condition , need to save it too . My game has a fog effect.*/ public boolfogEnable; }
Creating a StorageContainer Object // Open a storage container. IAsyncResultasyncResult= device.BeginOpenContainer("SavingPlayerProfile", null, null); // Wait for the WaitHandle to become signaled. asyncResult.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(asyncResult); // Close the wait handle. asyncResult.AsyncWaitHandle.Close();
Checking if Save File Exists string filename = "savedGameState.sav"; // Check to see whether the save exists. if (container.FileExists(filename)) // Delete it so that we can create one fresh. container.DeleteFile(filename);
Create the Save File and XMLSerializer Object // Create the file. Stream fileStream = container.CreateFile(filename); // Convert the object to XML data and put it in the stream. XmlSerializerserializer = new XmlSerializer(typeof(PlayerData));
Stream Data into the Save File, Close the File PlayerDataplayerData = new PlayerData(); /* Then set playerData with appropriate info from game */ playerData.fogEnable = true; playerData. PlayerName = “your name”; playerData.Score = 300; serializer.Serialize(fileStream, playerdata); // Close the file. fileStream.Close(); //dispose the containder Container.Dispose();
Loading the Game Data • Loading or reading the game data from the game file involves the following tasks: • Create a StorageContainer object to access the game file • Deserialize the game data to load using the Deserialize method of the XmlSerializerobject. • Close the stream and dispose of the StorageContainerobject.
Managing Game States • A game state defines the behavior of every object at any given point in the game. • In general, the simplest of games can have the following game states: • Initialize • DisplayMenu • PlayerPlay • PlayerLose • GameOver
Managing Game States • In general, the game tends to behave according to the value of the game state. • You manage game states according to the player’s action(s). • You must first define all possible game states, then program the code to support the various states and the transition between the states.
Basic Switch Logic for Managing Game State • Not all inclusive below! switch (currentGameState) { case GameState.PlayerPlay: updatePlayerGamePlayLoop(gameTime); break; case GameState.PlayerLose: if (player.IsReplay) { player.Reinitialize(); currentGameState= GameState.PlayerPlay; } else { currentGameState= GameState.GameOver; }
Recap • Programming the Components • Understanding Tool Creation • Tools • Programming the Game • Incorporating Artificial Intelligence (AI) • Evading AI • Chasing AI • Flocking or Grouping AI • Path finding AI • Other Pathfinding AI Techniques • Handling Game Data • Capturing User Data • Serializing Data • Storage the Game Data • Loading the Game Data • Managing Game States