-
Notifications
You must be signed in to change notification settings - Fork 4
Entity API
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 Entities and Creating Isometric Graphics.
Used to define a generic entity on the grid.
var entity = new Entity(name, image, point, grid, visible)Draws the Entity on the grid if Entity.visible is true.
Returns: (float) width of entity's image
Returns: (float) height of entity's image
A human readable name for the entity, e.g "Military Base".
The image for the entity that will be drawn on the grid.
The grid on which the entity exists.
The location of the entity on the grid.
Whether or not the entity is to be drawn on the grid.
Methods that only exist in the class and are not added to new objects.
Runs Entity.draw() for each visible entity in the Entity.entities array.
Removes an entity with given name from Entity.entities array.
Returns: (Entity) entity with given name
Returns: (Entity) entity that exists at given point
Properties that only exist in the class and are not added to new objects.
An array of all the entities that have been created.
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 usenew Point(x,y), replacingxandywith 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.