Grid based Games – Part 5.2: Towers

This post enhances the previous post by the ability of actually placing obstacles which somewhen may become towers.

Here is what we are going to create:

To have interaction with stage we will set up an EventListener for clicks. First of all we declare a variable named building and set its value according to the actual node. Therefore the mouseMoveHandler function has to be slightly changed:

var building = false;

function mouseMoveHandler ( event:MouseEvent )
{
	var actNode = getPos ( mouseX - size*0.5 , mouseY - size*0.5);
	with ( overlay.graphics )
	{
		clear();
		if ( actNode )
		{
			if ( actNode.blocked )
			{
				building = false;
				beginFill(0x990000, 0.5);
				lineStyle(1, 0x990000, 1);
				drawRect(actNode.x - size*0.5, actNode.y - size*0.5, size*2, size*2);
				endFill();
			}
			else
			{
				building = true;
				beginFill(0x009900, 0.5);
				lineStyle(1, 0x009900, 1);
				drawRect(actNode.x - size*0.5, actNode.y - size*0.5, size*2, size*2);
				endFill();
			}
		}
	}
}

We then add a new movieclip to draw what we have built and and again add the overlay to have it on top.

var buildMap:MovieClip = new MovieClip();
addChild(buildMap);
addChild(overlay);
stage.addEventListener(MouseEvent.CLICK, buildTower);

function buildTower ( event:MouseEvent )
{
	if ( building )
	{
		var actNode = getPos ( mouseX - size*0.5 , mouseY - size*0.5);
		with ( buildMap.graphics )
		{
			beginFill(0x000099, 1);
			lineStyle(1, 0xffffff, 1);
			drawRect(actNode.x - size*0.5, actNode.y - size*0.5, size*2, size*2);
			endFill();
		}
		var actArray = getNeighbors ( actNode );
		for each ( var nObj in actArray )
		{
			nObj.blocked = true;
		}
		actNode.blocked = true;
	}
}

By clicking a new rectangle is drawn to the buildMap on the actual node’s position and the node as well as its neighbors are blocked for further building.

function posToNode ( u, v )
{
	var nodeString = u + "." + v;
	var nodePos = posArray.indexOf(nodeString);
	if ( nodePos > -1 )
	{
		return ( nodeArray[nodePos] );
	}
	else
	{
		return null;
	}
}

posToNode does nothing more than concatenating u and v, looking it up in the posArray and returning the node. This is done for every adjacent node via the getNeighbors function:

function getNeighbors ( centerNode )
{
	var thisU = centerNode.u;
	var thisV = centerNode.v;
	var resultArray:Array = new Array();
	resultArray.push ( posToNode ( thisU - 1, thisV - 1 ) );
	resultArray.push ( posToNode ( thisU - 1, thisV ) );
	resultArray.push ( posToNode ( thisU - 1, thisV + 1 ) );
	resultArray.push ( posToNode ( thisU, thisV + 1 ) );
	resultArray.push ( posToNode ( thisU + 1, thisV + 1 ) );
	resultArray.push ( posToNode ( thisU + 1, thisV ) );
	resultArray.push ( posToNode ( thisU + 1, thisV - 1 ) );
	resultArray.push ( posToNode ( thisU, thisV - 1 ) );
	return resultArray;
}

With that we are now able to place obstacles on the playground. Placing is only possible on unblocked nodes.

Try it. What now? Yoho!

This entry was posted in Grid Based Games, grids, mochiads and tagged , , , , , , , , . Bookmark the permalink.

One Response to Grid based Games – Part 5.2: Towers

  1. Pingback: Grid based games – Part 5.3: Pathfinding « YARR!cade.com LÞ the kegogrog blog