Skip to content
This repository was archived by the owner on Dec 6, 2022. It is now read-only.

Entity API

Ian Glen edited this page Aug 16, 2013 · 18 revisions

scripts/entity/core.js

This API is used to define objects that are fixed to the grid, such as buildings, topographical features, etc. Each entity type is a subclass of the Entity class.

You might want to check out Creating Isometric Graphics.

Entity object

Used to define a generic entity on the grid.

var entity = new Entity(name, image, point, grid, visible)

Methods

Entity.draw()

Draws the Entity on the grid if Entity.visible is true.

Entity.getWidth()

Returns: (float) width of entity's image

Entity.getHeight()

Returns: (float) height of entity's image

Properties

Entity.name (string)

A human readable name for the entity, e.g "Military Base".

Entity.image (Image object)

The image for the entity that will be drawn on the grid.

Entity.grid (Grid)

The grid on which the entity exists.

Entity.point (IsometricPoint)

The location of the entity on the grid.

Entity.visible (boolean)

Whether or not the entity is to be drawn on the grid.

Old stuff (ignore)

addEntity()

Displaying an entity in Dominion is actually a fairly easy task with minimal coding. For the most part, there are two parts to displaying an entity:

  • loading the image for the entity
  • adding the entity to the entities array and make it visible

Loading the entity's image only requires two lines of code. At the top of game.js:

/**
 * entity resources
 */
var factory = new Image();
factory.src = "./assets/entities/factory.png";

The first line creates an image object. Make sure to name it accordingly. The second line defines the path to the entity's image.

Once that's all said and done, it's time to move onto adding the entity to the entities array. Also in game.js:

/**
 * game variables
 */
$(document).ready(function()
{
	/*** other code ***/

	// entities
	addEntity(new Entity("factory", factory, new Point(4,5), true));

	/*** more other code ***/
});

In this example, we've simply using the addEntity() function to add a new Entity object to the entities array. The Entity object has four basic attributes:

  • name (string): Name of the entity. This is what you'll use to reference the entity on the screen

  • image (image object): Image object for this entity. This is the image object you created earlier. Multiple entities can use the same image object.

  • point (point object): Point object for this entity. Defines the location of the entity. Simply use new Point(x,y), replacing x and y with grid coordinates.

  • visible (boolean): True or false. Determines whether object should be drawn on canvas.

In the above example, addEntity() occurs in the Game Variables section of game.js. Please note that addEntity() can be placed anywhere in any file as long as it falls inside a document.ready(). This is to ensure that the entity's image has been loaded first.

Clone this wiki locally