3 registered members (Joey, flink, AndrewAMD),
1,226
guests, and 4
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Tactical RPG Grid setup
#355027
01/24/11 10:19
01/24/11 10:19
|
Joined: Jan 2004
Posts: 3,023 The Netherlands
Helghast
OP
Expert
|
OP
Expert
Joined: Jan 2004
Posts: 3,023
The Netherlands
|
Roxas had asked previously how to calculate grid movement for a tactical game (link to thread: Tactical RPG Grid Calculation ) . I took an hour to write a pretty dynamic system for that, and thought people could make use of it. it's just a multi-dimensional array that calculates distance cost from any grid cell based on a given diameter, but it's a good start for any tactics game. the code clamps around the edges properly. you can use the mouse position to set a position to calculate from, and use the scrollwheel to increase/decrease the movement radius.
#include <acknex.h>
#include <default.c>
#define GRID_size 10
#define GRID_cellsize 32
var GridMoveSquares = 3; // stores size of movement diameter
var GridArray[GRID_size][GRID_size]; // stores size of grid
// visual debug representation variables
var current_row = 3;
var current_col = 1;
// function that resets every GRID value
function resetGrid() {
var X_index = 0;
var Y_index = 0;
for(X_index=0; X_index<GRID_size; X_index++) {
for(Y_index=0; Y_index<GRID_size; Y_index++) {
GridArray[X_index][Y_index] = 0;
}
}
}
// function that visually represents the GRID data
function drawGridData() {
var column_index = 0;
var row_index = 0;
for(column_index=0; column_index<GRID_size; column_index++) {
for(row_index=0; row_index<GRID_size; row_index++) {
if(GridArray[column_index][row_index] == 0) {
draw_text(str_for_num(NULL, GridArray[column_index][row_index]), GRID_cellsize*column_index, GRID_cellsize*row_index, vector(255,255,255));
} else {
draw_text(str_for_num(NULL, GridArray[column_index][row_index]), GRID_cellsize*column_index, GRID_cellsize*row_index, vector(0,0,255));
}
}
}
}
// actually calculate the cells that can be moved on
function setMoveGrid(var row, var col) {
var column_index = 0;
var row_index = 0;
var clampedIndex;
resetGrid(); // reset
for(column_index=GridMoveSquares; column_index>=0; column_index--) {
// fill right hand side of column
clampedIndex = clamp(col+column_index, 0, GRID_size-1);
GridArray[row][clampedIndex] = column_index;
for(row_index=GridMoveSquares; row_index>=column_index; row_index--) {
GridArray[clamp(row+(row_index-column_index), 0, GRID_size-1)][clampedIndex] = row_index;
GridArray[clamp(row-(row_index-column_index), 0, GRID_size-1)][clampedIndex] = row_index;
}
// fill left hand side of column
clampedIndex = clamp(col-column_index, 0, GRID_size-1);
GridArray[row][clampedIndex] = column_index;
for(row_index=GridMoveSquares; row_index>=column_index; row_index--) {
GridArray[clamp(row+(row_index-column_index), 0, GRID_size-1)][clampedIndex] = row_index;
GridArray[clamp(row-(row_index-column_index), 0, GRID_size-1)][clampedIndex] = row_index;
}
}
}
function getMouseInput() {
current_row = clamp(integer(abs(mouse_pos.x) / GRID_cellsize), 0, GRID_size-1);
current_col = clamp(integer(abs(mouse_pos.y) / GRID_cellsize), 0, GRID_size-1);
GridMoveSquares += (mickey.z/120); GridMoveSquares = clamp(GridMoveSquares, 0, GRID_size);
}
function main() {
mouse_mode = 4;
resetGrid(); // reset GRID
while(1) {
getMouseInput();
setMoveGrid(current_row,current_col); // calculate new GRID data
drawGridData(); // debug draw grid
wait(1);
}
}
I hope someone can make use of it, or atleast learn from it regards,
|
|
|
Re: Tactical RPG Grid setup
[Re: Helghast]
#355115
01/24/11 18:25
01/24/11 18:25
|
Joined: Jun 2006
Posts: 2,640 Earth
Germanunkol
Expert
|
Expert
Joined: Jun 2006
Posts: 2,640
Earth
|
There's no diagonal movement currently, correct? Only right/left/up/down...?
~"I never let school interfere with my education"~ -Mark Twain
|
|
|
Re: Tactical RPG Grid setup
[Re: Germanunkol]
#355190
01/25/11 08:48
01/25/11 08:48
|
Joined: Jan 2004
Posts: 3,023 The Netherlands
Helghast
OP
Expert
|
OP
Expert
Joined: Jan 2004
Posts: 3,023
The Netherlands
|
you should see something similar to: (Uploaded with ImageShack.us) which, as you can see, does also take into account diagonal movement (it's just an immediate cost of 2). Also, this is extremely bare-bones ofcourse, it only is a visual representation of how to calculate and clamp movement cost within a certain range, there is no movement, visuals or game at all present. Just something to get someone with the idea started. regards,
|
|
|
|