Emthree is a match three engine for Defold.
You can use Emthree in your own project by adding this project as a Defold library dependency. Open your game.project file and in the dependencies field under project add:
https://github.com/britzl/emthree/archive/master.zip
Or point to the ZIP file of a specific release.
Two examples, one basic and one a bit more advanced are available to try here:
This documentation uses a number of different concepts to describe the functionality of Emthree. Please refer to the list below to better understand how Emthree works.
A grid of slots where the game is played.
A position on the board. A slot can hold a block.
A block represents an element in a slot on the board. It could be fish, candy, cakes or something else, fitting the theme of the game. Blocks will fall if there are free slots below them. Blocks have a color and an optional type.
A blocker represents an element in a slot on the board that is static. It will not fall and it will stop blocks from falling any further. Blockers have an optional type.
A spawner represents an element in a slot on the board that creates/spawns blocks. It will not fall and it will stop blocks from falling any further. Spawners have an optional type.
Three or more blocks of the same color is considered a match.
A board is considered stable when the board doesn't contain any matches and all slots are filled with blocks.
Create an Emthree board of the specified dimensions
PARAMETERS
width
(number) - Board width in blocksheight
(number) - Board height in blocksblock_size
(number) - Size of a blockconfig
(table) - Optional table with board configuration values
The config
table can contain the following values:
remove_duration
- Time in seconds to wait while removing blocksswap_duration
Time in seconds for the swap animationslide_duration
- Time in seconds for the slide animationcollapse_duration
- Time in seconds for the collapse animationcollapse_direction
- Direction to move blocks in when collapsing the board after removing blocks. Can be one ofemthree.COLLAPSE_UP
,emthree.COLLAPSE_DOWN
,emthree.COLLAPSE_LEFT
oremthree.COLLAPSE_RIGHT
slide_easing
- Easing function to apply when sliding blocks. Any of thego.EASING_*
constants.collapse_easing
- Easing function to apply when collapsing blocks. Any of thego.EASING_*
constants.
RETURN
board
(table) - A representation of the board, complete with a 2D array of blocks
Stabilize the board. This will search through the board for any matches, remove these and the spawn new blocks. This process will be repeated until there are no matches to remove or new blocks to spawn. When the board is stable it will invoke the function specified by emthree.on_stabilized
.
PARAMETERS
board
(table) - The board to stabilizecallback
(function) - Optional function to call when the board is stable
Pass user input to the board to detect interaction with the blocks. Only touch/click events with a state of pressed
or released
must be sent. When a block is selected through a click Emthree will send an emthree.SELECT
message to the game object representing the block. When a block is deselected an emthree.RESET
message is sent. Emthree will release input focus while processing input and acquire it again when the board is stable.
PARAMETERS
board
(table) - The board to apply the input toaction
(table) - The action table received from the engine lifecycle function on_input. Only pass on touch/click events wherepressed
orreleased
is true.
Return an iterator function that can be used to iterate all blocks on the board. The blocks can optionally be filtered by the iterator to only return blocks that match certain criteria
PARAMETERS
board
(table) - The board to iteratefilter_fn
(function) - A function that can be used to filter blocks. The function will receive the board, x and y position as arguments and must return true or false depending on if the block at that position should be returned or not.
RETURN
fn
(function) - The iterator function. The function will return a block.
Get all blocks of a certain color
PARAMETERS
board
(table) - The board to get blocks fromcolor
(any) - The color to search for
RETURN
blocks
(table) - A list of all blocks matching the specified color
Get the block at a specific position
PARAMETERS
board
(table) - The board to get the block fromx
(number) - Horizontal positiony
(number) - Vertical position
RETURN
block
(table) - The block or nil if position is outside board
Check if a position is on the board
PARAMETERS
board
(table) - The board to check position onx
(number) - The horizontal slot positiony
(number) - The vertical slot position
RETURN
on_board
(boolean) - True if the position is on the board
Check if the content of a slot is a block
PARAMETERS
board
(table) - The board to checkx
(number) - Horizontal positiony
(number) - Vertical position
RETURN
result
(boolean) - True if there is a block in the slot
Check if the content of a slot is a blocker
PARAMETERS
board
(table) - The board to checkx
(number) - Horizontal positiony
(number) - Vertical position
RETURN
result
(boolean) - True if there is a blocker in the slot
Check if the content of a slot is a spawner
PARAMETERS
board
(table) - The board to checkx
(number) - Horizontal positiony
(number) - Vertical position
RETURN
result
(boolean) - True if there is a spawner in the slot
Check if a slot is empty
PARAMETERS
board
(table) - The board to checkx
(number) - Horizontal positiony
(number) - Vertical position
RETURN
result
(boolean) - True if the slot is empty
Remove a block from the board. This will invoke the function specified by emthree.on_block_removed()
. This will also post an emthree.REMOVE
message to the game object representing the block.
PARAMETERS
board
(table) - The board to remove block fromblock
(table) - The block to remove
Remove a list of blocks. Will repeatedly call emthree.remove_block()
.
PARAMETERS
board
(table) - The board to remove blocks fromblocks
(table) - The blocks to remove
Change a block on the board from one type and color to another. This will post an emthree.CHANGE
message to the game object representing the block.
PARAMETERS
block
(table) - The block to changetype
(any) - The type to change tocolor
(any) - The color to change to
Create a new block on the board. This will call the function passed to emthree.on_create_block()
.
PARAMETERS
board
(table) - The board to create block onx
(number) - The horizontal slot position of the blocky
(number) - The vertical slot position of the blocktype
(any) - The type of the blockcolor
(any) - The color of the block
RETURN
block
(table) - The created block
Create a new blocker on the board. This will call the function passed to emthree.on_create_blocker()
PARAMETERS
board
(table) - The board to create blocker onx
(number) - The horizontal slot position of the blockery
(number) - The vertical slot position of the blockertype
(any) - The type of the blocker
RETURN
blocker
(table) - The created blocker
Create a new spawner on the board. This will call the function passed to emthree.on_create_spawner()
PARAMETERS
board
(table) - The board to create spawner onx
(number) - The horizontal slot position of the spawnery
(number) - The vertical slot position of the spawnertype
(any) - The type of the spawner
RETURN
blocker
(table) - The created spawner
Fill the board with blocks. This will call the function passed to emthree.on_create_block()
.
PARAMETERS
board
(table) - The board to fill with blocks
Shuffle the blocks on the board.
PARAMETERS
board
(table) - The board to shuffle blocks on
Set a function to be called whenever a block is to be created on the board. The function is expected to spawn a game object and return the game object id.
PARAMETERS
board
(table) - The board that will notify when a new block needs to be createdfn
(function) - The function to call when a block should be created
The function must accept and return the following:
PARAMETERS
board
(table) - The board where the block should be createdposition
(vector3) - Position of the block to createtype
(any) - The block type, can be nilcolor
(any) - The color of the block
RETURN
id
(hash) - Id of the created game objecttype
(any) - Type of the created blockcolor
(any) - Color of the created block
Set a function to be called whenever a blocker is to be created on the board. The function is expected to spawn a game object and return the game object id.
PARAMETERS
board
(table) - The board that will notify when a new blocker needs to be createdfn
(function) - The function to call when a blocker should be created
The function must accept and return the following:
PARAMETERS
board
(table) - The board where the blocker should be createdposition
(vector3) - Position of the blocker to createtype
(any) - The blocker type, can be nil
RETURN
id
(hash) - Id of the created game objecttype
(any) - Type of the created blocker
Set a function to be called whenever a spawner is to be created on the board. The function is expected to spawn a game object and return the game object id.
PARAMETERS
board
(table) - The board that will notify when a new spawner needs to be createdfn
(function) - The function to call when a spawner should be created
The function must accept and return the following:
PARAMETERS
board
(table) - The board where the spawner should be createdposition
(vector3) - Position of the spawner to createtype
(any) - The spawner type, can be nil
RETURN
id
(hash) - Id of the created game objecttype
(any) - Type of the created spawner
Set a function to be called whenever a match on the board is detected. Use this callback to remove the blocks involved in the match and optionally also create new blocks based on the match. The default implementation will remove the blocks and do nothing else.
PARAMETERS
board
(table) - The board that will notify when a match is detectedfn
(function) - The function to call when a match is detected.
The function must accept the following arguments:
PARAMETERS
board
(table) - The board where the match was detectedblock
(table) - A block that is part of the matchhorisontal_neighbors
(table) - A list of horizontal neighboring blocks that are part of the matchvertical_neighbors
(table) - A list of vertical neighboring blocks that are part of the match
Set a function to be called whenever a block is removed. Use this callback to trigger effects on special blocks.
PARAMETERS
board
(table) - The board that will notify when a block is removedfn
(function) - The function to call when a block is removed. The function will receiveboard
andblock
as arguments.
Set a function to be called when the board is stable.
PARAMETERS
board
(table) - The board that will notify when stablefn
(function) - The function to call when the board is stable. The function will receiveboard
as arguments.
Set a function to be called whenever a two blocks have swapped places based on user input. Use this to trigger effects when certain types of blocks were swapped.
PARAMETERS
board
(table) - The board where swaps will be notifiedfn
(function) - The function to call when a swap is made. The function is expected to takeboard
,slot1
andslot2
as arguments and return true if swap resulted in any changes on the board.
Set a function to be called whenever the board has no possible switches available to create a match. You can use emthree.shuffle()
to attempt to rearragne the blocks.
PARAMETERS
board
(table) - The board where no possible switches availab le will be notifiedfn
(function) - The function to call when no possible switches are detected. The function is expected to takeboard
as arguments.
Get the slot at a screen position (pixels).
PARAMETERS
board
(table) - The board to get the slot fromx
(number) - Horizontal screen position (pixels)y
(number) - Vertical screen position (pixels)
RETURN
x
(number) - Horizontal slot positiony
(number) - Vertical slot position
Get the screen position (pixels) of a slot.
PARAMETERS
board
(table) - The board to get the slot fromx
(number) - Horizontal slot positiony
(number) - Vertical slot position
RETURN
x
(number) - Horizontal screen position (pixels)y
(number) - Vertical screen position (pixels)
Message sent to the game object representing a block on the board when it is to be removed.
Message sent to the game object representing a block on the board when it is to change color and/or type.
PARAMETERS
color
(any) - The color to change totype
(any) - The type to change to
Message sent to the game object representing a block on the board when it is selected.
Message sent to the game object representing a block on the board when it is deselected.