Overview
Building a game is a time consuming and difficult task. You need to bring together many different elements such as visual assets, game play strategy, challenges, and above all the code to bring this altogether. This is where Scroller2d attempts to help accelerate your ideas by providing a framework and visual editor to create levels in a data driven approach that allows you to modularize your game level core logic.
Every level has repeatable core principals: background, floor (horizontal scrolling), enemies, obstacles, collectibles, checkpoints, and artifacts.
![A diagram with the five sloth power types: ice, fire, wind, lightning, and none.]
Create a Level
To create a level you can interogate the Level struct or you can start with LevelEditor to save an initial level.
Upon saving your new level, you will immediately notice a handful of things.
Every element has a GUID based id. This is an internal value primarily used within LevelEditor.
To create a standard sloth without any special supernatural powers, you initialize a new instance of the structure, and supply a name and a color, as the following code shows:
var sloth = Sloth(name: "Super Sloth", color: .blue, power: .none)
If your sloth possesses one of the special powers of ice, fire, wind, or lightning, you can specify this at creation:
var superSloth = Sloth(name: "Silly Sloth", color: .green, power: .lightning)
If you’re creating a large number of sloths, you can define your own random name generator that conforms to the protocol, and use it to generate names:
let slothNamer = MyCustomSlothNamer()
var sloths: [Sloth] = []
for _ in 0...100 {
let name = slothNamer.generateName(seed: 0)
var sloth = Sloth(name: name, color: .green, power: .ice)
sloths.append(sloth)
}
Provide a Habitat
Sloths thrive in comfortable habitats. To create a sloth-friendly habitat, you specify whether it’s humid, warm, or both. The following listing creates a habitat that’s humid and warm, which results in a high :
let lovelyHabitat = Habitat(isHumid: true, isWarm: true)
After you create a sloth habitat, you’re ready for sloths to sleep in it. Sleeping in a habitat increases the of the sloth by the comfort level of the habitat. Sloths sleep for long periods so, by default, your sloth sleeps for 12 hours, but you can also customize this value:
superSloth.sleep(in: lovelyHabitat)
hyperSloth.sleep(in: lovelyHabitat, for: 2)
Exercise a Sloth
To keep your sloths happy and fulfilled, you can create activities for them to perform. Define your activities by conforming to the protocol and implementing the method:
struct Sightseeing: Activity {
func perform(with sloth: inout Sloth) -> Speed {
sloth.energyLevel -= 10
return .slow
}
}
Feed a Sloth
Sloths require sustenance to perform activities, so you can feed them. Standard sloth food includes leaves and twigs:
superSloth.eat(.largeLeaf)
hyperSloth.eat(.twig)
Schedule Care for a Sloth
To make it easy to care for your sloth, SlothCreator provides a structure that lets you define activities or foods for your sloth to enjoy at specific times. Create the schedule, then provide a tuple of a Date and an , or you can use some of the standard care procedures:
let events: [(Date, CareSchedule.Event)] = [
(Date.now, .bedtime),
(Date(timeIntervalSinceNow: 12*60*60), .breakfast),
(Date(timeIntervalSinceNow: 13*60*60), .activity(Sightseeing()))
]
let schedule = CareSchedule(events: events)
superSloth.schedule = schedule