Reader small image

You're reading from  Swift Game Development - Third Edition

Product typeBook
Published inSep 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781788471152
Edition3rd Edition
Languages
Right arrow
Authors (2):
Siddharth Shekar
Siddharth Shekar
author image
Siddharth Shekar

Siddharth Shekar is a game developer and teacher with over 6 years' industry experience and 12 years' experience in C++ and other programming languages. He is adept at graphics libraries such as OpenGL and Vulkan, and game engines such as Unity and Unreal. He has published games on the iOS and Android app stores. He has also authored books including Swift Game Development, Mastering Android Game Development with Unity, and Learning iOS 8 Game Development Using Swift, all published by Packt Publishing. He currently lives in Auckland, New Zealand, and is a lecturer in the games department at Media Design School. He teaches advanced computer graphics programming, PlayStation 4 native game development, and mentors final year production students.
Read more about Siddharth Shekar

Stephen Haney
Stephen Haney
author image
Stephen Haney

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 forman amazing combination of visual, auditory, and psychological challengesrewarding to both the player and the creator. He enjoyed writing this book and sincerely hopes that it directly furthers your career or hobby.
Read more about Stephen Haney

View More author details
Right arrow

Chapter 6. Generating a Never-Ending World

The unique challenge of an endless flyer-style game is in procedurally generating a rich, entertaining game world that extends as far as your player can fly. We will first explore level design concepts and tooling in Xcode; Apple added a built-in level designer to Xcode 6, allowing developers to arrange nodes visually within a scene. Once we become familiar with the SpriteKit level design methodology, we will create a custom solution to generate our world. In this chapter, you will build an entertaining world for our penguin game and learn how to design and implement levels in SpriteKit for any genre of game.

The topics in this chapter include the following:

  • Designing levels with the SpriteKit scene editor

  • Building encounters for Pierre Penguin

  • Integrating scenes into the game

  • Looping encounters for a never-ending world

  • Adding the Power-up Star at random places

  • Turning bronze coins into gold coins

Designing levels with the SpriteKit scene editor


The Scene editor is a valuable addition to SpriteKit. Previously, developers would be forced to hard-code positional values or rely on third-party tools or custom solutions for level design. Now, we can lay out our levels directly within Xcode by dragging and dropping sprites. We can create nodes, attach physics bodies and constraints, create physics fields, and edit properties directly from the interface.

Here is a simple example scene you might build by simply clicking and dragging:

In this example, I simply dragged and positioned sprites in the scene. If you are making an unsophisticated game, you can start in the scene editor rather than creating custom classes. By editing physics bodies in the editor, you can even create entire physics-based games in the editor, adding only a few lines of code for the controls.

Complex games require custom logic and texture animation for every object, so we will implement a system in our penguin game that...

Building encounters for Pierre Penguin


Endless flyer games continue until the player loses. They do not feature distinct levels; instead, we will design encounters (my own term) for our protagonist penguin to explore. We can create an endless world by stringing together encounters one after the other and randomly recycling from the beginning when we need more content.

The following diagram illustrates this basic concept:

A finished game might include twenty or more encounters to feel varied and random. We will create three encounters in this chapter in order to populate the encounter recycling system.

We will build each encounter in its own scene file, in the same way we would approach a separate level in a standard platformer or physics game.

Creating our first encounter

First, create an encounter folder group to keep our project organized. Right-click your project in the project navigator and create a new group named Encounters. Then, right-click on Encounters and add a new SpriteKitScene file...

Looping encounters for a never-ending world


We need at least three encounters to endlessly cycle and create a never-ending world; two can be on the screen at any one time, with a third positioned ahead of the player. We can track Pierre's progress and reposition the encounter nodes ahead of him.

Building more encounters

We need to build at least two more encounters before we can implement the repositioning system. You can create more if you like; the system will support any number of encounters. For now, add two more SpriteKit scene files to your game: EncounterB.sks and EncounterC.sks. Resize these scenes to 900 wide by 600 tall, the same as EncounterA. You can fill these encounters with Bees, Blades, Coins, and Bats—have fun! Make sure to assign the custom class attribute to the sprites that you drag into the scene editor.

For inspiration, here is my EncounterB.sks:

Here is my EncounterC.sks:

For perfect alignment with the ground, place your Blade sprites at -224 on the Y-axis.

Updating the...

Adding the Power-up Star at random places


We still need to add the Power-up Star into the world. We can randomly spawn a Star every ten encounters to add some extra excitement. Follow these steps to add the Star logic:

  1. Add a new instance of the Star class as a constant on the GameScene class:

            let powerUpStar = Star() 
  2. Anywhere inside the GameScene didMove function, add the Star as a child of the GameScene and position it:

            // Place the star out of the way for now: 
            self.addChild(powerUpStar) 
            powerUpStar.position = CGPoint(x: -2000, y: -2000) 
  3. Inside the GameScene didSimulatePhysics function, update your new encounter code as follows (the new code is in bold):

            // Check to see if we should set a new encounter: 
            if player.position.x > nextEncounterSpawnPosition { 
                encounterManager.placeNextEncounter( 
                    currentXPos: nextEncounterSpawnPosition) 
                nextEncounterSpawnPosition += 1200 
         
                // Each encounter...

Turning bronze coins into gold coins


You may notice that your gold coins in the scene editor show up as bronze coins in the game. This is because the Coin class defaults to using the bronze texture and value. We will specify our gold coins by setting their name attribute to gold in the scene editor and then checking for this name in the Coin init function. Follow these steps to implement golden coins:

  1. Open your encounters in the scene editor, select your gold coins, and use the Attributes inspector to set the name value of each node to gold, as shown here:

  2. We can easily loop through all nodes with a certain name. We will use this functionality to run the Coin class's turnToGold function on any node with the name gold. Open EncounterManager and add the following code in the init function, at the bottom of the encounter's for loop (the new code is in bold):

            // Save initial sprite positions for this encounter: 
            saveSpritePositions(node: encounterNode) 
            // Turn golden coins...

Summary


Great job—we have covered a lot of ground in this chapter. You learned about Xcode's new scene editor, used the scene editor to lay out sprites with custom classes, and interpreted the scene data to spawn game objects in our game world. Then, you created a system to loop encounters for our endless flyer game.

Congratulate yourself; the encounter system you built in this chapter is the most complex system in our game. You are officially in a great position to finish your first SpriteKit game!

Next, we will look at creating custom events when game objects collide. We will add health, damage, coin pick-up, invincibility, and more in Chapter 7, Implementing Collision Events.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Swift Game Development - Third Edition
Published in: Sep 2018Publisher: PacktISBN-13: 9781788471152
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Siddharth Shekar

Siddharth Shekar is a game developer and teacher with over 6 years' industry experience and 12 years' experience in C++ and other programming languages. He is adept at graphics libraries such as OpenGL and Vulkan, and game engines such as Unity and Unreal. He has published games on the iOS and Android app stores. He has also authored books including Swift Game Development, Mastering Android Game Development with Unity, and Learning iOS 8 Game Development Using Swift, all published by Packt Publishing. He currently lives in Auckland, New Zealand, and is a lecturer in the games department at Media Design School. He teaches advanced computer graphics programming, PlayStation 4 native game development, and mentors final year production students.
Read more about Siddharth Shekar

author image
Stephen Haney

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 forman amazing combination of visual, auditory, and psychological challengesrewarding to both the player and the creator. He enjoyed writing this book and sincerely hopes that it directly furthers your career or hobby.
Read more about Stephen Haney