<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>FlexBandit &#187; Power Grid</title>
	<atom:link href="http://flexbandit.com/archives/category/projects/powergrid/feed" rel="self" type="application/rss+xml" />
	<link>http://flexbandit.com</link>
	<description>Yet another Flex enthusiast? You Betcha!</description>
	<lastBuildDate>Thu, 13 May 2010 16:58:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Power Grid &#8211; MapTile Component</title>
		<link>http://flexbandit.com/archives/6</link>
		<comments>http://flexbandit.com/archives/6#comments</comments>
		<pubDate>Fri, 27 Jun 2008 13:43:35 +0000</pubDate>
		<dc:creator>donkeybandit</dc:creator>
				<category><![CDATA[Power Grid]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://matt.was-here.org/?p=6</guid>
		<description><![CDATA[The interactive map is the key component of Power Grid. As the initial version I am writing is for the &#8220;Atolla Mudulis&#8221; expansion, I have to compile a map that deals with multiple &#8220;tiles&#8221; in variable combinations. Also, the map needs to be movable and zoomable both with the mouse and via a button interface. [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft" style="margin: 5px; float: left;" src="http://flexbandit.com/powergrid/assets/board/A7.png" alt="Map Component" width="119" height="119" />The interactive map is the key component of Power Grid. As the initial version I am writing is for the &#8220;Atolla Mudulis&#8221; expansion, I have to compile a map that deals with multiple &#8220;tiles&#8221; in variable combinations. Also, the map needs to be movable and zoomable both with the mouse and via a button interface.</p>
<p>I tried two approaches to creating the map, one which involved the <em>TileContainer </em>and another which created a single image.<span id="more-6"></span></p>
<h2>The <em>TileContainer </em>Approach</h2>
<p>The advantage of using a <em>TileContainer </em>is that it deals with all of the mechanics of placing the tiles in the correct alignment (providing you specified the appropriate properties). Each Tile in the container maintains its original properties and can be identified in a MouseEvent. This would have a lot of advantages in a game that uses tiles (such as Carcassonne or even Dominoes) but does not serve the purposes of a game board too well. If no aspect resizing (i.e. zooming in/out) is required then the TileCointer would be satisfactory, but of course my game board does require this functionality.</p>
<h2>The <em>Image </em>Approach</h2>
<p>After wrestling with the <em>TileContainer </em>for a day, I decided to scrap this approach to the game board implementation in favour of a single image with multiple &#8220;hit areas&#8221; (i.e. one hit area per city). The advantages this gave me were:</p>
<ul>
<li>the <code>localX</code> and <code>localY</code> coordinates were preserved, even after resizing</li>
<li>problems with the MOUSE_OUT event were resolved when dragging the image around</li>
<li>more control over where each image tile was placed, i.e. no fighting with the <em>TileContainer </em>styles</li>
</ul>
<p>I created a class for the game board image called <em>MapTile</em> which extends the <em>Image</em> class.</p>
<p>The next challenge I faced after deciding to drop the <em>TileContainer </em>approach was how to deal with each image as it loaded. Initially I attempted a simple <em>for each</em> loop which added each image to the map, but this didn&#8217;t work due to the unpredictable time it takes to load each tile. I could have embedded the images, but I am trying to keep the size of the .swf down so opted to use a COMPLETE event which signalled the need to load the next tile. It works nicely, and in the current version allows you to watch each tile load into the map.</p>
<h2>Conclusion</h2>
<p>This was ultimately a pretty simple problem. I needed to create an image that could be used by the main map component to zoom in and out of, as well as drag operations. This class takes in the array of tiles to be used, then creates one image at run time.</p>
<h2><em>MapTile </em>Code</h2>
<p>All of the images used for the map board can be found <a title="in the Power Grid assets directory" href="http://matt.was-here.org/powergrid/assets/board/">here</a>.</p>
<p>The full code for the <em>MapTile</em> class is listed below:</p>
<pre>package com.gamecomponents
{
	import mx.controls.Image;
	import flash.events.Event;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.geom.Rectangle;
	import flash.geom.Point;
	import flash.display.Sprite;

	public class MapTile extends Image
	{
		private var mapBitmap:Bitmap;
		private var mapBitmapData:BitmapData;
		private var loader:Loader;

		private var row:int;
		private var column:int;
		private var creationCounter:int;
		private var tileChooser:Array;
		private var sideLength:int;

		// Location of tile images and file extension
		private const sourcePath:String  = "assets/board/";
		private const filenameExt:String = ".png";

		// All map tiles must be 500 pixels square
		private const tileWidth:int = 500;

		public function MapTile(inMapConfig:Array = null):void {
			if (inMapConfig == null) {
				tileChooser = [
					"A7", "B7", "C6",
					"D6", "E5", "F5",
					"G4", "H4", "I3",
				];
			} else {
				tileChooser = inMapConfig;
			}

			// Calculate how many tiles per side
			sideLength = Math.sqrt(tileChooser.length);

			row    = 0;
			column = 0;

			// Tiles are all currently 500 pixels square
			var mapSize:int = tileWidth * sideLength;
			mapBitmapData   = new BitmapData(mapSize, mapSize);
			mapBitmap       = new Bitmap();

			// Create map image
			creationCounter = 0;
			getNextTile();
		}

		private function getNextTile():Boolean {
			if (tileChooser.length &lt;= 0) {
				return false;
			}

			// Get the next tile in the array
			var tileName:String = tileChooser.shift();

			// Load new tile
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, picLoaded);
			loader.load(new URLRequest(sourcePath + tileName + filenameExt));

			return true;
		}

		private function picLoaded(event:Event):void
		{
			// Define a BitmapData object with dimensions identical to the sprite
			var loadedBitmapData:BitmapData;
			loadedBitmapData = new BitmapData(loader.width, loader.height);
			// Draw the image into the BitmapData object.
			loadedBitmapData.draw(loader);

			mapBitmapData.copyPixels(loadedBitmapData,
									 new Rectangle(0, 0, tileWidth, tileWidth),
									 new Point(column * tileWidth, row * tileWidth)
			);

			mapBitmap   = new Bitmap(mapBitmapData);
			this.source = mapBitmap;

			if ((creationCounter+1)%sideLength == 0) {
				column = 0;
				row++;
			} else {
				column++;
			}

			creationCounter++;

			if (!getNextTile()) {
				// All tiles have been loaded
				loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, picLoaded);
			}
		}
	}
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://flexbandit.com/archives/6/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Power Grid &#8211; Project Goals</title>
		<link>http://flexbandit.com/archives/5</link>
		<comments>http://flexbandit.com/archives/5#comments</comments>
		<pubDate>Fri, 27 Jun 2008 10:25:54 +0000</pubDate>
		<dc:creator>donkeybandit</dc:creator>
				<category><![CDATA[Power Grid]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://matt.was-here.org/?p=5</guid>
		<description><![CDATA[The motivation and choice for the Power Grid project comes from extensive play of the original Power Grid board game by Friedemann Friese. My aim is to improve upon the original German implementation of the online version which I found unplayable. This post defines the application and purpose in a little detail.]]></description>
			<content:encoded><![CDATA[<p>-</p>
<h1>Purpose</h1>
<p>This project has primarily been initiated to demonstrate the following key Flex skills:</p>
<ul>
<li>General Flex aptitude</li>
<li>Flex Data Services</li>
<li>Messaging</li>
<li>PureMVC Framework</li>
<li>OO competence</li>
<li>Skinning</li>
<li>Rich Media</li>
<li>Video (if technically viable and appropriate)</li>
</ul>
<p>The motivation and choice for this project comes from extensive play of the original Power Grid board game by Friedemann Friese. My aim is to improve upon the original German implementation of the <a title="Funkenschlag by Brettspielwelt" href="http://www.brettspielwelt.de/Spiele/Funkenschlag/?nation=en" target="_blank">online version</a> which I found unplayable.<span id="more-5"></span></p>
<h1>Components</h1>
<p>The solution is broken into three main parts.</p>
<h2>Intro Screen</h2>
<p>The intro is purely for demonstrating animation and creativity. As the majority of Flex/Flash adopters are media companies, it is essential for me that this skill is demonstrated effectively.</p>
<h3>Animation Description</h3>
<p>At the start of the animation there is a long shot of a power station with a blue sky and the sound of birds singing. The scene then zooms in and the sound changes to that of fuel burning and smoke starts to emerge from the chimneys. The camera follows the smoke upwards as the sky turns darker, then pans downwards back to the power station. During the pan, the whole scene turns to black and white with a dark blue tint. The Power Grid logo then stamps onto the screen in full colour and pauses.</p>
<h2>Game Lobby</h2>
<p>There needs to be an area for people to meet up and set up individual games. This site isn&#8217;t being designed to be the next MSN gamin zone, so only a simple screen is required at present.</p>
<h3>Login Dialog</h3>
<p>If an unauthenticated visitor enters the site, they need to provide a name for themself so they can be recognised. A dialog box appears requesting that the person enter a unique username.</p>
<h3>User List</h3>
<p>Along the right hand side is a box with a list of users.</p>
<h3>Game Set Up</h3>
<p>A window showing games open/in progress.</p>
<h3>Main Chat Window</h3>
<p>IRC style chat. Individual chat windows do not seem necessary at this point as users could use other messaging tools such as Jabber instead.</p>
<h2>Game Board</h2>
<p>The main gaming area, once a game has been initiated from the lobby.</p>
<h3>Markets</h3>
<p>A thin vertical border on the left hand side with coloured numbered squares. Each square represents a power station card in the &#8220;actual market&#8221;. When the user hovers the mouse over the border, the full market is displayed.</p>
<h3>Map</h3>
<p>The map displays the game board itself and contains cities that can be interacted with. By using the mouse or a set of buttons, the user can pan the map and zoom in/out using the scroll wheel. Clicking on a city may result in a marker being placed on it at certain points in the game.</p>
<h3>Player Status</h3>
<p>Along the right hand side of the screen a minimised status of all players is displayed. They are listed in player order (as a result of stage 1). Hovering or clicking on a players stats results in a larger window appearing with more detail.</p>
<h3>Raw Materials</h3>
<p>At the bottom of the screen a horizontal border will be displayed with a summary indicator of each raw material. Hovering on the border results in the full display of currently available materials.</p>
<h3>Chat Area</h3>
<p>An IRC style area displaying any chat messages from players/observers.</p>
<h1>Game Mechanics</h1>
<p>The rules of the game are clearly defined in the Power Grid rule book (available with the physical board game).</p>
]]></content:encoded>
			<wfw:commentRss>http://flexbandit.com/archives/5/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
