August 14, 2014

Engine Enhancements and Refactoring

I have been developing a Breakout clone to test out the Swift engine. Most of it has worked as expected, and the parts that have not have been refined to do exactly what I wanted. In addition to a few changes in the logic of a couple of the core systems, the public interface for the engine has changed to better reflect the ECS paradigm.

The most significant change is to the way a game developer should create a scene. Previously, the starting point for making a game was subclassing the base scene. The scene class is no longer subclassable. Instead, the preferred method is instantiating the base scene in the view controller and attaching systems to it.

The reason behind this change is that I found myself including game logic in the scene subclass that did not belong there. It was too easy to break the ECS paradigm in favor of convenience. Ultimately, that doesn’t matter as long as it’s still easy to make a game, but it would eventually present problems for more complex games when a developer finds himself putting too much logic in the scene and not making use of the modularity of systems.

The other big change is that, although a developer can add entities to a scene from the view controller, I have decided that the preferred method of adding entities is from inside a system. I made an initialization method that is called after all systems are added to the scene, where a system can create its entities. This moves all of the logic into systems, which reduces the scene to only a collection of systems. This leaves no ambiguity as to where the game code should go. It should all be in systems.

To minimize boilerplate code, I also created a Game class that is a view controller subclassed with the basics implemented (like creating a SKView). Here’s what the app delegate looks like in my Breakout clone:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool
{
	let viewController = Breakout()
	
	window = UIWindow(frame: UIScreen.mainScreen().bounds)
	window.rootViewController = viewController
	window.makeKeyAndVisible()
	
	return true
}

Basically, all it does is instantiate the Breakout subclass of the LGGame class. Here’s the entire Breakout class:

class Breakout: LGGame
{
	override func addSystems(scene: LGScene)
	{
		scene.addSystems(
			LGPhysicsSystem(gravity: LGVector(x: 0, y: 0)),
			LGRenderingSystem(),
			BrickSystem(rows: 4, cols: 8),
			BallSystem(),
			PaddleSystem(),
			DamageSystem(),
			GameManagerSystem()
		)
	}
}

That’s 15 lines of code to create a game. To see the entire source, check out the repository for my Breakout clone and the repository for the Swift engine. Both of these projects are under the MIT License, so you are free to use, modify, and distribute the source.

Posted by Luke Godfrey

Luke Godfrey is a graduate student at the University of Arkansas, pursuing a PhD in Computer Science. He has created applications on a number of platforms, and is especially interested in mobile and web development.

View more posts from this author

Leave a Reply

Your email address will not be published. Required fields are marked *