33
Developing the Game Functionality Lesson 6

Developing the Game Functionality

  • Upload
    truong

  • View
    43

  • Download
    0

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: Developing the Game Functionality

Developing the Game Functionality

Lesson 6

Page 2: Developing the Game Functionality

Exam Objective MatrixSkills/Concepts MTA Exam ObjectivesProgramming the Components

Understand Components (1.5)Capture User Data (1.6)Work with XNA (1.7)

Handling Game Data Work with XNA (1.7)

Page 3: Developing the Game Functionality

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

Page 4: Developing the Game Functionality

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.

Page 5: Developing the Game Functionality

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.

Page 6: Developing the Game Functionality

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.

Page 7: Developing the Game Functionality

Programming the Game: Showing ChangesFull health Low health

Page 8: Developing the Game Functionality

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.

Page 9: Developing the Game Functionality

Programming the Game: Showing ChangesFull magazine

Partially full magazine

Page 10: Developing the Game Functionality

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.

Page 11: Developing the Game Functionality

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.

Page 12: Developing the Game Functionality

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

Page 13: Developing the Game Functionality

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:1. Get the player character's position on the screen.2. If the player character's position is in the defined range,

then assign a random wait time to the sprite. 3. 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.

4. As soon as the wait time is over, make the sprite appear in some other place on the scene.

5. Continue steps 2 to 4 until the player catches the sprite.

Page 14: Developing the Game Functionality

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:

1. Calculate the difference between the player position and the AI position and decide on the AI direction of travel.

2. Normalize the direction, then add randomness to the direction.

3. Move towards the calculated player position.

Page 15: Developing the Game Functionality

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.

Page 16: Developing the Game Functionality

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.

Page 17: Developing the Game Functionality

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.

Page 18: Developing the Game Functionality

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.

Page 19: Developing the Game Functionality

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.

Page 20: Developing the Game Functionality

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.

Page 21: Developing the Game Functionality

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.

Page 22: Developing the Game Functionality

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 StorageContainer object.

Page 23: Developing the Game Functionality

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 FileExists method of the StorageContainer class to check if a save file exists, or use the DeleteFile method to delete an existing save file.

Page 24: Developing the Game Functionality

Defining Game Data to Savepublic struct PlayerData{

public string PlayerName; public Vector2PlayerPosition; public int Level; public int Score; public List<string> completedAchievements;/* If game is having 24 hr day night system we need to save that.*/ public System.TimeSpan currentGameTime;/* If your game has some weather condition , need to save it too . My game has a fog effect.*/ public bool fogEnable;

}

Page 25: Developing the Game Functionality

Creating a StorageContainer Object// Open a storage container.IAsyncResult asyncResult= 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();

Page 26: Developing the Game Functionality

Checking if Save File Existsstring 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);

Page 27: Developing the Game Functionality

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.XmlSerializer serializer = new XmlSerializer(typeof(PlayerData));

Page 28: Developing the Game Functionality

Stream Data into the Save File, Close the FilePlayerData playerData = 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 containderContainer.Dispose();

Page 29: Developing the Game Functionality

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 XmlSerializer object.

– Close the stream and dispose of the StorageContainer object.

Page 30: Developing the Game Functionality

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

Page 31: Developing the Game Functionality

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.

Page 32: Developing the Game Functionality

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;}

Page 33: Developing the Game Functionality

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