Pre-introducing: Carrot

Jario was never supposed to be a full-blown game, it’s merely a tech demo for Artemis, and something of a framework to go onto bigger and better platformers. I thought I should mention that one such game is now in the works. It’s still very early days, but it’s codenamed Carrot.

Like Jario, Carrot is a 2D platformer using the Slick game library and Artemis framework. Unlike Jario, I intend to build it up to be a good and proper game. I’ll keep you all posted and release some more details once we’ve actually figured them out ourselves!

Jario Update: Kinda Working!

I’ve just pushed an update to Jario with some pretty nice changes since I last mentioned it. As usual, you can check out the source if you’re interested or just Launch Java Web Start button!

I’ll be quick:

Features:

  • All the important items – coins, mushrooms, fire flowers and stars.
  • Goombas, Koopas and even empt shells, with the ‘proper’ graphics and animations.
  • Vastly improved input and collision handling.
  • Added a marginally more satisfying ending, though it still doesn’t do anything.
  • Fixed many, many bugs (but not the intermittent collision mesh one yet).

Architecture:

  • Cleaned up collision handling systems into a hierarchy of similar handlers for increasingly concrete entity types. I’ve found it to be very flexible and reusable so far, which is nice, since the logic of a game is little more than input and collisions.
  • Refactored image and animation spatials into a hierarchy of intermediate abstract classes, and added a SpatialComposer to group the different spatials of individual entity types and handle different states.
  • Inserted a hierarchy layer for spatial effect handling. Currently only processes changes in rendering alpha value, but does so quite nicely.
  • Played around with a TimerSystem to be used in conjunction with a Timer component, allowing systems to easily register callbacks with individual entities to be executed after a given delay. Makes it very easy to add temporary properties to entities. For instance, giving the player X seconds of invulnerability (hurt with >1 health, or press I) reduces to a single static function call. Still need to play with Artemis’ DelayedEntityProcessingSystem to see if that does a similar thing.

That’s all the significant bits so far. Still plenty more to go, though! Some of the above will need to change in order to support a complete level, which will be interesting. I also need some better game state transitions, though that’s nothing to do with Artemis. I’ll talk a little bit about some of the more interesting points soon, such as the collision handling and spatials, since those two are key to getting a game working smoothly.

Entity/Component Game Design That Works: The Artemis Framework

Artemis logo

In my previous post, Entity/Component Game Design: A Primer », I reviewed some of the main differences between traditional object-oriented game design and the emerging paradigm of entity/component game design. I deliberately avoided talking about Artemis, and focused instead on the weaknesses of the alternative approaches, because I believe Artemis is able to resolve many of those problems.

Here I want to talk about the Artemis framework for a while. I keep saying I’ll write about it, but I wanted to get a little more familiar with it first. Well, I’ve been playing with it pretty solidly for about three weeks now, and I feel like I’ve got enough experience to form some kind of an opinion. Although there’s a brief overview on it’s homepage, I want to present my own perspective of it, and talk about some of my reasons for falling in love with it.

Big thanks to the authors Arni Arent (appel) and Tiago Costa (Spiegel) for all their hard work and support!

The Artemis approach

The Artemis framework inherits the concept of Entities representing bags of Components, but it does away with the OO concept of encapsulating behaviour within the components. Instead, it adopts a data-driven design involving the separation of the data and logic of each component.

In essence, the Components serve as little more than bags of data. They have no update function, no internal logic, just getter and setter functions to expose their current values.

The logic is instead encapsulated in the Systems that accompany the entities and components. A ‘System’ is simply an object that reads and updates the data in any relevant components. You could say it’s simply the update function of the relevant components refactored into its own object.

I say “relevant components” because of how the systems do their processing. Instead of the game iterating imperatively through all of the entities and updating each in turn, the systems take more of a functional programming approach. Each system specifies a set of components that it’s interested in, and then in each frame it processes only those entities that contain all specified components.

Furthermore, each system exists to perform a specific role, updating all instances of the relevant components before passing control to the next system. You could say that the components are processed in cross-sections across all entities at once, rather than entities (and all of their child components) being processed in chunks.

Advantages

That’s more or less how Artemis goes about its business, but without having used it, it may not be immediately obvious why this is a Good Thing. Following are a few reasons why I believe it’s a Very Good Thing. Of course, you can only truly appreciate the details after working with it, so I’ll try to keep the discussion at a high level.

Communication

The magic of systems is that they have no reservations in accessing multiple types of component at a time. The systems compose the communication between related components so that the components themselves don’t need to. Systems typically process one entity of interest at a time, and for the period in which they’re processing, they can combine any of the components within each entity however they like, with no structural side-effects.

For example, a MovementSystem could process all entities with Position and Velocity components, and each frame it would update the values of each Position component given the associated Velocity component. A RenderSystem could process all entities with Position and RenderableMesh components, and it would draw each mesh to the screen given the values from the updated Position component. In neither case do the components require any knowledge of each other.

Reusability

Refactoring the logic this way essentially gives you two layers of components – data components (Components) and logic components (Systems) which read/write the data components. Since the Components are pure data, they should never need to change. A 3D Velocity vector will always have X, Y and Z components, and so will be perfectly reusable in any 3D scenario.

The logic in the systems will depend on the rules of the game world, and they can be interchanged as needed. Since each system is typically self-sufficient enough to be autonomous, changing the behaviour of a system will not necessitate a change in the behaviour of any other system.

Say you want/have to change your game from a 3D free-roaming game to a 3D side-scroller. For side-scrolling movement, all you need to do is plug in a side-scrolling MovementSystem (it could be exactly the same, but simply discard the X component of the velocity), and it will automatically apply to all entities with Position and Velocity components. No components or other systems need to change as a result of the change of MovementSystem, since no components or systems even know it exists. (You may then need to update the CameraSystem and ControlSystem as well in this example, but the same process applies.)

Not prescriptive

Because the entities are simple bags of components, and the components are simple bags of data, as a developer you really only need to concern yourself with crafting the systems. The systems can easily take care of composing the relevant components within each entity, and beyond that they have no restrictions in how they process things. This means you can focus on constructing effective systems to solve problems in creative ways.

For example, I have a single CollisionSystem which detects and records all collisions in a single pass, after which I have an OO hierarchy of collision handling classes down which collisions are passed to be handled appropriately. I have an asynchronous TimerSystem which, together with a Timer component and external handler class, lets me attach and listen for delayed events with registered callbacks on individual entities with a single static function call. This may not be the “right” way to do it (and there often is no “right” way), but that’s up to me as the developer. I have the freedom to experiment, and to choose what works best, without sacrificing any part of the framework.

Convenience

All Artemis processing occurs within a managed World object. For convenience, the world object contains four managers that make your job almost too easy:

  • EntityManager manages the creation and removal of entities and their components, and allows components to be retrieved from entities by type.
  • GroupManager allows entities to be assigned to unique groups (string IDs, such as “ENEMIES”) and retrieved as a group at any time in any system.
  • TagManager allows individual entities to be assigned uniquely identifying tags (again, string IDs, such as “PLAYER”) and retrieved individually, similarly to groups.
  • SystemManager stores all of your systems, allowing them to be retrieved individually by type if necessary (for instance, the RenderSystem may need to communicate with the CameraSystem).

Performance

The Artemis library itself is very lightweight (for some sense of comparison, Artemis is 30KB compared to the ~1.5MB of Slick + LWJGL), and the internals are built for performance at larger scales. I haven’t yet reached the point where performance becomes and issue, so I can’t comment on quite how far that is, but that alone is an endorsement. Author testing has managed to create/remove a few thousand rendered entities per second, a number which goes up to millions without rendering.

On top of that, the functional programming approach to component processing has inherent potential for performance gains, if taken advantage of. I haven’t read too much into this yet as it hasn’t been necessary, but I’ll talk about it at a later date.

Conclusion

Despite appearances, this is just a static outline of how the Artemis framework fits together and some of the things it does well by itself. I’m not going to dive into all the different things Artemis allows you to do; I’ll try to cover some of those in separate posts in the near future.

I will however say that when using Artemis, the development process changes radically (for the better). You can tackle the implementation of one aspect of your game at a time in isolation. You have a clear implementation path to follow. As your library of components grows, adding new entity types to the game is as simple as creating a new Entity object and adding a different set of Components (or just modifying parameters), and they are automatically handled by the relevant Systems that you already have in place. At that point, it’s almost trivial to read in all your entities from a text file and be truly data-driven.

It’s a pleasure to work with, and every day I’m thinking of new ways to do things that make it even easier. If this article piqued any curiosity or excitement in your inquisitive mind, then I strongly encourage you to investigate using Artemis with Slick. Start by downloading the demo game, StarWarrior, and browsing through the source code. Then try making some small changes to transform it into a different game; perhaps something like Artemoids? Where you take it next is up to you and your imagination.

Just remember to keep an open mind, and have fun! I’m no expert, but I’m more than happy to give whatever advice I can, and the Slick forums are always friendly.

Resources

  • Artemis Entity System Framework – The official homepage of the Artemis project. It’s quite new (about a month old), and rather small as I mentioned. From the homepage you can get the compiled JAR, source (JAR, SVN, Git), JavaDoc, and the demo StarWarrior game (inspired by Space Invaders). There’s also an unofficial C# port going on.
  • Artemis thread in the Slick forums – Artemis was originally developed for Slick, and so that’s where most of the discussion is so far. I should mention however that it’s not dependent on Slick, and appel is already using it with libgdx.
    • For those of you interested in what I’ve been discussing, I joined in on page 12.
  • Entity Systems are the future of MMOG development (T=Machine) – A very thorough application to MMOs, and a primary inspiration for Artemis.
  • Anything on this site with the “artemis” tag – The count of this content will be increasing consistently in the future, because Artemis is a lot of fun to talk about.

Introducing: Jario

Jario logoYou saw Hario, and you revelled in its technical prowess. Now, the summer (winter) blockbuster of 2011 has arrived.

Jario is a port of Hario, and is similarly a very basic Mario clone, the quintessential platformer. From Hario’s C++ and HGE, Jario is running with Java and the Slick game library, as well as the brilliant Artemis entity-component framework, and it is an exploration of how to apply such a framework to a platformer-style game.

It’s currently in its very early stages after about a week of development from scratch, but it runs, and it already has more features than Hario. And although you may not believe it when you see it, it has superior graphics as well. There is no art more noble than that of a programmer.

It’s important to note that Jario is not a game so much as a proof of technology for the foundation of a game. As features are implemented it will become more game-like, and if I ever ‘finish’ it then it will perhaps be a game, but in my eyes it is just a body of code. The good news is that I can now refer to this code as I discuss Artemis and entity-component frameworks in general, which I certainly will get around to now. Jario is just the first step on the path to bigger and better things.

As usual, you can download the Web Start or run it in your browser. In addition, since it’s basically a piece of tech, I figured I should release the source code on Bitbucket to get some feedback from those with a little more Artemis experience. Finally, for this release there’s also a bonus feature: a pretty button for launching the Web Start! Behold:

Launch Java Web Start button

NOTE: Google Chrome users (such as myself) may find this button just downloading a JNLP file rather than launching anything tangible. To resolve this, click the little down arrow next to the downloaded file on the downloads shelf (the toolbar across the bottom) and select “Always open files of this type”. This will tell Chrome to launch JNLPs automatically (as well as downloading) rather than making the user do it, just like the other browsers.

Retro-introducing: Shadow Quest

Shadow Quest iconA couple of years ago, I took a subject at uni called Object-Oriented Software Development (433-294). Although I had already introduced myself to OOP, this was the best subject I’d ever taken, because the OOP paradigm was my favourite thing about programming, and it’s always worthwhile learning important things formally. On top of this, we had a fantastic lecturer (Shanika Karunasekera) with a bold plan to inject some life into the otherwise tiresome semester project – instead of building a generic banking application or similar, we would be writing a game.

This was, of course, right up my alley, and ultimately the result was Shadow Quest. You can read more about it on the project page, but essentially we were given a set of specific requirements to build an RPG using the Slick game library, and that would be our assessment (though various stages of planning and development). Brilliant. Everyone loved it.

Of course some students found it less challenging than others, and for them the challenge was set to create the best game extension by the end of the semester. So long as the basic requirements were fulfilled, we could add anything we wanted, and the author of the best game would win a book voucher.

I leapt at this opportunity, and set about implementing a bunch of features from Diablo II, my favourite RPG (despite it being a hack’n'slash). I added player experience, levelling, stats, inventory and equipment, magic items, random item drops, gold drops, buying potions, and the thing I was most proud of was my randomly generated dungeon: five levels of caves procedurally generated by a cellular automaton. It took a lot of work, but worked a treat, and I felt like I was in with a chance for best game.

Unfortunately, come judgement day, there was some very impressive competition, including one game which blew all the competition away in terms of features and polish. A giant map, animations, weather, visual effects, magic spells, archery, horse-riding, a town with indoors in the buildings, a bunch of NPCs, and so on. It was the deserving winner, but I still reckon it was the work of more than one person.

Anyway, I dug out the code for my version of Shadow Quest the other day, and decided to post it up here for posterity, with a few tweaks for efficiency. Again, you can check out the project page, the source code on Bitbucket (don’t judge me, it’s old!), or more importantly play it in your browser or download the Web Start. Enjoy!

Why Use Slick?

Slick logoI talk about the Slick 2D game library a lot because I use it a lot, and with good reason. I thought I’d take a minute to go over some of that reason and give a little overview of what Slick is.

Slick is, like I said, a 2D game library written in Java. A game library is essentially a framework which provides a big chunk of functionality that is useful or necessary for creating a game. This primarily involves rendering the visual aspect of the game, but also includes some conveniences such as implementing the game loop and providing simple methods of changing the game parameters (such as the window resolution and FPS limit).

Slick is written in Java, is open source, and uses the Lightweight Java Game Library (LWJGL) for rendering. Many (if not all) of the ‘serious’ commercial game libraries and engines are written in C++, mainly for performance reasons, but Java has its advantages:

  • All the code runs in the Java Virtual Machine (JVM) which is implemented on most operating systems, meaning a game written in Java will be playable on Windows, Mac, Linux and other systems, with no extra effort from the developer.
  • There are a couple of low-effort, high-convenience deployment methods for Java programs. They can be automatically downloaded and run locally using the Java network language protocol (JNLP), or run in a browser applet embedded in a Web page. Both methods ensure the end user always has the latest version.
  • Java is a much more developer-friendly language than C++, which counts for a lot on a project as large and diverse as a game.
  • The performance typically isn’t quite as good as C++, but it’s more than good enough more often than not, especially for 2D games.

It should also be noted that there are an increasing number of handy C# game libraries, comparable to Java but with DirectX support at the cost of being cross-platform (or at least cross-non-Microsoft-platform).

But how does Slick compare to other Java game libraries? The most comparable alternatives in the 2D arena include GTGE (last updated in 2005), JGame (still active, and ported to Flash), and PulpCore (primarily for applets and animation). There are some 3D options too, such as libgdx and the Java Monkey Engine (JME), which can of course render 2D graphics, with a little more arm-twisting. The main advantage of Slick however is that it’s very easy to use, and remains consistently so thanks to its exclusive focus on 2D graphics.

It also provides a lot of practical features out of the box, best expressed in bullet point form:

  • Graphical flexibility for drawing images, primitives, and fonts using OpenGL.
  • Integrated sound and music playback with OpenAL.
  • Integrated and very easy input handling for keyboard, mouse and controllers.
  • Handy functionality built into the library, such as game states and path finding support.
  • Additional libraries to provide further features, such as powerful tilemap support and particle editing.
  • Helpful community, continued development and extension, and plenty of documentation and tutorials.
  • Total library download size of under 2MB, including LWJGL.
  • Open source and totally free (BSD license)!
  • A build in development for Android support, further extending the reach of your code.

Although it may not offer some of the more advanced features such as 3D graphics and physics, it’s a good (perhaps the best) choice for those games that don’t need them. Honestly, the main reason I started using Slick was because it was prescribed in one of my university subjects, but there was good reason for that too. I’ve yet to find a more suitable framework for painless development of 2D Java games.

As an added bonus, it’s the target library of the Artemis framework which I’ve fallen in love with recently, and which I will write more about very soon.

Introducing: Artemoids

As I mentioned previously, I’m back to work, and my first order of business is playing around with Slick and the Artemis framework. To that end, after a mere day or so of coding, I’m pleased to present Artemoids, a simple Asteroids clone using the Artemis framework. I’ll talk more about the framework soon, but suffice it to say that it is incredible, and exactly what I’ve been looking for on and off over the past couple of years.

So. Artemoids is a simple, top-down 2D game in which you fly a ship around in space (zero gravity) and shoot asteroids. If an asteroid hits your ship you die, and when you shoot an asteroid it splits into two smaller asteroids. That’s pretty much it. There’s really no win/lose conditions, because I don’t intend to make a full game out of it; it’s primarily a test and demo of the Artemis framework, and something easy to cut my teeth on to kick off the holidays.

That said, I am planning a much more sophisticated game with quite a few similarities to Asteroids, so the Artemoids code should serve as a good foundation when I kick that off (and hopefully that will be soon, with an accompanying announcement).

You can try it out for yourself right now in a browser applet, or download a JNLP Java Web Start file to run it a little more locally. I’m quite excited about finally getting both of these deployment methods to work happily, as it means I’ll be significantly more able to show off any future Java projects I create.

Also, notably, this is the first project I’ve ever open-sourced, not because I’ve been a selfish git all this time (well, not only because of that) but because my projects have typically been pretty small-scale and hacked together for personal use. Hopefully however Artemoids (and of the some games that follow) will help to serve as an example for how to use the fledgling Artemis framework, and I’ll be referring to some of my code in future posts/articles. You can check out my Artemoids Bitbucket repository to read the source, submit any issues (bugs/feature requests) you find, or even fork the code for your own project. I’m a big fan of FLOSS, and I’m more than happy to lend a hand to anyone with any interest in what I’m doing.