/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ndU6
*/
package {
import flash.text.TextField;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
deb = new TextField();
deb.mouseEnabled = false;
deb.width = 320;
deb.height = 240;
addChild(deb);
tmap = new xMap();
tmap.initEmpty(32,32);
var a:xTile;
a = tmap.vecSet[1];
a.food = 1;
a.prod = 1;
a.col = 0x404040;
a = tmap.vecSet[2];
a.food = 5;
a.col = 0xa0a0a0;
a = tmap.vecSet[2];
a.food = 3;
a.prod = 2;
a.col = 0xe0a0a0;
var i:int;
for (i = 0; i < 512; i++)
{ tmap.setTile(Math.random()*32,Math.random()*32,Math.random()*4); }
myCity = new xCity();
myCity.cx = 8;
myCity.cy = 5;
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var tmap:xMap;
public var myCity:xCity;
public var deb:TextField;
public function onEnter(e:Event):void
{
graphics.clear();
tmap.drawMap(graphics);
graphics.lineStyle(2,0xFF0000);
graphics.beginFill(0,0.2);
graphics.drawCircle(myCity.cx*tmap.cw+tmap.cw*0.5,myCity.cy*tmap.ch+tmap.ch*0.5,10+myCity.pop);
graphics.endFill();
myCity.update(tmap);
deb.text = ""+myCity.pop+"\n"+myCity.food;
}//onenter
}//classend
}
import flash.display.Graphics;
internal class xCity
{
public var cx:int = 0;
public var cy:int = 0;
public var pop:int = 1;
public var food:int = 0;
public function update(tmap:xMap):void
{
var i:int; var k:int; var a:xTile;
for (i = -2; i<3;i++)
for (k = -2; k<3;k++)
{
//tmap.setTile(k+cx,i+cy, 2);
a = tmap.getTile(k+cx,i+cy);
food += a.food;
}
food -= pop;
if (food >= pop*10) {food = 0; pop+=1;}
}//update
}//xcity
internal class xTile
{
public var id:int = 0;
public var food:int = 0;
public var prod:int = 0;
public var col:uint = 0x808080;
}//xtile
internal class xMap
{
public var mw:int = 0;
public var mh:int = 0;
public var cw:Number = 16;
public var ch:Number = 16;
public var vecGrid:Vector.<int>;
public var vecSet:Vector.<xTile>;
public function xMap()
{
resetSet();
}//ctor
public function resetSet():void
{
var i:int; var num:int;
num = 1024;
vecSet = new Vector.<xTile>(num,false);
for (i =0;i<num;i++) {vecSet[i]=new xTile(); vecSet[i].id = i; }
}//resetset
public function initEmpty(w:int, h:int):void
{
var num:int; var i:int;
mw = w; mh = h;
num = mw*mh;
vecGrid = new Vector.<int>(num,false);
for (i =0;i<num;i++){vecGrid[i]=0;}
}//init
public function getTile(ax:int, ay:int):xTile
{
var t:int;
if (ax < 0) { return vecSet[0]; } if (ax >= mw) { return vecSet[0]; }
if (ay < 0) {return vecSet[0]; } if (ay >= mh) {return vecSet[0];}
t = vecGrid[ay*mw+ax]; return vecSet[t];
}//gettile
public function setTile(ax:int, ay:int, t:int):void
{
if (ax < 0) { return; } if (ax >= mw) { return; }
if (ay < 0) {return; } if (ay >= mh) {return;}
vecGrid[ay*mw+ax]=t;
}//gettile
public function drawMap(g:Graphics):void
{
var i:int; var k:int; var yt:int;
var t:int; var a:xTile;
for (i = 0; i < mh; i++)
{ yt = i*mw;
for (k = 0; k < mw; k++)
{
t = vecGrid[yt+k];
if (t <= 0) { continue; }
a = vecSet[t];
g.beginFill(a.col, 1);
g.drawRect(k*cw,i*ch,cw,ch);
g.endFill();
}//nextk
}//nexti
}//drawmap
}//xmap