- Home /
Unable to check WALL to WALL check for tower defense game
I need help from you ppl again :) . I am making a 2D tower defense game. So far I did is I have made a 2d array which act as a grid for my game, placed towers on it, ran enemy and some dragging and tower firing stuff
Now I am facing a problem in tower placement logic. I want that any one path should be free for enemy, means that user must be unable to block a way by placing towers. for eg. if user places towers vertically on a map it must leave a space of one tower if all towers has placed in vertical way. or in any other way it must leave at least one space so that enemy can escape from that way. I think you have got what I am trying to say.
I have code a logic for it that, whenever tower has placed it checks in its all direction if there is a wall or tower, if there is a tower upwards it again call this function on that up tower until a wall comes, it returns 1 for upward wall and 5 for downward wall and return function(up/down tower) if there is a tower. here is the code:
int checkCollision(tower)
{
if( there is a tower up) return checkCollision(up tower);
if(there is a tower down)
return checkCollision(down tower);
......all directions.....
if( there is a wall on UP )
return 1;
if( there is a wall DOWN )
return 5;
....all walls......
return 0;
}
Now what I want is, simultaneously check if there is a up wall and down wall OR check any other directions with any other possibility(like up-down, up-diagnol, down diagnol...so on) then do not let user to place tower as there should be one place left for enemy.
I am unable to check it, I mean my code tells that a wall is found but how can I check that a wall is found in one direction and a wall also found in another direction, I dont want to go into possibilities that
if(checkCollision(tower) == 1 && checkCollision(tower) == 5)
"You cannot place"
I want something like:
if( any combination of more than 2 wall found and there is entry/exit point in between) //so it is wall to wall
"You cant place"
I have also tired placing flags that true if there are wall on two sides(like up-down, up-diagonal etc. ) then dont let user to place tower but still I cant make it. Pls help me with this I am really stucked here.
I think you need a pathfinding algorithm for this stuff.
Your answer
Follow this Question
Related Questions
convert c# to JavaScript Problem 0 Answers
Send a variable from javascript to a C# Application 0 Answers
is Unity support socket javascpt client 1 Answer
Test if object is within collider bounds on start 0 Answers
Including a DLL in unity? 1 Answer