Table of Contents
Procedural Content Generation for C++ Game Development
Credits
About the Author
Acknowledgment
About the Reviewer
www.PacktPub.comSupport files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Extra Exercises
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. An Introduction to Procedural Generation
Procedural generation versus random generation
Procedural generation
Random generation
Introducing randomness
Pseudorandom number generation
Why computers can't generate truly random numbers
Generating random numbers in C++
Generating random numbers within a range
Seeds
Defining seeds
Using seeds
Generating random seeds during the runtime
Controlled randomness is the key to generating random numbers
The use of procedural generation in games
Saving space
Map generation
Texture creation
Animation
Sound
Benefits of procedural generation
Larger games can be created
Procedural generation can be used to lower budgets
An increase in gameplay variety
An increase in replayability
The drawbacks of procedural generation
More taxing on the hardware
Worlds can feel repetitive
You sacrifice quality control
You may generate an unplayable world
It is hard to script set game events
A brief history of rogue-like games
How we'll implement procedural generation
Populating environments
Creating unique game objects
Creating unique art
Audio manipulation
Behavior and mechanics
Dungeon generation
Component-based design
The complete game
Exercises
Summary
2. Project Setup and Breakdown
Choosing an IDE
Microsoft Visual Studio
Code::Blocks
Other IDEs
Build systems
Breaking down the game template
Download templates
The class diagram
The object hierarchy
Level data
Collision
Input
Simple and Fast Multimedia Library (SFML)
Defining SFML
Why we'll be using SFML
Learning SFML
Alternatives
Polymorphism
Inheritance
Virtual functions
Pure virtual functions
Pointers and object slicing
The roguelike template setup
Downloading SFML
Linking SFML
Running the project
Adding an item
Updating and drawing
Exercises
Summary
3. Using RNG with C++ Data Types
Setting the game seed
Setting Boolean values randomly
Generating a number between 0 and 1
Choosing if an item spawns
Random number distribution
Giving the player random stats
Accessing random elements of a collection
Spawning a random item
Generating random characters
Repeating loops
Spawning a random number of items
Exercises
Summary
4. Procedurally Populating Game Environments
Potential obstacles
Keeping within the bounds of a level
Avoiding overlapping objects
Creating meaningful levels
Level tiles
Defining the spawn area
Calculating the level bounds
Checking the underlying game grid
Selecting a suitable game tile
Randomly selecting a tile
Checking whether a tile is suitable
Converting to absolute position
Spawning items at a random location
Expanding the spawning system
Using enumerators to denote an object type
Optional parameters
The complete spawn functions
Updating the spawn code
Randomly spawning enemies
Spawning random tiles
Adding a new game tile
Choosing a random tile
Implementing the SpawnRandomTiles function
Exercises
Summary
5. Creating Unique and Randomized Game Objects
Creating a random player character
Choosing a player class
An overview of sprites and textures
Setting an appropriate sprite
Buffing the player stats
Random character traits
Returning the player traits array
Setting trait sprites
Procedurally generating an enemy class
Procedural items
Random Gem and Heart classes
Random gold class
The random potion class
Creating a random potion
Determining potion pickups
Exercises
Summary
6. Procedurally Generating Art
How procedural generation is used with art
Using sprite effects and modifiers
Combining multiple textures
Creating textures from scratch
Creating complex animations
The benefits of procedurally generated art
Versatility
Cheap to produce
It requires little storage
The drawbacks of procedurally generated art
Lack of control
Repeatability
Performance heavy
Using SFML sprite modifiers
How colors work in SFML
Creating sprites of a random color
Selecting a preset color at random
Generating a color at random
Creating sprites of a random size
Saving modified sprites
Passing a texture into an image
Drawing to a RenderTexture class
Saving an image to a file
Creating enemy sprites procedurally
Breaking sprites into components
The draw setup
Randomly selecting sprite components
Loading the default armor textures
Choosing the armor tier
Rendering the armor textures
Rendering the final textures
Overriding the default draw behavior
Debugging and testing
Editing the game tiles
Exercises
Summary
7. Procedurally Modifying Audio
An introduction to SFML audio
sf::Sound versus sf::Music
sf::SoundBuffer
Selecting a random main track
Adding sound effects
Editing sound effects
Playing a sound function
The audio listener
Creating a fluctuation in a pitch
3D sound – spatialization
The audio listener
The minimum distance
Attenuation
The position of the sound
Fixed positions
Moving positions
Exercises
Summary
8. Procedural Behavior and Mechanics
An introduction to pathfinding
What is a pathfinding algorithm?
Dijkstra's algorithm
The A* algorithm
A breakdown of A*
Representing a level as nodes
The open and closed list
The H, G, and F costs
The H value
The G value
The F value
The Manhattan distance
Parenting nodes
The pseudo-algorithm
Coding the A* pathfinding algorithm
The Tile datatype
Creating supporting functions
The Level class
The Enemy class
Variable declarations
Precalculating the H values
Defining the main loop
Finding the adjacent nodes
Calculating the G and F costs
Calculating the G and F cost
Checking for superior paths
Creating the final path
Implementing A* in the game
Enabling the enemy to follow a path
Calling the pathfinding behavior
Viewing our path
Procedurally generated level goals
The variable and function declarations
Generating a random goal
Checking whether a goal is complete
Drawing the goal on the screen
Exercises
Summary
9. Procedural Dungeon Generation
The benefits of procedural level design
Replayability
A reduction in development time
Larger game worlds
Considerations
A lack of control
Required computing power
Suitability
An overview of dungeon generation overview
Generating rooms
Generating a maze
Connecting rooms and mazes
The recursive backtracker
Procedurally generating a dungeon
Changing how we view the maze
Updating the Game and Level classes
Generating a maze
Preparing before the generation of a maze
Carving passages
Adding rooms
Choosing the tile textures
The if/else approach
Bitwise tile maps
Calculating the tile values
Mapping the tile value to textures
Calculating tile textures
Creating unique floor themes
Adding entry and exit points
Setting a player's spawn location
Undoing the debug changes
Exercises
Summary
10. Component-Based Architecture
Understanding component-based architecture
Problems with a traditional inheritance-based approach
Convoluted inheritance structures
Circular dependencies
Benefits of component-based architecture
Avoiding complex inheritance structures
Code is broken into highly reusable chunks
Highly maintainable and scalable
The disadvantages of component-based architecture
Code can become too fragmented
Unnecessary overhead
Complex to use
An overview
Designing the component system
C++ templates
Using templates
Template declarations
Using templates
Template specialization
Function overloading
Creating a base component
Component functions
Attaching a component
Retuning a component
Creating a transform component
Encapsulating transform behavior
Adding a transform component to the player
Using the transform component
Updating the game code
Creating a SpriteComponent
Encapsulating sprite behavior
Adding a sprite component to the player class
The updated drawing pipeline
Updating the game code
Creating an audio component
Defining the behavior of an audio component
Adding an audio component to the player class
Using the audio component
Exercises
Summary
11. Epilogue
Project breakdown
Procedurally populating environments
Creating unique and random game objects
Procedurally generating art
Procedurally modifying audio
Procedural behavior and mechanics
Procedural dungeon generation
Component-based architecture
The pros and cons of procedural generation
Pros
Cons
Summary
Index