Skip to content
Stefan edited this page Mar 4, 2018 · 13 revisions

After finishing the initial Hello World tutorial you are able to get some text rendered on screen but there are some more concepts inside Marte that are not (yet) well explained.

Entity

An Entity is everything in your game: the hero controlled by the player, some flashing information text, enemies... nearly everything. This choice was made with MarteEngine's main concept in mind: simplicity.

Entity class can (and should) be extended following the basic Java approach:

public class Player extends Entity {

}

If you create a new Java class and make it extend Entity, you have do to only one thing: define a constructor for it:

public class Player extends Entity {

	/**
	 * @param x, x coordinate on screen where Player starts
	 * @param y, y coordinate on screen where Player starts
	 */
	public Player(float x, float y) {
		super(x, y);
		// TODO Auto-generated constructor stub
	}
}

You can always define a different kind of constructor with additional parameters: MarteEngine doesn't force you, it just helps you!

Another example is to associate an Image to your player Entity so you can see something on screen at the desired coordinates:

public class Player extends Entity {

	/**
	 * @param x, x coordinate on screen where Player starts
	 * @param y, y coordinate on screen where Player starts
	 */
	public Player(float x, float y) {
		super(x, y);
		// load Image from disk and associate it as player image
		setGraphic(new Image("data/image/player.png"));
	}
	
}

Done! Your player image should automatically be rendered on the given coordinates without any complicated coding involved!

Of course MarteEngine's Entities offer much more builtin functionality (physics, collision detection and many other things). The following tutorials will explain these features that help you focus on your games and not on the complicated coding! That's already done for you.

World

World is a container for entities: imagine it as a level of a videogame. The game starts and the hero moves around in a level with obstacles, enemies, a sky and so on. The hero is just one Entity but many other entities populate your game world too: the World class permits that!

Again, creating a World is simple. Just extend MarteEngine's World class and build a basic constructor

public class Level extends World {

	/**
	 * @param id, unique identifier for World
	 * @param container, container for World
	 */
	public Level(int id, GameContainer container) {
		super(id, container);
		// TODO Auto-generated constructor stub
	}

}

You can override the init method of World to load one or more entities into your World:

@Override
public void init(GameContainer container, StateBasedGame game)
		throws SlickException {
	super.init(container, game);
	
	Player player = new Player(100,100);
	add(player,World.GAME);
}

Note: you can always remove one entity using this piece of code:

	ME.world.remove(entity);

This because MarteEngine store reference to current world as static variable on ME class and so you can use all world methods anywhere!

Render and update

Another key point to understand is that both Entity and World could update game logic and render something on screen. To do this, all of your classes that extend Entity or World can override two methods: render and update:

public class Player extends Entity {

	/**
	 * @param x, x coordinate on screen where Player starts
	 * @param y, y coordinate on screen where Player starts
	 */
	public Player(float x, float y) {
		super(x, y);
		// load Image from disk and associate it as player image
		setGraphic(new Image("data/image/player.png"));
	}
	
	@Override
	public void update(GameContainer container, int delta)
			throws SlickException {
		super.update(container, delta);
	}
	
	@Override
	public void render(GameContainer container, Graphics g)
			throws SlickException {
		super.render(container, g);
	}
	
}
public class Level extends World {

	/**
	 * @param id, unique identifier for World
	 * @param container, container for World
	 */
	public Level(int id, GameContainer container) {
		super(id, container);
	}

	
	@Override
	public void init(GameContainer container, StateBasedGame game)
			throws SlickException {
		super.init(container, game);
		
		Player player = new Player(100,100);
		add(player,World.GAME);
	}
	
	@Override
	public void update(GameContainer container, StateBasedGame game, int delta)
			throws SlickException {
		super.update(container, game, delta);
	}
	
	@Override
	public void render(GameContainer container, StateBasedGame game, Graphics g)
			throws SlickException {
		super.render(container, game, g);
	}
	
}

What does that mean in detail? Basically it means that you can render entities in any way YOU like (or again, let MarteEngine help you) or update game logic for particular entities (like player controlled one) in YOUR way! Feel free to experiment and read MarteEngine code to understand how MarteEngine can help you!

ResourceManager

ResourceManager is an utility class: you can use it or not: it just stores references to all of our media files (spritesheets, images, sounds, font, constants) in a resource.xml file. You can write it outside of your Java classes to configure your game the way you want! In your classes, you can use your media files by just referencing them using costants! For example let's write our Player class again using the ResourceManager to load the image:

public class Player extends Entity {

	/**
	 * @param x, x coordinate on screen where Player starts
	 * @param y, y coordinate on screen where Player starts
	 */
	public Player(float x, float y) {
		super(x, y);
		// load Image using resource.xml file using costant name
		Image img = ResourceManager.getImage("player");
		setGraphic(img);
	}

Simple enough or not? You must initialize ResourceManager when your game starts using:

ResourceManager.loadResources("data/resources.xml");

And your resource.xml look like:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE resources SYSTEM "http://gornova.github.io/MarteEngine/0.3/resourceManager.dtd">
<resources>
	<!-- basedir -->
	<basedir path="data" />
	<!-- sounds -->
	<!-- songs -->
	<!-- images -->
	<image key="player" file="player.png" />
	<!-- sheets -->
	<!-- fonts -->
</resources>

Syntax is (hopefully) clear! In resource.xml you define an image with key "player" where image is "player.png", so you can reference it later in your game using the "player" keyword.


In the test package you can find some examples how to use this concept to make great games!

You should now continue with the Keyboard and Mouse Input tutorial!


MarteEngine version 0.3

Clone this wiki locally