Skip to content
Snippets Groups Projects
Commit 1eb30370 authored by Eldar Hauge Torkelsen's avatar Eldar Hauge Torkelsen
Browse files

Its no longer a bug, its a feature!

parent acb3c410
No related branches found
No related tags found
No related merge requests found
......@@ -72,8 +72,8 @@ public partial class CityGen : MonoBehaviour {
//TODO:add prefab TEST
this.prefab = Resources.Load<GameObject>(prefabPath);
this.footprint.x = prefab.GetComponentInChildren<Renderer>().bounds.max.x - prefab.GetComponentInChildren<Renderer>().bounds.min.x;
this.footprint.y = prefab.GetComponentInChildren<Renderer>().bounds.max.y - prefab.GetComponentInChildren<Renderer>().bounds.min.y;
this.footprint.x = prefab.GetComponentInChildren<Renderer>().bounds.max.x +0.5f - prefab.GetComponentInChildren<Renderer>().bounds.min.x;
this.footprint.y = prefab.GetComponentInChildren<Renderer>().bounds.max.z +0.5f - prefab.GetComponentInChildren<Renderer>().bounds.min.z;
this.offsetForFootprint.x = prefab.GetComponentInChildren<Renderer>().bounds.min.x;
this.offsetForFootprint.y = prefab.GetComponentInChildren<Renderer>().bounds.min.y;
......@@ -85,30 +85,52 @@ public partial class CityGen : MonoBehaviour {
public int height;
public Vector3 orientation;
public string zoneName;
public int development; // How developed is the building / equal to index in the zoneType prefabs
public Tile(){
this.zoneName = "Empty";
}
public string updateTile(Vector3 value){
public Tile(Tile original){
this.height = original.height;
this.orientation = original.orientation;
this.zoneName = original.zoneName;
this.development = original.development;
}
public Tile updateTile(Vector3 value){
Tile newTile = new Tile(this);
// update zoneName
float mostProminent = 0;
for(int i =0; i < 3; i++ ){
if(value[i] > mostProminent){
mostProminent = value[i];
}
}
if(value.x != 0){
this.zoneName = "Recidential";
newTile.zoneName = "Recidential";
newTile.development = (int)value.x;
} else if (value.y != 0){
this.zoneName = "Commercial";
newTile.zoneName = "Commercial";
newTile.development = (int)value.y;
} else if (value.z != 0){
this.zoneName = "Industrial";
newTile.zoneName = "Industrial";
newTile.development = (int)value.z;
} else {
this.zoneName = "Empty";
newTile.zoneName = "Empty";
newTile.development = 0;
}
return this.zoneName;
return newTile;
}
public void genDemo(int id, string zoneName){
this.height = 4 + id;
this.zoneName = zoneName;
this.orientation = new Vector3(1.0f, 0.0f, 0.0f);
this.development = id;
}
}
......@@ -118,8 +140,8 @@ public partial class CityGen : MonoBehaviour {
public List<Prefab> prefabs;
public Vector2 layoutDimension;
[HideInInspector]
public int[,] layout;
public Tile[] tiles;
public Tile[,] layout;
//public Tile[] tiles;
public Prefab findPrefabById(int id){
foreach(Prefab prefab in prefabs){
......@@ -136,22 +158,42 @@ public partial class CityGen : MonoBehaviour {
this.zoneTypes[0] = new ZoneType(
"Recidential",
(Vector3 tileInfo) => {return new Vector3(1,0,0);}
(Vector3 tileInfo) => {
tileInfo.x += 0.5f;
tileInfo.y += 0.25f;
tileInfo.z += -0.5f;
return tileInfo;
}
);
this.zoneTypes[1] = new ZoneType(
"Commercial",
(Vector3 tileInfo) => {return new Vector3(0,1,0);}
(Vector3 tileInfo) => {
tileInfo.x += 0.25f;
tileInfo.y += 0.25f;
tileInfo.z += -0.25f;
return tileInfo;
}
);
this.zoneTypes[2] = new ZoneType(
"Industrial",
(Vector3 tileInfo) => {return new Vector3(0,0,1);}
(Vector3 tileInfo) => {
tileInfo.x += -0.25f;
tileInfo.y += -0.25f;
tileInfo.z += 0.75f;
return tileInfo;
}
);
this.zoneTypes[3] = new ZoneType(
"Empty",
(Vector3 tileInfo) => {return Vector3.zero;}
(Vector3 tileInfo) => {
return tileInfo;
tileInfo.x += 0.25f;
tileInfo.y += -0.75f;
tileInfo.z += -0.75f;
}
);
prefabs = new List<Prefab>();
......@@ -168,22 +210,6 @@ public partial class CityGen : MonoBehaviour {
return null;
}
//Update tile information and return id of prefab
public int updateTile(int x, int y, Vector3 value){
string zoneName = this.tiles[(int)(x + y * this.layoutDimension.x)].updateTile(value);
//update layout[x,y]
if(zoneName == "Recidential"){
return (int)value.x;
} else if (zoneName == "Commercial"){
return (int)value.y;
} else if (zoneName == "Industrial"){
return (int)value.z;
} else {
return 0;
}
}
public void genDemo(){
for(int i = 0; i < this.zoneTypes.Length; i++){
......@@ -217,29 +243,37 @@ public partial class CityGen : MonoBehaviour {
}
}
this.layoutDimension.x = 5;
this.layoutDimension.y = 5;
this.layoutDimension.x = 15;
this.layoutDimension.y = 15;
this.layout = new int[(int) this.layoutDimension.x,(int) this.layoutDimension.y];
this.layout = new Tile[(int)this.layoutDimension.x, (int)this.layoutDimension.y];//[(int) this.layoutDimension.x,(int) this.layoutDimension.y];
for(int i =0; i < this.layoutDimension.x; i++){
for(int j = 0; j < this.layoutDimension.y; j++){
this.layout[j, i] = new Tile();
}
}
string[] zoneNames = {"Recidential", "Commercial", "Industrial"};
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
this.layout[j,i] = 1+i+j*3;
this.layout[j,i].genDemo(1+j+i*3, zoneNames[j]);//= 1+i+j*3;
}
}
this.tiles = new Tile[25];
//this.tiles = new Tile[25];
for(int i =0; i < this.layoutDimension.x * this.layoutDimension.y; i++){
/*for(int i =0; i < this.layoutDimension.x * this.layoutDimension.y; i++){
this.tiles[i] = new Tile();
}
string[] zoneNames = {"Recidential", "Commercial", "Industrial"};
for(int y = 0; y < 3; y++){
for(int x = 0; x < 3; x++){
this.tiles[(int)(x + y * this.layoutDimension.x)].genDemo(1+x+y*3,zoneNames[x]);
}
}
}*/
}
......
......@@ -22,7 +22,7 @@ public partial class CityGen : MonoBehaviour {
this.city.genDemo();
// Run simulation
for(int i = 0; i < 1; i++) {
for(int i = 0; i < 5; i++) {
this.updateBoard();
}
......@@ -36,8 +36,8 @@ public partial class CityGen : MonoBehaviour {
string logBoard = "";
for(int i = 0; i < this.city.layoutDimension.y; i++) {
for(int j = 0; j < this.city.layoutDimension.x; j++) {
logBoard += this.city.layout[j,i] + this.city.tiles[j + i * (int)this.city.layoutDimension.x].zoneName + " ";
Prefab prefab = city.findPrefabById(this.city.layout[j,i]);
logBoard += this.city.layout[j,i].development + this.city.layout[j,i].zoneName + " ";
Prefab prefab = city.findPrefabById(this.city.layout[j,i].development);
if(prefab != null && prefab.prefab != null){
//print(prefab.identifier);
Vector3 position = new Vector3( transform.position.x + j * prefab.footprint.x,
......@@ -66,7 +66,7 @@ public partial class CityGen : MonoBehaviour {
}*/
// A temp variable to hold the next state while it's being calculated.
int[,] newBoard = new int[(int)this.city.layoutDimension.x, (int)this.city.layoutDimension.y];
Tile[,] newBoard = new Tile[(int)this.city.layoutDimension.x, (int)this.city.layoutDimension.y];
for (var y = 0; y < this.city.layoutDimension.y; y++) {
for (var x = 0; x < this.city.layoutDimension.x; x++) {
......@@ -90,11 +90,13 @@ public partial class CityGen : MonoBehaviour {
}
//updates the type and development of tile at (x,y)
private int updateTile(int x, int y)
private Tile updateTile(int x, int y)
{
// The value indicating type and development of current tile.
Vector3 value = getTileValueVector(x, y);
List<Vector3> effects = new List<Vector3>();
// This nested loop enumerates the 9 cells in the specified cells neighborhood.
for (var j = -1; j <= 1; j++) {
// If loopEdges is set to false and y+j is off the board, continue.
......@@ -104,9 +106,9 @@ public partial class CityGen : MonoBehaviour {
// Loop around the edges if y+j is off the board.
int k = (y + j + (int)this.city.layoutDimension.y) % (int)this.city.layoutDimension.y;
for (var i = -1; i <= 1; i++) {
// If loopEdges is set to false and x+i is off the board, continue.
// If loopEdges is set to false and x+i is off the board, continue.
if (!this.loopEdges && x + i < 0 || x + i >= (int)this.city.layoutDimension.x) {
continue;
}
......@@ -116,33 +118,37 @@ public partial class CityGen : MonoBehaviour {
// Count the neighbor cell at (h,k) if it is alive.
ZoneType zoneType = this.city.findZoneByName(
this.city.tiles[(int)(h + k * this.city.layoutDimension.x)].zoneName
this.city.layout[h, k].zoneName
);
if(zoneType != null){
value = zoneType.zoneRule(value);
}
//if(zoneType != null){
effects.Add(zoneType.zoneRule(value));
//}
}
}
int prefabId = this.city.updateTile(x, y, value);
return prefabId;
return this.city.layout[x, y].updateTile(
this.standardize(
this.grindDaEffects(effects)
)
);
}
// Get a Vector4 representing tile at (x,y) preference towards the three zoneTypes and development.
public Vector3 getTileValueVector(int x, int y){
string zoneName = this.city.tiles[(int)(x + y * this.city.layoutDimension.x)].zoneName;
string zoneName = this.city.layout[x, y].zoneName;
ZoneType zoneType = this.city.findZoneByName(zoneName);
if(zoneType.zoneName == "Empty"){
return Vector3.zero;
}
/*
int development = zoneType.getDevelopmentIndex(
this.city.layout[x, y]
);
this.city.layout[x, y].development
); */
int development = this.city.layout[x,y].development;
switch(zoneName){
case "Recidential":
return new Vector3(1.0f * development, 0.0f, 0.0f);
......@@ -156,5 +162,31 @@ public partial class CityGen : MonoBehaviour {
return Vector3.zero;
}
public Vector3 grindDaEffects(List<Vector3> effects){
Vector3 result = new Vector3();
foreach (Vector3 effect in effects)
result += (effect + new Vector3(
UnityEngine.Random.Range(0, 10),
UnityEngine.Random.Range(0, 10),
UnityEngine.Random.Range(0, 10)
)
);
return result;
}
public Vector3 standardize(Vector3 vector){
vector.Normalize();
vector = vector * 3;
if (Mathf.Min(vector.x, vector.y, vector.z) == 0.0f)
return vector;
float ratio = Mathf.Max(vector.x, vector.y, vector.z) / Mathf.Min(vector.x, vector.y, vector.z);
return vector * ratio ;
}
}
/*
public Tile updateTile(Vector3 value){
Tile newTile = new Tile(this);
// update zoneName
int mostProminent = 0;
float heighestValue = 0.0f;
for(int i =0; i < 3; i++ ){
if(value[i] > heighestValue){
heighestValue = value[i];
mostProminent = i;
}
}
float ratio = Mathf.Max(value.x, value.y, value.z) / Mathf.Min(value.x, value.y, value.z);
//Find the range of development (require zoneTypes) -> this.zoneTypes[mostProminent].prefabsIdentifier.Count;
// Set zoneName based on most prominent feature
switch(mostProminent){
case 0:{
if(this.zoneName == "Recidential"){
newTile.development = this.development++;
}else{
newTile.development = this.development--;
}
newTile.zoneName = "Recidential";
break;
}
case 1:{
if(this.zoneName == "Commercial"){
newTile.development = this.development++;
}else{
newTile.development = this.development--;
}
newTile.zoneName = "Commercial";
break;
}
case 2:{
if(this.zoneName == "Industrial"){
newTile.development = this.development++;
}else{
newTile.development = this.development--;
}
newTile.zoneName = "Industrial";
break;
}
}
return newTile;
}
*/
\ No newline at end of file
fileFormatVersion: 2
guid: 0aae670bee2894d9ba0692b061f5d3c7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment