Last active
August 29, 2015 14:06
-
-
Save shadowmint/38cbe3fab3e2f76cfa45 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma strict | |
private var map:TacticsMap; | |
function Start () { | |
// Create a map | |
map = TacticsMap.factory(10, 10, 2.0); | |
map.registerType(0, "blocks/Grass"); | |
map.randomize(); | |
map.build(this.gameObject); | |
// Look at the map | |
var camera = GameObject.Find("Camera"); | |
camera.transform.position = new Vector3(0, -10, 10); | |
camera.transform.LookAt(new Vector3(0, 0, 0)); | |
} | |
function Update () { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma strict | |
import System.Collections.Generic; | |
/** Create a new instance */ | |
public static function factory(dx:int, dy:int, size:float):TacticsMap { | |
var rtn = new TacticsMap(); | |
rtn.tiles = new int[dx * dy]; | |
rtn.heights = new int[dx * dy]; | |
rtn.dx = dx; | |
rtn.dy = dy; | |
rtn.size = size; | |
rtn.blocks = new TacticsBlock[dx * dy]; | |
return rtn; | |
} | |
/** Map cell */ | |
public class TacticsBlock { | |
var x:int; | |
var y:int; | |
var o:GameObject; | |
} | |
/** Map helper */ | |
public class TacticsMap { | |
/** The set of tile ids */ | |
public var tiles:int[]; | |
/** The set of height ids */ | |
public var heights:int[]; | |
/** Width in blocks */ | |
public var dx:int; | |
/** Height in blocks */ | |
public var dy:int; | |
/** Block size */ | |
public var size:float; | |
/** Tile types */ | |
public var types:Dictionary.<int, GameObject> = new Dictionary.<int, GameObject>(); | |
/** Set of game currently visible objects for this map */ | |
public var blocks:TacticsBlock[]; | |
/** Register a tile type */ | |
public function registerType(id:int, prefab:String) { | |
var factory:GameObject = Resources.Load(prefab, GameObject); | |
this.types[id] = factory; | |
} | |
/** Generate random map contents */ | |
public function randomize() { | |
var offset = this.dx * this.dy; | |
for (var i = 0; i < offset; ++i) { | |
this.tiles[i] = 0; | |
this.heights[i] = math.random(0, 10); | |
} | |
} | |
/** Generate scene objects for this map */ | |
public function build(root:GameObject) { | |
var xoffset = (this.dx * this.size) / 2.0; | |
var yoffset = (this.dy * this.size) / 2.0; | |
for (var y = 0; y < this.dy; ++y) { | |
for (var x = 0; x < this.dx; ++x) { | |
var offset = x + y * this.dx; | |
var b = new TacticsBlock(); | |
b.x = x; | |
b.y = y; | |
b.o = root.Instantiate(this.types[this.tiles[offset]]); | |
b.o.transform.localScale = new Vector3(this.size, this.size, this.size); | |
b.o.transform.position = new Vector3(x * this.size - xoffset, y * this.size - yoffset, this.heights[offset] * this.size / 10); | |
this.blocks[offset] = b; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment