1
Fork 0
slime-blood-and-pain/Assets/Scripts/Map.cs

92 lines
2.7 KiB
C#
Raw Normal View History

2019-04-27 11:38:29 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Map : MonoBehaviour
{
2019-04-27 12:21:33 +00:00
[BeforeStartAttribute]
public int mapSize = 30;
[BeforeStartAttribute]
public int roomsToGenerate = 5;
[BeforeStartAttribute]
public int maxRoomSize = 8;
2019-04-27 11:38:29 +00:00
public GameObject[,] tiles;
public Sprite wallSprite;
public Sprite floorSprite;
public GameObject tilePrefab;
public Tile GetTile(int x, int y) {
GameObject tileObject = tiles[x, y];
Tile tile = tileObject.GetComponent<Tile>();
return tile;
}
private void InitTile(int x, int y, bool is_walkable, Sprite tile_sprite) {
Tile tile = GetTile(x, y);
2019-04-27 12:21:33 +00:00
tile.walkable = is_walkable;
tile.sprite = tile_sprite;
2019-04-27 11:38:29 +00:00
}
private void FillWithWalls() {
2019-04-27 12:21:33 +00:00
for(int x = 0; x < mapSize; x++) {
for(int y = 0; y < mapSize; y++) {
GameObject tileObject = Instantiate(tilePrefab, transform);
tileObject.transform.position = new Vector3(x, y, 0);
tiles[x, y] = tileObject;
Tile tile = tileObject.GetComponent<Tile>();
tile.walkable = false;
tile.sprite = wallSprite;
2019-04-27 11:38:29 +00:00
}
}
}
2019-04-27 12:21:33 +00:00
private void PlaceRoom(int start_x, int start_y, int end_x, int end_y) {
2019-04-27 11:38:29 +00:00
for(int x = start_x; x <= end_x; x++) {
for(int y = start_y; y <= end_y; y++) {
InitTile(x, y, true, floorSprite);
}
}
}
2019-04-27 12:21:33 +00:00
private void PlaceCorridor(int start_x, int start_y, int end_x, int end_y, bool horizontal_priority) {
2019-04-27 11:38:29 +00:00
if(horizontal_priority) {
for(int x = start_x; x <= end_x; x++) {
InitTile(x, start_y, true, floorSprite);
}
for(int y = start_y; y <= end_y; y++) {
InitTile(end_x, y, true, floorSprite);
}
}
else {
for(int y = start_y; y <= end_y; y++) {
InitTile(start_x, y, true, floorSprite);
}
for(int x = start_x; x <= end_x; x++) {
InitTile(x, end_y, true, floorSprite);
}
}
}
private void GenerateMap() {
FillWithWalls();
2019-04-27 12:21:33 +00:00
for(int i = 0; i < roomsToGenerate; i++) {
int start_x = Random.Range(0, mapSize);
int start_y = Random.Range(0, mapSize);
int end_x = Random.Range(0, mapSize) + start_x;
int end_y = Random.Range(0, mapSize) + start_y;
PlaceRoom(start_x, start_y, end_x, end_y);
}
2019-04-27 11:38:29 +00:00
}
private void Start()
{
//Create the tile array
2019-04-27 12:21:33 +00:00
tiles = new GameObject[mapSize, mapSize];
2019-04-27 11:38:29 +00:00
//Generate the map
GenerateMap();
}
}