Swift Game Development Third Edition Learn iOS 12 game development using SpriteKit, SceneKit and ARKit 2.0 Siddharth Shekar Stephen Haney BIRMINGHAM - MUMBAI Swift Game Development Third Edition Copyright © 2018 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Commissioning Editor: Ashwin Nair Acquisition Editor: Reshma Raman Content Development Editors: Flavian Vaz Technical Editor: Akhil Nair Copy Editor: Safis Editing Project Coordinator: Sheejal Shah Proofreader: Safis Editing Indexers: Tejal Daruwale Soni Graphics: Jason Monteiro Production Coordinator: Shantanu Zagade First published: July 2015 Second published: February 2017 Third edition: September 2018 Production reference: 1260918 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78847-115-2 www.packtpub.com mapt.io Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? • Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals • Learn better with Skill Plans built especially for you • Get a free eBook or video every month • Mapt is fully searchable • Copy and paste, print, and bookmark content Packt.com Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.Packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.Packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. Contributors About the authors Siddharth Shekar is a game developer and teacher with over 5 years of industry experience and 12 years of experience in C++ and other programming languages, and is adept at graphics libraries such as OpenGL and Vulkan and game engines such as Unity and Unreal. He has also published games on the iOS and Android App Stores. Siddharth has also authored 4 books, including Mastering Android Game Development with Unity and Learning iOS 8 Game Development Using Swift, all published by Packt Publishing He currently lives in New Zealand and is a lecturer in the Games Department at Media Design School. He teaches graphics programming and PlayStation 4 native game development, and mentors final year production students. I would like to thank my parents for supporting me in everything that I choose to do. I would also like to thank Media Design School for encouraging me to continue working on this book. Finally, I would like to thank Packt Publishing for putting this book together and offering me the opportunity to write the book. Stephen Haney has written two books on iOS game development. He began his programming journey at the age of 8 years on a dusty, ancient laptop using BASIC. He has been fascinated with building software and games ever since. Now well versed in multiple languages, he enjoys programming as a creative outlet the most. He believes that indie game development is an art form—an amazing combination of visual, auditory, and psychological challenges—rewarding to both the player and the creator. He enjoyed writing this book and sincerely hopes that it directly furthers your career or hobby. About the reviewer Rafał Sroka is a Software Engineer with extensive expertise in iOS development, building-up mobile apps, QA and release process, API design, and complex software projects. He has worked on various high-value projects in Poland, Germany, and Switzerland. He prefers working in cross-functional start-up environments where fast delivery of well-written and tested code is crucial. He holds a Master's degree in Electronics and Telecommunications. In his free time, he's a dedicated rock climber and mountaineer. He is a doer. Packt is Searching for Authors Like You If you're interested in becoming an author for Packt, please visit authors.packtpub. com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. Table of Contents Preface ix Chapter 1: Designing Games with Swift 1 Why you will love Swift 2 Prerequisites 3 What you will learn in this book 3 Embracing SpriteKit 4 Reacting to player input 4 Structuring your game code 5 Building UI/menus/levels 5 Integrating with Game Center 5 Maximizing fun 5 Crossing the finish line 5 Monetizing your work 6 New in Swift 4.2 6 Setting up your development environment 7 Introducing and installing Xcode 7 Creating our first Swift game 8 Navigating our project 13 Exploring the SpriteKit demo 13 Examining the demo code 16 Cleaning up 16 Summary 17 Chapter 2: Sprites, Camera, Action! 19 Preparing your project 20 Drawing your first sprite 23 Building a SKSpriteNode class 23 [ i ] Table of Contents Animation—movement, scaling, and rotation 25 Sequencing multiple animations 26 Recapping your first sprite 26 The story on positioning 27 Alignment with anchor points 27 Working with textures 29 Downloading the free assets 29 More exceptional art assets 29 Drawing your first textured sprite 29 Adding the bee image to your project 30 Loading images with SKSpriteNode 30 Designing for Retina 31 The ideal asset approach 32 Hands-on with retina in SpriteKit 32 Organizing art into texture atlases 33 Exploring Assets.xcassets 33 Collecting art into texture atlases 34 Updating our bee node to use the texture atlas 35 Iterating through texture atlas frames 35 Putting it all together 37 Centering the camera on a sprite 37 Summary 41 Chapter 3: Mix in the Physics 43 Laying the foundations 43 Adopting a protocol for consistency 44 Organizing game objects into classes 44 The icy tundra 46 Adding the player's character 50 Renovating the GameScene class 54 Physics bodies and gravity 55 Dropping like flies 55 Solidifying the ground 56 Exploring physics simulation mechanics 57 Bumping bees into bees 60 Movement with impulses and forces 61 Summary 62 Chapter 4: Adding Controls 63 Retrofitting the Player class for flight 64 The Beekeeper 64 Updating the Player class 64 [ ii ] Table of Contents Moving the ground 64 Assigning a physics body to the player 65 Creating a physics body shape from a texture 65 Polling for device movement with Core Motion 66 Implementing the Core Motion code 66 Wiring up the sprite onTap events 68 Implementing touchesBegan in the GameScene 68 Larger than life 69 Teaching our penguin to fly 70 Listening for touches in GameScene 71 Fine-tuning gravity 72 Spreading your wings 72 Improving the camera 73 Pushing Pierre forward 76 Tracking the player's progress 77 Looping the ground as player the moves forward 77 Summary 79 Chapter 5: Spawning Enemies, Coins, and Power-Ups 81 Introducing the cast 82 Locating and adding the art assets 82 Adding the Power-up Star 84 Adding the Star class 84 A new enemy - the Mad Fly 86 Adding the MadFly class 86 Another terror - Bats! 87 Adding the Bat class 87 Guarding the ground with the Blade 88 Adding the Blade class 89 Adding coins 90 Creating the coin classes 90 Organizing the project navigator 91 Testing the new game objects 92 Preparing for endless flight 94 Summary 95 Chapter 6: Generating a Never-Ending World 97 Designing levels with the SpriteKit scene editor 98 Separating level data from game logic 99 Using custom classes in the scene editor 99 Building encounters for Pierre Penguin 99 Creating our first encounter 100 [ iii ]