How would I make a custom entity file?

I always see heapsIO projects with a custom entity file. I am wondering how would I make my own? Like what are some logic I would put in it to make it work with any project? In the official samples by the creator of haxe he has a entity file they are all very different depending on the project but how can I make a basic template one that has the general functions needed for an entity file.

So in Heaps you don’t “need” any entity file, because it is an all-purpose engine (for all kinds of games) and just based on mere code.

Sometimes you don’t have an entity at all, e.g. for game jam entries (in a game with more “creative” content) or in an ECS maybe (or any data oriented programming).


This can be an approach:

class Entity {
    var sprite : h2d.Object;
    var x : Float;
    var y : Float;
    public function new(){
        // what happens upon creation of the entity
}
    public function update() {
        // what the entity is doing every frame or so
        // set sprite position to entity position here
    }
}

Note: You might also do class Entity extends h2d.Object but it creates a bit of a mess.

But probably you have

  • some sort of processing (update)
  • coordinates (var x, y or you can also use/rely on h2d.col.Point? idk)
  • a sprite (which likely is a h2d.Object in Heaps)

This is another approach by Deepnight

class Entity {
	// Graphical object
	public var sprite : YourSpriteClass;

	// Base coordinates
	public var cx : Int;
	public var cy : Int;
	public var xr : Float;
	public var yr : Float;

	// Resulting coordinates
	public var xx : Float;
	public var yy : Float;

	// Movements
	public var dx : Float;
	public var dy : Float;

	public function new() {
		//...
	}

	public function update() {
		//...
	}
}

…

Deepnight uses dx, dy for the movement. Now the logic added to update would be something like

this.xx += this.dx;
this.yy += this.dy;

The basic idea using an entity class is to allow other objects (classes) in the game extend (extends) that class and then they all can profit of the movement functionality already implemented in Entity. And other functionality all game objects may find useful.

1 Like

How do I load a sprite with Res when sprite is an object? It just says hxd.res.Image should be h2d.Object.

Usually, a regular sprite is based on some sort of image file. Loading such a file into the game must be done step by step, see here:

(more on resource management here ← )

Where is that located on the wiki because if there are more of those I would really like to look at them.

unfortunately the official web representation hasn’t been updated for a while
and won’t be at the moment. it seems there is no interest from official site to publish the wiki content so far.
so you just have to browse the wiki as is (via navigation on the right)