Protocol Oriented Programming, (a.k.a. “POP”) is all the rage these days. I’ve found myself using it in all sorts of places. One of them is my own video game project – Grandfather. This is a quick breakdown of a few areas I’ve found Protocol Oriented Programming for Games to be particularly useful.

A game character carrying a key, all made possible through the magic of Protocol Oriented Programming for Games.

Put Common Animations in a Protocol Extension

When Mega Man gets hit, he flashes. When enemies get hit, they flash. As developers, we know it’s a bad idea to force all game characters to inherit this flashing animation from a single superclass. That is why we usually take a more compositional approach when designing game entities. So why not create a protocol for it?

The Flashable protocol adds a simple flashing animation to any SKSpriteNode. To give a game character a flashing animation, just make it adopt the protocol. Now when you call megaMan.flashing(), he’ll flash. When you call megaMan.endFlashing(), he’ll stop flashing. Any sprite in your game can do this!

Use Protocol Oriented Programming for Games To Make Anything Carryable

A lot of games have items. Game characters can pick up and drop items. So why not break that logic off into a protocol as well? Now anything in the game that adopts the protocol can either be a carried item or a carrier of items. Here’s one way you might do that using protocols and protocol extensions.

Let’s step through some of this logic. Carrying items typically implies some kind of ownership. Anything that can be carried is ususally carried by one sprite at a time. Carryable things also switch their category bitmasks when they are carried. In my case, I have a default implementation wherein a carried thing only collides with the floor. However, this value can be overridden by anything that implements the Carryable protocol. A key could only open locked doors when it is carried, for example.

There’s also a protocol for carrier. Carriers carry Carryable things. Makes sense, right? What I really like about this approach is the way different carriers can implement different ways of dropping and holding onto items. The holdOntoItem() function needs to be called during your carrier’s update() loop function. dropItem() can be called whenever you want your character to drop an item. In this default implementation, the item will get dropped in the direction your character is facing. Neat eh?

Make Any Scene A Side-Scroller

Ever since Apple added SKCameraNode to SpriteKit, it has gotten quite a bit easier to manage camera controls. Thank goodness we no longer need to keep track of a world node that moves as the player moves! It was super confusing. Now with protocol extensions, we can abstract some of our camera logic out of the scene. This lets us reuse our camera logic on any scene that needs to be a side scroller. Here’s how I did it on my game prototype.

There are a few ways you can make a scene a side scroller. I chose to use a camera that follows the player. The camera remains fixed on the y-axis but dynamic on the x-axis. I’ve also added two fixed cameras to the scene. When the player hits the edge of the scene on either side, we switch from the camera that follows the player to one of the fixed cameras. The transition is buttery smooth. The player never knows the difference.

With this protocol extension, you need only hook up the three cameras and the two stop points (the places in the x-direction where once the player crosses them, we switch to a fixed camera). Then simply call the updateCameraForSideScroller() in your update loop, and you’ve got a side scrolling level.

Stay Tuned to Learn More Protocol Oriented Programming for Games!

I have done a ton of refactoring in my game, using all the new stuff that’s available. I have found plenty of other uses for protocol compositions, and I’ll probably write another installment soon. This feels like the right way to build games. With Protocol Oriented Programming for Games, you get flexible pieces you can add or take away from your game elements. It lets you craft your masterpiece in a much more modular and compositional style.