1
Fork 0

Create WIP map script

This commit is contained in:
Steffo 2019-04-27 13:38:29 +02:00
parent b97dc71ee5
commit fbd17b8ae5
5 changed files with 110 additions and 19 deletions

81
Assets/Scripts/Map.cs Normal file
View file

@ -0,0 +1,81 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Map : MonoBehaviour
{
public const int SIZE_X = 30;
public const int SIZE_Y = 30;
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) {
GameObject tileObject = Instantiate(tilePrefab);
tileObject.transform.position = new Vector3(x, y, 0);
tiles[x, y] = tileObject;
Tile tile = GetTile(x, y);
tile.is_walkable = is_walkable;
tile.tile_sprite = tile_sprite;
}
private void FillWithWalls() {
for(int x = 0; x < SIZE_X; x++) {
for(int y = 0; y < SIZE_Y; y++) {
InitTile(x, y, false, wallSprite);
}
}
}
private void GenerateSquare(int start_x, int start_y, int end_x, int end_y) {
for(int x = start_x; x <= end_x; x++) {
for(int y = start_y; y <= end_y; y++) {
InitTile(x, y, true, floorSprite);
}
}
}
private void GenerateCorridor(int start_x, int start_y, int end_x, int end_y, bool horizontal_priority) {
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();
//Generate a random Square
int start_x = Random.Range(0, SIZE_X);
int start_y = Random.Range(0, SIZE_Y);
int end_x = Random.Range(start_x, SIZE_X);
int end_y = Random.Range(start_y, SIZE_Y);
GenerateSquare(start_x, start_y, end_x, end_y);
}
private void Start()
{
//Create the tile array
tiles = new GameObject[SIZE_X, SIZE_Y];
//Generate the map
GenerateMap();
}
}

View file

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 3a2a8e24cbb5e9a4f88abc3055e15cb7 guid: 052bc569472779a429bfc51246f723d4
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View file

@ -1,18 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}

17
Assets/Scripts/Tile.cs Normal file
View file

@ -0,0 +1,17 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tile : MonoBehaviour
{
public bool is_walkable;
public Sprite tile_sprite;
private SpriteRenderer spriteRenderer;
private void Start()
{
spriteRenderer = GetComponent<SpriteRenderer>();
spriteRenderer.sprite = tile_sprite;
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4df8012d0534aea47b137c1838a5a6af
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: