Building XNA 2.0 Games A Practical Guide for Independent Game Development ■■■ James Silva and John Sedlak Building XNA 2.0 Games: A Practical Guide for Independent Game Development Copyright © 2008 by James Silva and John Sedlak All rights reserved No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher ISBN-13 (pbk): 978-1-4302-0979-9 ISBN-13 (electronic): 978-1-4302-0980-5 Printed and bound in the United States of America Trademarked names may appear in this book Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark Lead Editor: Ewan Buckingham Technical Reviewer: Fabio Claudio Ferracchiati Editorial Board: Clay Andres, Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Kevin Goff, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper, Frank Pohlmann, Ben RenowClarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Beth Christmas Copy Editor: Marilyn Smith Associate Production Director: Kari Brooks-Copony Production Editor: Ellie Fountain Compositors: Susan Glinert and Octal Publishing, Inc Proofreader: Nancy Sixsmith Indexer: Carol Burbo Artist: Kinetic Publishing Services, LLC Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013 Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit http://www.springeronline.com For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705 Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www.apress.com Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use eBook versions and licenses are also available for most titles For more information, reference our Special Bulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales The information in this book is distributed on an “as is” basis, without warranty Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work The source code for this book is available to readers at http://www.apress.com This book is dedicated to my mom and dad, who were always supportive of my game development obsession —James Silva Contents at a Glance About the Authors xiii About the Technical Reviewer xv Acknowledgments xvii Introduction xix ■CHAPTER A NET Snapshot ■CHAPTER A Crash Course in XNA 19 ■CHAPTER Planning Your Game 41 ■CHAPTER The Map Editor 51 ■CHAPTER The Character Editor 93 ■CHAPTER Bringing It to the Game 127 ■CHAPTER Particle Mayhem 171 ■CHAPTER XACT Audio, Rumble, and More 221 ■CHAPTER Scripting, AI, and Depth (and Death) 249 ■CHAPTER 10 Menus, a HUD, and Deployment 291 ■CHAPTER 11 Postprocessing Effects 333 ■CHAPTER 12 Networking 361 ■APPENDIX A Designing the Wraith 399 ■APPENDIX B Storage 413 ■INDEX 421 v ■I N D E X creating AI class for, 402 XNA, a crash course in, 19–39 creating characters for the game, 399–411 designing with, 399–411 XNA Framework, eliminating classic networking hassles with, 361 getting previous state and location of, 408–410 XNA Framework Remote Performance Monitor for Xbox 360, 331 graphics, 400 XNA Game Studio, connecting the Xbox 360 to, 329–330 initialization script for, 401 wraith characters definition script, sounds added to, 402 process of creating, 399–400 wrench triggers adding new to appropriate frames of animation, 210 defining, 209–210 XNA Game Studio 2.0 installing, 19 networking with, 361–362 working with pixel shaders in, 333 XNA Game Studio Connect downloading and launching, 329–330 XNAPong in character editor, 210 adding a background image, 33–35 putting to use for smashing zombies, 209–216 adding rumble to, 35–36 Write() function adding the game logic, 27–32 ball logic for, 31–32 creating in character editor, 122–124 building, 20–39 making in Map class, 88 class-level (Game1.cs) variables, 28 using device and container to get a path, 415 coding audio for, 38–39 creating file for graphics in, 24 WriteToNet() function, 385–386 loading textures, 23 ■X paddles and gamepad logic, 28–31 XACT See also Cross-Platform Audio Creation Tool (XACT) setting up game audio in, 229–231 task list for game logic, 27 ■Y XACT Audio, Rumble, and More, 221–248 you-are-dead screen, for ZombieSmashers game, 325 XACT Auditioning Utility, for auditioning sound files, 231–232 ■Z XACT project zap cue object, playing from XNAPong, 38 creating new, 229 zdx filename extension, 59 creating new wave bank file in, 229–231 zombie attack, adding animation for, 251 Xbox 360 deploying ZombieSmashers game on, 328 deployment, 291 storage container object, 413 Xbox 360 project creating, 328 debugging, 331 zombie bloodsplosions, adding triggers for, 250–251 Zombie class, extending AI base class with, 265 zombie head splatter, 267 zombie hit trigger, adding in CharacterEditor, 250–251 441 442 ■I N D E X Zombie Smashers XNA game, keeping separate functions organized, 136 fields used in, 147–148 ZombieSmashersXna project See also ZombieSmashers game in-game scoring, 327 adding CharacterEditor to, 93–94 building the game, 127–153 cleaning up odds and ends, 168–169 combat concept art, 46 creating, 51–53 hero concept art, 46 maps concept art, 47 implementing the action in, 210–216 initializing, 148 loading game content, 148 making enemies killable, 249–267 manually changing namespaces in, 128 parting word from author, 397–398 plugging everything into, 394–395 reorganizing the code, 322–326 naming, 49 setting character’s facing, scale, state and starting animation in, 135–136 rough game plan for, 49–50 setting things in motion, 147–149 super simple scripting, 153–167 super simple scripting, 153–167 tool planning for, 47–48 updating, 320–326 zombie.zmx file, adding to ZombieSmashers game, 199–204 updating frameTime calculating and sound, 324–325 zombies updating scroll and handling input, 148–149 adding animations to, 250–251 adding to ZombieSmashers game, 199–204 bringing into ZombieSmashers game, 202–204 in CharacterEditor, 199–201 making killable, 249–267 putting wrench to use for smashing, 209–216 shooting, 204 smashing, 204–216 ZombieSmashers game adding a background image to, 150–153 adding the HUD and menu to, 320–321 bringing it all together, 285–289 bringing triggers into, 191–196 bringing zombies into, 202–204 class-level objects, 147 creating, 128 drawing one character for, 149 ZombieSmashers project adding MapEditor project to, 52–53 creating, 51–53 ZombieSmashersXna project adding audio to, 229–239 adding music to, 236–239 bringing audio functionality into, 233 sounds used for, 222–223 ZombieSmashersXNA.Particles namespace creating BloodDust class in, 255–256 Zombie-Smashing game choosing 2D or 3D for, 45 concept image for protagonist, 46 initial design, 46–47 maps, 47 planning, 45–50 tool planning for, 47–48 .. .Building XNA 2. 0 Games A Practical Guide for Independent Game Development ■■■ James Silva and John Sedlak Building XNA 2. 0 Games: A Practical Guide for Independent Game Development... 148–149 adding animations to, 25 0? ? ?25 1 adding to ZombieSmashers game, 199? ? 20 4 bringing into ZombieSmashers game, 20 2? ? 20 4 in CharacterEditor, 199? ? 20 1 making killable, 24 9? ?26 7 putting wrench to use for. .. Audio Creation Tool (XACT) setting up game audio in, 22 9? ?23 1 task list for game logic, 27 ■Y XACT Audio, Rumble, and More, 22 1? ?24 8 you-are-dead screen, for ZombieSmashers game, 325 XACT Auditioning