Halving and Doubling protocol

In one of my many previous posts on screen sizes I mentioned that I have decided to use points and asset catalogs with scaled assets for AdventureKit.

So far my test game has been developed with the iPad in mind using iPad-sized assets and everything laid out in iPad coordinate space.

As the assets for the iPhone are now set to be half the size of those on the iPad, I need a way to halve any spatial game data.

This is so that everything remains in the same position and moves the same distance on the iPhone despite everything being half the size.

The Halving and Doubling Protocols

In order to do this, I have created a Halving protocol which any of my game data objects can conform to.

It’s a simple protocol that has an associated type and an initialisation method that takes an instance of the same object.

The game data objects declare themselves to be the associated type.

Their implementation of the initialisation method then goes through each existing property and any that are size or space related are halved.

The initialisation then ends having created a new instance of the same object with this halved data.

I have created a Playground that goes through the Halving protocol in detail, explaining how it works and why I chose to do it this way.

The Doubling protocol works in much the same way, except it doubles all the values instead.

You can find and download the Playground on Github.