Jump to content
  • Advertisement
Sign in to follow this  
Awoken

Template for tower defence game

This topic is 1319 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 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] is the space id of the goal space the enemy wants to reach
// target_path[1] 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]]; 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]]] == false && Space[1][Space[y][target_path[x]]] == false){

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

Path_String[Path_String_incremental]=Path_String[x] + "%" + Space[y][target_path[x]];
keepgoing = true;
target_path[Path_String_incremental] = Space[y][target_path[x]];
if (Space[y][target_path[x]] == target_path[0]){ // 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]];

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
Advertisement

p.s,  I have poor programming conventions


You also posted this in Game Design, where it does not belong. Moving it somewhere else.

Share this post


Link to post
Share on other sites

If you don't put your code in code blocks, it's too hard to read. You can use the angle bracket button in the editor window, or use code tags with square brackets.

 

- Eck

 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!