3D Tower Defense, Part 2

August 2nd, 2008 | by phantom |

So, I haven’t forgotten about this project and work has finally progressed. I did in fact do a little bit of work on this a while ago, however it wasn’t really all that important so I decided to hold off on an update for now.

Today however I have made what I consider significant progress so I figured it was a good time to update.

The minor update made before was a matter of GUI control. While all the options to the right of the screen did allow alot of control over the placement of the town and its exculsion zone it wasn’t really that intuative to use; so with that in mind I quickly created a drag-and-drop like system.

Left-clicking in the main town rectangle allows you to drag it around the playing field and using the scroll wheel lets you resize the town. Holding down the right mouse button and then scrolling adjusts the exculsion zone. These small improvements made the town placement much nicer to work with.

Today I decided to crack on with two key components of the game;

  • Creature “AI”
  • Tower placement

Tower placement

This was in fact done second, however as it introduced some new GUI elements I thought I’d show it here first. The main problem with tower placement was that I wanted to use a context menu, however the right click was already being used above for exculsion zone resizing. This meant that whenever I did the right click to resize the exculsion zone I would, once I released, get the menu pop up.

Not helpful.

It was at this point I decided I needed to split the system into two “modes”; a default editing mode and a game mode. So a few lines of code and some GUI fiddling later we have the following setup;

New Alpha 2 GUI Options

This allows us to edit the game layout etc as before, then when we hit ‘Toggle Game Mode’ it disables editing like so;

New Alpha 2 GUI Options 2

At which point we can right click and place towers around the place as we like and we can’t effect the town at all.

 The context menu’s appearing state if controlled via an event which fires when the control is opening; by setting a value we can cause the menu to abort popping up.

private void towerplacementContextMenu_Opening(object sender, CancelEventArgs e)
{
        e.Cancel = !gamemode;
}

 Talking of placing towers the current system looks as following;

Alpha 2 right click menu active in game mode

A Placed Tower

Currently the towers can be placed anywhere and don’t react to anything, which is my next job.

Creature AI

Currently the creatures use a very simple piece of AI;

  1. Get the location of the town
  2. Work out which segment of the town is closest
  3. Get a direction to that bit of town
  4. Step in that direction by one cell
  5. Rinse and repeat

Something this simple was easy to code, however it doesn’t take into account other aspects of the world, like towers and any landscape which might get added. At some point the AI will probably get upgraded to A* or something like that for path finding across the grid. Right now however the simple system gets the job done, the positions here are relative to the first GUI screen shots taken above;

Alpha 2 Post Update 1 For Creature AI

Alpha 2 Post Creature AI update 2

The code to perform the updates is pretty simple, although maybe not optimal but this isn’t important right now;

public void Update()
        {
            // Head for the closest city cell
            Point townlocation = grid.TownLocation;
            Size townSize = grid.TownSize;
            Point finalMagnitude = new Point(0, 0);
            float currentDistanceSq = float.MaxValue;
            Point gridPoint = grid.ConvertCellToGridPoint(cell);

            for(int x = 0; x < townSize.Width; x++)
            {
                for (int y = 0; y < townSize.Height; y++ )
                {
                    Point testPoint = Point.Add(townlocation, new Size(x, y));
                    Point magnitude = Point.Subtract(testPoint, new Size(gridPoint));  // direction with magnitude on each component

                    float distanceSq = Math.Abs(magnitude.X * magnitude.X + magnitude.Y * magnitude.Y);
                    if(distanceSq < currentDistanceSq)
                    {
                        currentDistanceSq = distanceSq;
                        finalMagnitude = magnitude;
                    }
                }
            }

            Size direction = new Size(Math.Sign(finalMagnitude.X), Math.Sign(finalMagnitude.Y)); // direction reduced to 1 cell step

            Point newlocation = Point.Add(gridPoint, direction);   // new location
            cell = grid.ConvertGridPointToCell(newlocation);

        }

The current binary version can be found to download below. As before it requires a .Net 3.5 install to use it and is only tested on Windows Vista right now. Tower Defense Concept Tester Alpha 2 Binary.zip (0 bytes)

You must be logged in to post a comment.