Defold Grid Engine provides grid-based movement, interactions, and utility features to a Defold game engine project.
Please click the ☆ button on GitHub if this repository is useful or interesting. Thank you!
Add the latest version to your project's dependencies:
https://github.com/whiteboxdev/library-defold-grid-engine/archive/master.zip
Import the dgrid Lua module into your character's script:
local dgrid = require "dgrid.dgrid"
The grid system itself must be initialized before registering any characters:
local grid_box_size = 16
local collision_map = {
{ 2, 2, 2, 2, 2 },
{ 2, 1, 1, 1, 2 },
{ 2, 1, 1, 1, 2 },
{ 2, 1, 1, 1, 2 },
{ 2, 2, 2, 2, 2 }
}
local property_map = {
["31"] = { bonus_points = 1000 },
["55"] = { bonus_points = 2000 }
}
function init(self)
dgrid.set_stride(grid_box_size)
dgrid.set_collision_map(collision_map)
dgrid.set_property_map(property_map)
end
The dgrid.set_stride()
function sets the size of each grid box. In the example above, each grid box is set to 16 pixels.
The dgrid.set_collision_map()
function assigns a collision map to the grid. Collision maps consist of a two-dimensional array of integers, each of which corresponds to a collision tag. All tags can be found in the dgrid.tag
table. Custom tags may be inserted into the dgrid.tag
table if you wish to detect additional collision cases. dgrid will post a dgrid.msg.collide_passable
or dgrid.msg.collide_impassable
message to your character's on_message()
function when your character collides with any grid box. If you did not specify a collision map, then dgrid.msg.collide_none
will be posted instead.
The dgrid.set_property_map()
function assigns a property map to the grid. Property maps consist of a table of custom data. The purpose of this map is to assign semantics to your grid boxes. Keys correspond to the x and y coordinates of the targetted grid box, while values correspond to the custom data. When a character collides with a grid box that contains custom data, the data is attached to the dgrid.msg.collide
messages as the message.property
field. In the example above, colliding with the grid box at coordinates <3, 1> will send a message.property
value of { bonus_points = 1000 }
.
If the bottom-left of your tilemap is not located at the origin of the game world, you should call the dgrid.set_map_offset()
function, which allows you to shift your collision and property maps to match up with the world position of your tilemap.
Configuration is complete. Next step is to register your characters:
local config = {
size = vmath.vector3(16, 32, 0),
direction = dgrid.direction.down,
speed = 3
}
function init(self)
self.dgrid = dgrid.register(config)
end
size
: Size of your character in pixels.direction
: Initial direction in which your character is looking.speed
: Movement speed in grid boxes per second. Ifspeed = 0
, then movement is instant.
dgrid snaps your character into a grid box on registration. To do this, the bottom-center stride x stride
square region of your character is used to properly position it onto the grid.
Finally, make sure to call self.dgrid.update()
and self.dgrid.unregister()
in your character's script:
function update(self, dt)
self.dgrid.update(dt)
end
function final(self)
self.dgrid.unregister()
end
Table for referencing character orientation:
dgrid.direction = {
up = { value = 1, string = "up", offset = vmath.vector3(0, 1, 0) },
left = { value = 2, string = "left", offset = vmath.vector3(-1, 0, 0) },
down = { value = 3, string = "down", offset = vmath.vector3(0, -1, 0) },
right = { value = 4, string = "right", offset = vmath.vector3(1, 0, 0) }
}
value
: Identification value of this direction.string
: Name of this direction.offset
: Coordinate offset of this direction.
Table for referencing messages posted to your character's on_message()
function:
dgrid.msg = {
move_start = hash("move_start"),
move_end = hash("move_end"),
move_repeat = hash("move_repeat"),
collide_none = hash("collide_none"),
collide_passable = hash("collide_passable"),
collide_impassable = hash("collide_impassable")
}
move_start
: Posted when your character starts moving from rest.move_end
: Posted when your character stops moving.move_repeat
: Posted when your character continues moving between grid boxes without stopping.collide_none
: Posted when your character collides with any grid box which lies outside of the supplied collision map. Themessage.property
field contains the user-defined data at this grid position.collide_passable
: Posted when your character collides with any passable grid box. Themessage.name
field contains the tag's hashedname
string. Themessage.property
field contains the user-defined data at this grid position.collide_impassable
: Posted when your character collides with any impassable grid box. Themessage.name
field contains the tag's hashedname
string. Themessage.property
field contains the user-defined data at this grid position.
Table for referencing collision tags. Each key (index of tag) corresponds to an integer used in your collision map. Custom tags may be inserted if you wish to detect additional collision cases.
dgrid.tag = {
{ name = hash("passable"), passable = true },
{ name = hash("impassable"), passable = false }
}
name
: Hashed name of this tag.passable
:bool
indicating whether characters may pass through grid boxes assigned to this tag.
Gets the size of each grid box in pixels.
Returns a number.
Gets the collision map.
Returns a table of lists of integers in the following format:
{
{ <tag>, ... },
...
}
Gets the property map.
Returns a table of custom data. Keys correspond to the x and y coordinates of the targetted grid box, while values correspond to the custom data:
{
["xy"] = { ... },
...
}
Gets the collision and property map offset.
Returns a vector3
.
Gets tag information.
name
: Hashed name of a tag.
Returns a table in the following format:
{
key = <tag>,
value = { name = hash("<tag_name>"), passable = <bool> }
}
Converts grid coordinates to pixel coordinates. The returned pixel coordinates point to the center of the grid box.
grid_position
:vector3
denoting the grid box to convert. Thez
component remains unchanged.
Returns a vector3
.
Converts pixel coordinates to grid coordinates.
pixel_position
:vector3
denoting the pixel to convert. Thez
component remains unchanged.
Returns a vector3
.
Converts grid coordinates to map coordinates. Map coordinates take into account the map_offset
.
grid_position
:vector3
denoting the grid box to convert. Thez
component remains unchanged.
Returns a vector3
.
Sets the size of each grid box in pixels.
stride
: Size of each grid box in pixels.
Sets the collision map.
collision_map
: Table of lists of integers in the following format:
{
{ <tag>, ... },
...
}
Sets the property map.
property_map
: Table of custom data. Keys correspond to the x and y coordinates of the targetted grid box, while values correspond to the custom data:
{
["xy"] = { ... },
...
}
Sets the collision and property map offset. If the bottom-left of your tilemap is not loaded at the origin of the game world, then this function will allow you to shift your collision property maps to match up with the world position of your tilemap.
offset
:vector3
denoting number of grid boxes to shift.
Sets an existing tag's passable
flag.
name
: Hashed name of tag.passable
:bool
indicating whether characters may pass through grid boxes assigned to this tag.
Adds a tag to the dgrid.tag
table.
name
: Hashed name of tag.passable
:bool
indicating whether characters may pass through grid boxes assigned to this tag.
Returns the tag's integer value which may be used when constructing your collision map.
Registers the current game object in the grid system.
config
: Table for setting up this character's properties.size
:vector3
of integers specifying this character's dimensions in pixels.direction
: Initialdgrid.direction
in which your character is looking.speed
: Movement speed in grid boxes per second. Ifspeed = 0
, then movement is instant.
Returns an instance of dgrid.
Gets the size of this character in pixels.
Returns a number.
Gets the dgrid.direction
in which this character is looking.
Returns an entry of the dgrid.direction
table.
Gets the speed of this character in grid boxes per second.
Returns a number.
Checks if this character is moving.
Returns a bool
.
Checks if this character is able to move through impassable grid boxes specified by the collision map.
Returns a bool
.
Gets the grid coordinates of this character.
Returns a vector3
.
Gets the map coordinates of this character. Map coordinates take into account the map_offset
.
Returns a vector3
.
Gets the position of the grid box directly in front of this character in grid coordinates.
Returns a vector3
.
Sets the dgrid.direction
in which this character is looking. This affects the return value of funtions such as self.dgrid.reach()
. This is also useful for simply turning a character in some direction without actually moving.
direction
: Entry in thedgrid.direction
table.
Sets the speed of this character in grid boxes per second.
speed
: Speed of this character in grid boxes per second. Ifspeed = 0
, then movement is instant.
Allows this character to move through impassable grid boxes as specified by the collision map.
flag
:bool
indicating whether to force movement.
Adds a lerp callback, which triggers upon each complete character movement.
callback
: Callback function.volatile
:bool
indicating whether to remove this callback after being triggered once.
Removes a lerp callback, which triggers upon each complete character movement. Does nothing if the specified callback does not exist.
callback
: Callback function.volatile
:bool
indicating whether to remove this callback after being triggered once.
Sets the position of this character in grid coordinates.
grid_position
:vector3
denoting the grid box to warp to.
Begin moving in some direction. Movement will continue until self.dgrid.stop()
is called.
direction
: Entry in thedgrid.direction
table.
Stop moving in some direction.
direction
: Entry in thedgrid.direction
table. Omit this argument if you wish to stop movement in all directions instead of just one.
Updates all relevant properties. Must be called in this character's update()
function.
dt
: Change in time since last frame.
Unregisters this character from dgrid. Must be called in this character's final()
function.