Wednesday, September 19, 2012

Artemis and niftyGui


Artemis is a good Entity System Framework library that helps coding your games. Core concept here is "Entity", that is only a container for Components, that hold data. Adding Systems to the game, you can draw stuff on screen, move it and so on.
More info on Artemis web site!


Nifty gui solves an old problem for Java game programmers: gui. You can define in an xml file your gui, or from Java code: the good thing here is you have decoupled from your game code gui management and this is really good.
For information see Nifty gui website!

My experiment

In my experiment I've tried both Artemis and nifty-gui and results are good! My idea was to replicate my old You can't win (I've wrote this game for Axiom contest, build a entity component system for it, so is not my first time on this topic) with Artemis.

lone shooter against horde of enemies!


Writing a game with Artemis is simple, once you have understood the basic concept of entities and in particular of systems. You can write complex games and not force you in a rendering system, because you write rendering system!
This works perfectly with Nifty-Gui: just wrote a simple user interface (see screenshoot) and place into my code: here we are!

Piece of code

For example here a RenderingSystem of my experiment:


package it.artemis.hello.system;

import it.artemis.hello.component.Transform;
import it.artemis.hello.spatial.SpatialForm;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;

import com.artemis.Aspect;
import com.artemis.ComponentMapper;
import com.artemis.Entity;
import com.artemis.systems.EntityProcessingSystem;

public class RenderSystem extends EntityProcessingSystem {
 private Graphics g;
 private ComponentMapper renderMapper;
 private ComponentMapper trasformMapper;
 private boolean debug;

 @SuppressWarnings("unchecked")
 public RenderSystem(Graphics g, boolean debug) {
  super(Aspect.getAspectFor(SpatialForm.class, Transform.class));
  this.g = g;
  this.debug = debug;
 }

 @Override
 public void initialize() {
  renderMapper = world.getMapper(SpatialForm.class);
  trasformMapper = world.getMapper(Transform.class);
 }

 @Override
 protected void process(Entity e) {
  Transform transform = trasformMapper.get(e);
  SpatialForm spatial = renderMapper.get(e);
  spatial.getSpatial().draw(transform.getX(), transform.getY());
  if (debug) {
   g.setColor(Color.green);
   g.drawRect(transform.getX(), transform.getY(), spatial.getWidth(),
     spatial.getHeight());
   g.setColor(Color.white);
  }
 }
}

Nice, right? In this example I'm using SpatialForm (that hold entity image) and Transform (position, speed, rotation and so on) aspects and I'm using it in drawing of entities!

Conclusions

Experiment and investigations continues, but Artemis (in active development!) is a valid choice for any game developer and my suggestion is to have a decent gui written with nifty!

You can download source code from here!

Play on windows 32 bit here, move with WASD and fire with left mouse button

No comments:

Post a Comment