Creating a Framework for a Reusable HTML5 Engine

Although the game built in Chapters 1 through 3 is perfectly acceptable for a one-off game, the code is fairly brittle and specific to the game itself. In the next few chapters, you put together a more general engine that allows for better code reuse from game to game.

This engine is going to be called Quintus. Quintus means “fifth” in Latin (which seemed appropriate) and will be used for the rest of the games built in this book. As mentioned in Chapter 6, “Being a Good Mobile Citizen,” the engine has two dependencies: jQuery and Underscore.js. You’ve seen that you can build a game in HTML5 without any dependencies; doing so, however, means that this book would contain more nongame-related code and spend less time on actual game development.

When building a game in JavaScript, you don’t need to reinvent the wheel for traditional patterns used in game development. Although JavaScript has its quirks as a language, it’s extremely malleable and can be molded to fit most programming styles you like. This doesn’t mean that you can build a performant game any way you want; certain styles of development lend themselves well to JavaScript’s asynchronous, single-threaded nature better than others.

Designing the Basic Engine API

The primary guts of the engine are housed in a single file called quintus.js. To make the engine do anything useful, additional modules need to be pulled in for functions such as rendering and input. The Quintus engine ...

Get Professional HTML5 Mobile Game Development now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.