This tutorial will hopefully provide some introductory insights into incorporating data structures into game design. The app on which this tutorial is based is a game called "Alphabet Ring Flier," available in the AppStore, where the user must control a biplane in order to fly through letter rings with the goal of spelling specific words. In the meantime, the player must avoid a series of enemies that are spawned randomly at different times and places.
First, we define an Encounter class, which will encapsulate the data required to generate an encounter with enemy characters. This Encounter class also corresponds to a node in a linked list. Hence, by linking together a series of nodes in this way, we can obtain a data structure that contains all the data necessary to create a series of enemy encounters for a single level of gameplay.
To begin with, we define a class called Encounter and define a series of variables with optional integer types. These optional integer variables include our enemies (i.e. numberOfSpaceCraft, numberOfAlienHeads, numberOfFireballs) as well as the letter rings through which the player must fly (i.e. numberOfLetters). Another variable waitTime will be a non-optional double and will represent the amount of time between enemy encounters. Finally, another variable nextEncounter will be of the Encounter type, that is, the same type as that which we are currently defining, and will represent the next encounter in a series of encounters (i.e. the next node in a series of linked nodes)
var nextEncounter: Encounter? var waitTime: Double = 5.00 var numberOfSpikeBalls: Int? var numberOfLetters: Int? var numberOfFireballs: Int? var numberOfSpaceCraft: Int? var numberOfAlienHeads: Int? init(waitTime: Double, numberOfSpikeBalls: Int?, numberOfSpaceCraft: Int?, numberOfLetters: Int?,numberOfAlienHeads: Int?, numberOfFireballs: Int?){ self.waitTime = waitTime self.numberOfSpikeBalls = numberOfSpikeBalls self.numberOfSpaceCraft = numberOfSpaceCraft self.numberOfLetters = numberOfLetters self.numberOfFireballs = numberOfFireballs self.numberOfAlienHeads = numberOfAlienHeads nextEncounter = nil }
In addition to the variables and the initializer defined above, we will also define two functions setNextEncounter(waitTime:numberOfSpikeBalls:numberOfSpaceCraft:numberOfLetters:numberOfAlienHeads:numberOfFireballs) which will be used to set the next encounter in a series. This function will return the next encounter in the series, allowing us to chain these functions together so as to create an encounter series.
The other function getNextEncounter() is simply a getter for returning the next encounter in the series for the current enemy encounter.
class Encounter{ func setNextEncounter(waitTime: Double, numberOfSpikeBalls: Int?, numberOfSpaceCraft: Int?, numberOfLetters: Int?, numberOfFireballs: Int?, numberOfAlienHeads: Int?) -> Encounter{ self.nextEncounter = Encounter(waitTime: waitTime, numberOfSpikeBalls: numberOfSpikeBalls, numberOfSpaceCraft: numberOfSpaceCraft, numberOfLetters: numberOfLetters, numberOfAlienHeads: numberOfAlienHeads, numberOfFireballs: numberOfFireballs) return self.nextEncounter! } func getNextEncounter() -> Encounter?{ return nextEncounter } }
Now that we have our "node" defined, we are to define an encounter series, and after that, a class that will generate a series of encounter series so that we can conveniently and scalably configure customizable game levels. This provides an alternative to the common technique of using timers to spawn enemies after regular intervals.
To continue, please click here
Or you can return to the tutorials table of contents: tutorials table of contents