Kezarus
Endless Game Maker
Hello everyone!
Just posting my turn-based movement code for the ones that might need it.
First of, there are other codes besides just the movement, like enums and constants, but I think you can figure it out.
Feel free to ask me anything. I hope this helps. =]
Just posting my turn-based movement code for the ones that might need it.
First of, there are other codes besides just the movement, like enums and constants, but I think you can figure it out.
Call this to comence the move nodes
GML:
function CharCombatMove(charId){
CharCombatMoveFill(charId, charId.x div COMBAT_CELL_SIZE, charId.y div COMBAT_CELL_SIZE, 0, noone);
}
This is a recursive function to place all Move Nodes
GML:
function CharCombatMoveFill(charId, cellX, cellY, totalCost, cameFrom){
//STAY WITHIN BOUNDS
if( cellX < 0 || cellY < 0 ||
cellX >= room_width div COMBAT_CELL_SIZE ||
cellY >= room_height div COMBAT_CELL_SIZE ){
return;
}
var cost = 1;
if( cameFrom == noone ){ //STARTING POINT COST
cost = 0;
}else{
cost = CombatMoveGetSquareCost(charId, cellX, cellY);
}
if( cost == -1 ){ return; } //BLOCKED TERRAIN
totalCost += cost;
//NOT ENOUGH MOVEMENT TO REACH IT
if( totalCost > charId.moveNow + charId.dashNow ){ return; }
//CENTER OF SQUARE
var wx = (cellX * COMBAT_CELL_SIZE)+(COMBAT_CELL_SIZE div 2);
var wy = (cellY * COMBAT_CELL_SIZE)+(COMBAT_CELL_SIZE div 2);
var moveNode = collision_point(wx, wy, objMoveNode, false, false);
var cantClick = false;
if(moveNode == noone){
moveNode = instance_create(wx, wy, objMoveNode);
cantClick = position_meeting(wx, wy, objCharCombat); //SET NODE ON TOP ANOTHER CHAR AS UNCLICKABLE
}else if(moveNode.totalCost <= totalCost){
return;
}else if(moveNode.totalCost > totalCost){
//FOUND A BEST PATH
//WILL BE SET BELOW
}
moveNode.owner = charId;
moveNode.totalCost = totalCost;
moveNode.cameFrom = cameFrom;
moveNode.cantClick = cantClick;
if(moveNode.totalCost > charId.moveNow){
moveNode.isDash = true;
}else{
moveNode.isDash = false;
}
CharCombatMoveFill(charId, cellX+1, cellY+0, totalCost, moveNode);
CharCombatMoveFill(charId, cellX-1, cellY+0, totalCost, moveNode);
CharCombatMoveFill(charId, cellX+0, cellY+1, totalCost, moveNode);
CharCombatMoveFill(charId, cellX+0, cellY-1, totalCost, moveNode);
}
It returns the cost of a square or -1 if it's blocked by something
GML:
function CombatMoveGetSquareCost(charId, cellX, cellY){
//returns -1 if blocked
var wCost = 1; //STANDARD
//GET SNOW COST
if(objCombatManager.grdSnow[# cellX, cellY] != -1){
wCost += 1;
}
//GET TERRAIN COST
var wx = (cellX * COMBAT_CELL_SIZE)+(COMBAT_CELL_SIZE div 2);
var wy = (cellY * COMBAT_CELL_SIZE)+(COMBAT_CELL_SIZE div 2);
var inst = collision_point(wx, wy, objDodats, false, false);
if( inst != noone ){
if( inst.type == DodatsEnum.Bushes || inst.type == DodatsEnum.Rocky ){
wCost += 1;
}else if( inst.type == DodatsEnum.Tree || inst.type == DodatsEnum.BigRock ){
wCost = -1; //BLOCKED
}
}
//ACCOUNT FOR ALLIES AND ENEMIES
var otherChar = collision_point(wx, wy, objCharCombat, false, false);
if( otherChar != noone ){
if( charId.Team != otherChar.Team ){
wCost = -1; //BLOCKED
}
}
return wCost;
}
GML:
image_blend = c_blue;
image_alpha = 0.75;
depth = -y+2;
totalCost = 0;
cameFrom = noone;
mouseOver = false;
isDash = false;
owner = noone;
alarm[0] = 1;
cantClick = false;
GML:
/// @description CONSTRUCTOR
if(isDash){
image_blend = c_red;
}
if(cantClick){
visible = false;
}
GML:
draw_self();
if( mouseOver ){
depth = -100000;
var lastNode = id;
var wCameFrom = cameFrom;
while( wCameFrom != noone ){
draw_set_color(c_blue);
draw_set_alpha(1);
draw_line_width(lastNode.x, lastNode.y, wCameFrom.x, wCameFrom.y, 10);
lastNode = wCameFrom;
wCameFrom = lastNode.cameFrom;
}
draw_set_font_ext(fntStandard, c_yellow, fa_center, fa_middle, 1);
draw_text(x, y, totalCost);
}
GML:
/// @description ADD PATH
if( !cantClick ){
//GET PATH
var path = [];
var wCameFrom = id;
do{
array_push(path, [wCameFrom.x, wCameFrom.y]);
wCameFrom = wCameFrom.cameFrom;
}until(wCameFrom == noone)
with(owner){
//SET PATH
movePath = path;
//DECREMENT MOVE
if(other.totalCost <= moveNow){
moveNow -= other.totalCost;
}else{
var cost = other.totalCost - moveNow;
moveNow = 0;
dashNow -= cost;
}
alarm[1] = 1; //MOVE START
}
instance_destroy(objMoveNode);
}
GML:
mouseOver = true;
image_alpha = 1;
GML:
mouseOver = false;
image_alpha = 0.75;
depth = -y+2;
This moves the Char
GML:
/// @description MOVE
alarm[1] = 1;
//NO DESTINATION
if( moveToX == -1 ){
//BUT I HAVE WHERE TO GO
if( array_length(movePath) > 0 ){
var arr = array_pop(movePath);
moveToX = arr[0];
moveToY = arr[1];
}else{
//END OF PATH
alarm[1] = -1;
speed = 0;
CharCombatMoveEnd(id);
}
}
var speedNow = moveSpeed; //TODO: PUT MODIFIER HERE FOR GAME SPEED UP
if( moveToX != -1 ){
if( point_distance(x, y, moveToX, moveToY) <= speedNow ){
x = moveToX;
y = moveToY;
moveToX = -1;
moveToY = -1;
speed = 0;
}else{
//REAL MOVE
move_towards_point(moveToX, moveToY, speedNow);
}
}
Feel free to ask me anything. I hope this helps. =]
Last edited: