Skip to content

Latest commit

 

History

History

003-how-to-use-behavior-tile-movement

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
title c3p hasDemo deprecated preview tags date
How to use behavior TileMovement
how-to-use-behavior-tile-movement-20.06.07.c3p
true
true
preview.png
javascript
created updated
2020-06-05 12:00
2020-06-05 12:00

How to use behavior TileMovement

global_runtime.js

runOnStartup(async  runtime =>  {  globalThis.g_runtime  =  runtime })

c3_tile_movement_helper.js

function SimulateControl(uid, direction) {
	const objectUID = g_runtime.getInstanceByUid(uid).behaviors.TileMovement;
	const canMove = CanMoveTo(uid, direction)
	if (canMove) objectUID.simulateControl(direction);
	return canMove;
}

function CanMoveTo(uid, direction) {
	const objectUID = g_runtime.getInstanceByUid(uid).behaviors.TileMovement;
	const position = objectUID.getGridPosition();
	const x = position[0];
	const y = position[1];

	let canMove = false;

	switch (direction.toLowerCase()) {
		case "left":
			canMove =  objectUID.canMoveTo(x-1,y);
		break;
		case "right":
			canMove =  objectUID.canMoveTo(x+1,y);
		break;
		case "up":
			canMove =  objectUID.canMoveTo(x,y-1);
		break;
		case "down":
			canMove =  objectUID.canMoveTo(x,y+1);
		break;
		default:
			canMove = false;
		break;
	}

	return canMove;
}