How to instantiate artemis-odb entities with components

Some time ago I played around with the Artemis framework. Artemis is an ECS, short for Entity-Component System. It’s a system in which you favor composition over inheritance. The components are data containers tied to a very light-weight entity and the game logic goes into the systems. That’s a very brief overview of what ECS is and does. Artemis is a well known one for Java and it was easy to use. Now coming back to it after a few years, things have changed. There’s a few spin-offs, among which artemis-odb.

Artemis-odb seemed the most interesting and I dove right in. And I instantiated entities and set up systems without issue. But what I couldn’t find was how to attach components to an entity without being in a system. The documentation is rather unclear about this and kept telling me to put component creation in a system, while I didn’t want to do that. Turns out it’s very easy to do, just not documented very well:

public class Health extends Component {
public int current;
public int maximum;
}

You just use world.getComponentMapper(Class) to retrieve the mapper:

final int playerId = world.create();
final ComponentMapper<Health> healthComponentMapper = world.getMapper(Health.class);
final Health health = healthComponentMapper.create(playerId);
health.maximum = HEALTH;
health.current = HEALTH;

It’s that easy!

Getting IntelliJ to play nice with LibGDX Gradle builds

After running the gdx-setup.jar and importing the Gradle project into IntelliJ IDEA 14.0.2, it wouldn’t (or better put: couldn’t) find the installed Android SDK’s for the Android module. The simple fix for this is to change the version of a classpath dependency in the build.gradle.

Open the build.gradle of the root folder and look for this line:

buildscript {

repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}

You’ll need to change the version to 0.14.0 (0.14.2 also seems to work for me) to get IDEA to work.

LibGDX’s Scene2D and TiledMap

Posting a short tutorial because I had some trouble finding the right resources for this and I hope that my post will help others should they come across the same difficulties.

I wanted to use LibGDX’s Scene2D API with a TiledMap. Before I used Tiled I just had a simple image as a “map” and that worked to figure out how to move the camera and basic collision detection (based on other actors in the stage), but it is rather lengthy  and a hassle to maintain/extend.

How to use Scene2D with TiledMap

tiledMap = new TmxMapLoader().load("maps/island.tmx");
mapRenderer = new OrthogonalTiledMapRenderer(tiledMap, 1);

First we load the TiledMap into our program and instantiate our renderer.

mapRenderer.setView((OrthographicCamera) stage.getCamera());

We then bind set the view of the TiledMap to the stage’s camera. This is one thing I forgot and I saw nothing (aside from my debugging bounding boxes).

mapRenderer.render();
stage.act();
stage.draw();

To conclude with drawing the map and the stage. The order is important, unless you want to only see your map…

Of course, this is only rendering, so let me move on to:

Collision Detection

Before I added a TiledMap, my collision detection was just my game looping through the actors and if it had a bounding box overlap, it would detect a collision. Combining this method with the use of an objectlayer in Tiled which has rectangles defining bounding boxes for solid things resulted in the following code:

public boolean collision(Actor movingObject) {
Rectangle rectangleMovingObject = new Rectangle(movingObject.getX(), movingObject.getY(), movingObject.getWidth(), movingObject.getHeight());
for (Actor actor : world.getChildren()) {
if (actor == movingObject) {
continue;
}
if (actor instanceof Image) {
continue;
}
Rectangle rectangle = new Rectangle(actor.getX(), actor.getY(), actor.getWidth(), actor.getHeight());
if (Intersector.overlaps(rectangle, rectangleMovingObject)) {
return true;
}
}

MapObjects layerObjects = layer.getObjects();
for (RectangleMapObject rectangleObject : layerObjects.getByType(RectangleMapObject.class)) {

Rectangle rectangle = rectangleObject.getRectangle();
if (Intersector.overlaps(rectangle, new Rectangle(movingObject.getX(), movingObject.getY(),16,16))) {
Log.d(rectangle + " " + movingObject.getX() + " " + movingObject.getY());
return true;
}
}

return false;
}

As you can see, this code constantly asks the layer for its objects. I’m leaving it in this quick and dirty way because my game only has one map (and one other actor) as of yet, but you should clean that code up if you plan on using it.

Conclusion

As said, a simple tutorial. Probably a bit unnecessary, but I wanted to see it spelled out this clearly so that I won’t waste another hour or three on this.

Resources used

  • libgdx version 0.9.9
  • Tiled version 0.9.1

My Milked Week

Ludum Dare

It’s been some time since my last post. To be honest, I was kind of disappointed after my Ludum Dare Brave Warrior entry. I got nothing but good critiques and feedback, but the score didn’t reflect that. Not that I care about getting a high score, but the lack of negative feedback left a bad taste in my mouth.

I’ll join the next LD, albeit under some extra time constraint: I have a concert to play with my double bass class. Perhaps I’ll join the Jam, I don’t know yet.

Running projects

I have a few things going on, but the two big projects I’m working on right now are

– Brave Warrior- Network Game

I started Brave Warrior as a remake of my last ludum dare entry, but I got sidetracked a lot and it’s turning into this whole other beast. I guess what I wanted yarpa to be. Right now I’ve turned it into an RPG with unspecified mechanics. I’ll publish a first test somewhere this week or next week. More information will follow in its own post.

The network game is an idea I had a couple of months ago. I won’t elaborate on it, but it’ll be awesome.

Play Store

I also made an account in the Google Play Store and I’ve pushed my first alpha build onto Google Play.

LD26 Post Mortem and #1GAM

Short LD26 Post Mortem

The results are in for Ludum Dare 26. I’m not as satisfied with the results. The scores are average, which is ok in itself. But I’m somewhat disappointed that there weren’t any negative or “average” feedback comments. There were people out there who didn’t enjoy my game as much and they didn’t share why or what was wrong with my game.

I’m not going to rant, but I put in effort to give feedback (positive or negative) to (most of) the games I played. I expected the same in return. Not going to rant.

Anyways, onward;

#1GAM

There’s an initiative I wanted to join some while ago: One Game A Month. The goal is simple, make a game each month and share it. So, starting from July, I’m going to finish a game each month. For my first month, I’ll take it easy and make a simple puzzle game. Nothing to serious or new. Just some tried and true mechanics to get me started.

Later,
Eek.