Sign in to follow this  

Template for tower defence game

This topic is 1106 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

is anyone looking to make a xeno-tactic style tower defence game?  What I mean is one where the enemies are free to navigate around towers placed anywhere on a playing area as opposed to a defined path all enemies must follow.

 

If you are I've got the fundamental framework for an efficient game.  I tested my code and was able to run the game with over 50 towers and over 80 enemies with minimal system lag.

 

here is my pathfinding algorithm for anyone to adopt. written in javascript.  Some modifying to make it fit your game would be necessary.

 

EXPLANATION

 

for (var i=0; i<= TotalEnemiesOnBoard; i++) {

var Path_String = new Array(); // used to build a string containing the space id's the enemy would move to in succession to reach it's goal space. The % is there as a place marker between each space id.
Path_String[1] = "%" + CurrentSpaceOccupiedByEnemy[i]; // i is equal to the id of the enemy we're trying to find a path for.

for (var x = 1; x <= TotalSpaces; x ++){
Space[1][x] = Space[0][x];
};
Space[1][NewTower] = false; // NewTower is the space id of the space the player is trying to build a tower
// Space[0][x] is a boolean indicating if the space can be moved to. If a tower occupies the space then it's state would be set to false, otherwise it would be set to true. 'x' is the space's id
// Space[1][x] is a temporary copy of Space[0][x] used to alter the boolean state without messing with Space[0][x]
// Space[2][x] indicates total number of surrounding spaces for this space. This allows a map that is comprised of differently shaped spaces.
// Space[3][x] ~ Space[?][x] stores the space id's of all surrounding spaces for each space in question.
// ** IMPORTANT **, I don't declare the Space Array in this algorithm. that has to be done when you generate your map, however it looks.

// target_path[0][i] is the space id of the goal space the enemy wants to reach
// target_path[1][i] is the space id of the current space the enemy is occupying

// ** IMPORTANT **, I don't delcare target_path here, I do so when I create a new enemy
var sm_incremental = 0; // through each iteration of the algorithm, if a path hasn't been found, this is increased by one.
var incremental = 1; // used to tell the algorithm to keep searching for a path if no path has been yet found by being increased by sm_incremental.
var Path_String_incremental = 1; // if a free space has been found for an enemy to move to, this will increase by one.
var keepgoing = false; // this lets the algorithm know if it should continue searching
for (var x = 1; x <= incremental; x++){
sm_incremental = 0; // new iteration so this is reset to 0
for ( var y = 3; y <= 2 + Space[2][target_path[x][i]]; y++){ // total iterations of y is equal to number of surrounding spaces each space would have. squares would have 4 or 8 depending and hexagons would have 6. a mix of the two and other shapes would vary y through each iteration

if (Space[0][Space[y][target_path[x][i]]] == false && Space[1][Space[y][target_path[x][i]]] == false){

sm_incremental ++;
Path_String_incremental ++;
Space[1][Space[y][target_path[x][i]]]=true;

Path_String[Path_String_incremental]=Path_String[x] + "%" + Space[y][target_path[x][i]];
keepgoing = true;
target_path[Path_String_incremental][i] = Space[y][target_path[x][i]];
if (Space[y][target_path[x][i]] == target_path[0][i]){ // if this condition is satisfied, a path has been found to the goal space for this enemy.

keepgoing = false;
path2use = Path_String[Path_String_incremental];
y = 3 + Space[2][target_path[x][i]];

x = incremental + 1;
};
};
};

if (keepgoing == true){

incremental += sm_incremental;

};
};

};
// Last note, you'd have to run this algorithm for each enemy on the board and make sure that all enemies can find their respective goal space. if they can then the tower the player is trying to create is allowed.

 


Anyways, it might be of help or use to someone.

If you like this, I also have other optimization techniques for a tower defence game.

 

p.s,  I have poor programming conventionswacko.png

Edited by Awoken

Share this post


Link to post
Share on other sites

This topic is 1106 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this