1
0
Fork 0

Merge branch 'TriceraptodactylDev'

This commit is contained in:
Triceraptodactyl 2018-04-25 14:00:21 +02:00
commit fb03f163db
3 changed files with 215 additions and 229 deletions

View file

@ -3,255 +3,241 @@ using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class GenerationProcessor { public class GenerationProcessor {
public enum ExtendedTileType { public enum ExtendedTileType {
BorderOuter, BorderInner, BorderSingle, Ground0, Ground1, Ground2, Ground3, DoorInner, DoorOuter, Rock, RockL, RockU, RockR, RockD, RockLU, RockLR, RockLD, RockUR, RockUD, RockRD, RockLURD, RockLUD, RockLUR, RockURD, RockLRD, Flag BorderOuter, BorderInner, BorderSingle, Ground0, Ground1, Ground2, Ground3, DoorInner, DoorOuter, Rock, RockL, RockU, RockR, RockD, RockLU, RockLR, RockLD, RockUR, RockUD, RockRD, RockLURD, RockLUD, RockLUR, RockURD, RockLRD, Flag
} }
Dictionary<ExtendedTileType, GameObject> prefabs;
public GenerationProcessor(Dictionary<ExtendedTileType, GameObject> prefabs) {
this.prefabs = prefabs;
}
public GameObject ProcessRoom(Dictionary<Vector2Int, GenTile> tiles) { Dictionary<ExtendedTileType, GameObject> prefabs;
GameObject root = new GameObject { public GenerationProcessor(Dictionary<ExtendedTileType, GameObject> prefabs) {
name = "Room" this.prefabs = prefabs;
}; }
foreach ( Vector2Int v in tiles.Keys ) {
ExtendedTileType type = GetRandomGroundType(); public GameObject ProcessRoom(Dictionary<Vector2Int, GenTile> tiles) {
int rotation = 0; GameObject root = new GameObject {
switch ( tiles[v].type ) { name = "Room"
case Room.TileType.WALL: };
type = GetCorrectWallType(tiles, v); foreach ( Vector2Int v in tiles.Keys ) {
rotation = GetCorrectWallRotation(tiles, v, type); ExtendedTileType type = GetRandomGroundType();
break; int rotation = 0;
case Room.TileType.GROUND: switch ( tiles[v].type ) {
type = GetRandomGroundType(); case Room.TileType.WALL:
break; type = GetCorrectWallType(tiles, v);
case Room.TileType.DOOR: rotation = GetCorrectWallRotation(tiles, v, type);
type = GetCorrectDoorType(tiles, v); break;
rotation = GetCorrectDoorRotation(type, tiles, v); case Room.TileType.GROUND:
break; type = GetRandomGroundType();
case Room.TileType.ROCK: break;
type = GetCorrectRockType(tiles, v); case Room.TileType.DOOR:
break; type = GetCorrectDoorType(tiles, v);
} rotation = GetCorrectDoorRotation(type, tiles, v);
GameObject go = CreateGOFromType(v, rotation, tiles[v].type, type, root); break;
// Todo dirty hack case Room.TileType.ROCK:
if ( go.tag == "door" ) { type = GetCorrectRockType(tiles, v);
go.GetComponent<Door>().SetToOuter(GenerationProcessor.GetDirectionVector(tiles[v].position)); break;
} }
} GameObject go = CreateGOFromType(v, rotation, tiles[v].type, type, root);
return root; // Todo dirty hack
if ( go.tag == "door" ) {
go.GetComponent<Door>().SetToOuter(GenerationProcessor.GetDirectionVector(tiles[v].position));
}
} }
return root;
}
public GameObject CreateGOFromType(Vector2 v, int rotation, Room.TileType type, ExtendedTileType t, GameObject root) { public GameObject CreateGOFromType(Vector2 v, int rotation, Room.TileType type, ExtendedTileType t, GameObject root) {
GameObject tmp = null; GameObject tmp = null;
if ( type != Room.TileType.GROUND ) if ( type != Room.TileType.GROUND )
CreateGOFromType(v, 0, Room.TileType.GROUND, GetRandomGroundType(), root); CreateGOFromType(v, 0, Room.TileType.GROUND, GetRandomGroundType(), root);
if ( prefabs.ContainsKey(t) && root != null ) { if ( prefabs.ContainsKey(t) && root != null ) {
tmp = Object.Instantiate(prefabs[t], root.transform); tmp = Object.Instantiate(prefabs[t], root.transform);
tmp.transform.position = v; tmp.transform.position = v;
tmp.transform.Rotate(new Vector3(0, 0, rotation)); tmp.transform.Rotate(new Vector3(0, 0, rotation));
}
return tmp;
} }
return tmp;
}
private ExtendedTileType GetCorrectWallType(Dictionary<Vector2Int, GenTile> tiles, Vector2Int position) { private ExtendedTileType GetCorrectWallType(Dictionary<Vector2Int, GenTile> tiles, Vector2Int position) {
int groundNumber = 0; int groundNumber = 0;
Vector2Int toCheck = position + new Vector2Int(0, -1); Vector2Int toCheck = position + new Vector2Int(0, -1);
if ( tiles.ContainsKey(toCheck) && tiles[toCheck].type != Room.TileType.WALL ) if ( tiles.ContainsKey(toCheck) && tiles[toCheck].type != Room.TileType.WALL )
groundNumber++; groundNumber++;
toCheck = position + new Vector2Int(-1, 0); toCheck = position + new Vector2Int(-1, 0);
if ( tiles.ContainsKey(toCheck) && tiles[toCheck].type != Room.TileType.WALL ) if ( tiles.ContainsKey(toCheck) && tiles[toCheck].type != Room.TileType.WALL )
groundNumber++; groundNumber++;
toCheck = position + new Vector2Int(0, 1); toCheck = position + new Vector2Int(0, 1);
if ( tiles.ContainsKey(toCheck) && tiles[toCheck].type != Room.TileType.WALL ) if ( tiles.ContainsKey(toCheck) && tiles[toCheck].type != Room.TileType.WALL )
groundNumber++; groundNumber++;
toCheck = position + new Vector2Int(1, 0); toCheck = position + new Vector2Int(1, 0);
if ( tiles.ContainsKey(toCheck) && tiles[toCheck].type != Room.TileType.WALL ) if ( tiles.ContainsKey(toCheck) && tiles[toCheck].type != Room.TileType.WALL )
groundNumber++; groundNumber++;
switch ( groundNumber ) { switch ( groundNumber ) {
case 0: case 0:
return ExtendedTileType.BorderInner; return ExtendedTileType.BorderInner;
case 2: case 2:
return ExtendedTileType.BorderOuter; return ExtendedTileType.BorderOuter;
default: default:
return ExtendedTileType.BorderSingle; return ExtendedTileType.BorderSingle;
}
} }
}
private int GetCorrectWallRotation(Dictionary<Vector2Int, GenTile> tiles, Vector2Int v, ExtendedTileType type) { private int GetCorrectWallRotation(Dictionary<Vector2Int, GenTile> tiles, Vector2Int v, ExtendedTileType type) {
switch ( type ) { switch ( type ) {
case ExtendedTileType.BorderSingle: case ExtendedTileType.BorderSingle:
Vector2Int toCheck = v + new Vector2Int(0, -1); Vector2Int toCheck = v + new Vector2Int(0, -1);
if(tiles.ContainsKey(toCheck) && tiles[toCheck].type != Room.TileType.WALL) if(tiles.ContainsKey(toCheck) && tiles[toCheck].type != Room.TileType.WALL)
return 0; return 0;
toCheck = v + new Vector2Int(-1, 0); toCheck = v + new Vector2Int(-1, 0);
if(tiles.ContainsKey(toCheck) && tiles[toCheck].type != Room.TileType.WALL) if(tiles.ContainsKey(toCheck) && tiles[toCheck].type != Room.TileType.WALL)
return 270; return 270;
toCheck = v + new Vector2Int(0, 1); toCheck = v + new Vector2Int(0, 1);
if(tiles.ContainsKey(toCheck) && tiles[toCheck].type != Room.TileType.WALL) if(tiles.ContainsKey(toCheck) && tiles[toCheck].type != Room.TileType.WALL)
return 180; return 180;
toCheck = v + new Vector2Int(1, 0); return 90;
if(tiles.ContainsKey(toCheck) && tiles[toCheck].type != Room.TileType.WALL) case ExtendedTileType.BorderInner:
return 90; toCheck = v + new Vector2Int(1, -1);
break; if(tiles.ContainsKey(toCheck) && tiles[toCheck].type != Room.TileType.WALL)
case ExtendedTileType.BorderInner: return 0;
toCheck = v + new Vector2Int(1, -1); toCheck = v + new Vector2Int(1, 1);
if(tiles.ContainsKey(toCheck) && tiles[toCheck].type != Room.TileType.WALL) if(tiles.ContainsKey(toCheck) && tiles[toCheck].type != Room.TileType.WALL)
return 0; return 90;
toCheck = v + new Vector2Int(1, 1); toCheck = v + new Vector2Int(-1, 1);
if(tiles.ContainsKey(toCheck) && tiles[toCheck].type != Room.TileType.WALL) if(tiles.ContainsKey(toCheck) && tiles[toCheck].type != Room.TileType.WALL)
return 90; return 180;
toCheck = v + new Vector2Int(-1, 1); return 270;
if(tiles.ContainsKey(toCheck) && tiles[toCheck].type != Room.TileType.WALL) case ExtendedTileType.BorderOuter:
return 180; Vector2Int toCheck1 = v + new Vector2Int(0, -1);
toCheck = v + new Vector2Int(-1, -1); Vector2Int toCheck2 = v + new Vector2Int(-1, 0);
if(tiles.ContainsKey(toCheck) && tiles[toCheck].type != Room.TileType.WALL) if(tiles.ContainsKey(toCheck1) && tiles.ContainsKey(toCheck2) && tiles[toCheck1].type != Room.TileType.WALL && tiles[toCheck2].type != Room.TileType.WALL)
return 270; return 270;
break; toCheck1 = v + new Vector2Int(0, 1);
case ExtendedTileType.BorderOuter: if(tiles.ContainsKey(toCheck1) && tiles.ContainsKey(toCheck2) && tiles[toCheck1].type != Room.TileType.WALL && tiles[toCheck2].type != Room.TileType.WALL)
Vector2Int toCheck1 = v + new Vector2Int(0, -1); return 180;
Vector2Int toCheck2 = v + new Vector2Int(-1, 0); toCheck2 = v + new Vector2Int(1, 0);
if(tiles.ContainsKey(toCheck1) && tiles.ContainsKey(toCheck2) && tiles[toCheck1].type != Room.TileType.WALL && tiles[toCheck2].type != Room.TileType.WALL) if(tiles.ContainsKey(toCheck1) && tiles.ContainsKey(toCheck2) && tiles[toCheck1].type != Room.TileType.WALL && tiles[toCheck2].type != Room.TileType.WALL)
return 270; return 90;
toCheck1 = v + new Vector2Int(0, 1);
if(tiles.ContainsKey(toCheck1) && tiles.ContainsKey(toCheck2) && tiles[toCheck1].type != Room.TileType.WALL && tiles[toCheck2].type != Room.TileType.WALL)
return 180;
toCheck2 = v + new Vector2Int(1, 0);
if(tiles.ContainsKey(toCheck1) && tiles.ContainsKey(toCheck2) && tiles[toCheck1].type != Room.TileType.WALL && tiles[toCheck2].type != Room.TileType.WALL)
return 90;
toCheck1 = v + new Vector2Int(0, -1);
if(tiles.ContainsKey(toCheck1) && tiles.ContainsKey(toCheck2) && tiles[toCheck1].type != Room.TileType.WALL && tiles[toCheck2].type != Room.TileType.WALL)
return 0;
break;
}
return 0; return 0;
} }
return 0;
}
private ExtendedTileType GetCorrectRockType(Dictionary<Vector2Int, GenTile> tiles, Vector2Int position) { private ExtendedTileType GetCorrectRockType(Dictionary<Vector2Int, GenTile> tiles, Vector2Int position) {
int meta = 0; int meta = 0;
Vector2Int toCheck = position + new Vector2Int(0, -1); Vector2Int toCheck = position + new Vector2Int(0, -1);
if ( tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.ROCK ) if ( tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.ROCK )
meta += 1; meta += 1;
toCheck = position + new Vector2Int(-1, 0); toCheck = position + new Vector2Int(-1, 0);
if ( tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.ROCK ) if ( tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.ROCK )
meta += 2; meta += 2;
toCheck = position + new Vector2Int(0, 1); toCheck = position + new Vector2Int(0, 1);
if ( tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.ROCK ) if ( tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.ROCK )
meta += 4; meta += 4;
toCheck = position + new Vector2Int(1, 0); toCheck = position + new Vector2Int(1, 0);
if ( tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.ROCK ) if ( tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.ROCK )
meta += 8; meta += 8;
switch ( meta ) { switch ( meta ) {
case 1: case 1:
return ExtendedTileType.RockD; return ExtendedTileType.RockD;
case 2: case 2:
return ExtendedTileType.RockL; return ExtendedTileType.RockL;
case 3: case 3:
return ExtendedTileType.RockLD; return ExtendedTileType.RockLD;
case 4: case 4:
return ExtendedTileType.RockU; return ExtendedTileType.RockU;
case 5: case 5:
return ExtendedTileType.RockUD; return ExtendedTileType.RockUD;
case 6: case 6:
return ExtendedTileType.RockLU; return ExtendedTileType.RockLU;
case 7: case 7:
return ExtendedTileType.RockLUD; return ExtendedTileType.RockLUD;
case 8: case 8:
return ExtendedTileType.RockR; return ExtendedTileType.RockR;
case 9: case 9:
return ExtendedTileType.RockRD; return ExtendedTileType.RockRD;
case 10: case 10:
return ExtendedTileType.RockLR; return ExtendedTileType.RockLR;
case 11: case 11:
return ExtendedTileType.RockLRD; return ExtendedTileType.RockLRD;
case 12: case 12:
return ExtendedTileType.RockUR; return ExtendedTileType.RockUR;
case 13: case 13:
return ExtendedTileType.RockURD; return ExtendedTileType.RockURD;
case 14: case 14:
return ExtendedTileType.RockLUR; return ExtendedTileType.RockLUR;
case 15: case 15:
return ExtendedTileType.RockLURD; return ExtendedTileType.RockLURD;
default: default:
return ExtendedTileType.Rock; return ExtendedTileType.Rock;
}
} }
}
private ExtendedTileType GetCorrectDoorType(Dictionary<Vector2Int, GenTile> tiles, Vector2Int position) { private ExtendedTileType GetCorrectDoorType(Dictionary<Vector2Int, GenTile> tiles, Vector2Int position) {
int neighbourDoors = 0; int neighbourDoors = 0;
Vector2Int toCheck = position + new Vector2Int(0, -1); Vector2Int toCheck = position + new Vector2Int(0, -1);
if ( tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.DOOR ) if (tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.DOOR)
neighbourDoors++; neighbourDoors++;
toCheck = position + new Vector2Int(-1, 0);
if (tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.DOOR)
neighbourDoors++;
toCheck = position + new Vector2Int(0, 1);
if (tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.DOOR)
neighbourDoors++;
toCheck = position + new Vector2Int(1, 0);
if (tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.DOOR)
neighbourDoors++;
if(neighbourDoors == 1)
return ExtendedTileType.DoorOuter;
return ExtendedTileType.DoorInner;
}
private int GetCorrectDoorRotation(ExtendedTileType type, Dictionary<Vector2Int, GenTile> tiles, Vector2Int position) {
Vector2Int toCheck;
if(type == ExtendedTileType.DoorOuter) {
toCheck = position + new Vector2Int(0, -1);
if (tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.DOOR)
return 270;
toCheck = position + new Vector2Int(-1, 0); toCheck = position + new Vector2Int(-1, 0);
if ( tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.DOOR ) if (tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.DOOR)
neighbourDoors++; return 180;
toCheck = position + new Vector2Int(0, 1); toCheck = position + new Vector2Int(0, 1);
if ( tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.DOOR ) if (tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.DOOR)
neighbourDoors++; return 90;
toCheck = position + new Vector2Int(1, 0);
if ( tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.DOOR )
neighbourDoors++;
switch ( neighbourDoors ) {
case 1:
return ExtendedTileType.DoorOuter;
default:
return ExtendedTileType.DoorInner;
}
}
private int GetCorrectDoorRotation(ExtendedTileType type, Dictionary<Vector2Int, GenTile> tiles, Vector2Int position) {
switch ( type ) {
case ExtendedTileType.DoorOuter:
Vector2Int toCheck = position + new Vector2Int(0, -1);
if ( tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.DOOR )
return 270;
toCheck = position + new Vector2Int(-1, 0);
if ( tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.DOOR )
return 180;
toCheck = position + new Vector2Int(0, 1);
if ( tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.DOOR )
return 90;
toCheck = position + new Vector2Int(1, 0);
return 0;
case ExtendedTileType.DoorInner:
Vector2Int toCheckD = position + new Vector2Int(0, -1);
if ( tiles.ContainsKey(toCheckD) && tiles[toCheckD].type == Room.TileType.DOOR )
return 90;
return 0;
}
return 0; return 0;
} }
toCheck = position + new Vector2Int(0, -1);
if (tiles.ContainsKey(toCheck) && tiles[toCheck].type == Room.TileType.DOOR)
return 90;
return 0;
}
public static Vector2Int GetDirectionVector(GenTile.Position p) { public static Vector2Int GetDirectionVector(GenTile.Position p) {
switch ( p ) { switch(p) {
case GenTile.Position.TOP: case GenTile.Position.TOP:
return new Vector2Int(0, 1); return new Vector2Int(0, 1);
case GenTile.Position.LEFT: case GenTile.Position.LEFT:
return new Vector2Int(-1, 0); return new Vector2Int(-1, 0);
case GenTile.Position.RIGHT: case GenTile.Position.RIGHT:
return new Vector2Int(1, 0); return new Vector2Int(1, 0);
case GenTile.Position.BOTTOM: case GenTile.Position.BOTTOM:
return new Vector2Int(0, -1); return new Vector2Int(0, -1);
default: default:
return new Vector2Int(); return new Vector2Int();
}
} }
}
private ExtendedTileType GetRandomGroundType() { private ExtendedTileType GetRandomGroundType() {
int num = ( int ) ( UnityEngine.Random.value * 4 ); int num = (int) (UnityEngine.Random.value * 4);
switch ( num ) { switch(num) {
case 0: case 0:
return ExtendedTileType.Ground0; return ExtendedTileType.Ground0;
case 1: case 1:
return ExtendedTileType.Ground1; return ExtendedTileType.Ground1;
case 2: case 2:
return ExtendedTileType.Ground2; return ExtendedTileType.Ground2;
default: default:
return ExtendedTileType.Ground3; return ExtendedTileType.Ground3;
}
} }
}
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 34 KiB

After

Width:  |  Height:  |  Size: 33 KiB