From fbd17b8ae5a8b6c3f5136a0eef04e26a159dd1dc Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 27 Apr 2019 13:38:29 +0200 Subject: [PATCH] Create WIP map script --- Assets/Scripts/Map.cs | 81 ++++++++++++++++++++ Assets/Scripts/{Test.cs.meta => Map.cs.meta} | 2 +- Assets/Scripts/Test.cs | 18 ----- Assets/Scripts/Tile.cs | 17 ++++ Assets/Scripts/Tile.cs.meta | 11 +++ 5 files changed, 110 insertions(+), 19 deletions(-) create mode 100644 Assets/Scripts/Map.cs rename Assets/Scripts/{Test.cs.meta => Map.cs.meta} (83%) delete mode 100644 Assets/Scripts/Test.cs create mode 100644 Assets/Scripts/Tile.cs create mode 100644 Assets/Scripts/Tile.cs.meta diff --git a/Assets/Scripts/Map.cs b/Assets/Scripts/Map.cs new file mode 100644 index 0000000..be6b013 --- /dev/null +++ b/Assets/Scripts/Map.cs @@ -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(); + 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(); + } +} diff --git a/Assets/Scripts/Test.cs.meta b/Assets/Scripts/Map.cs.meta similarity index 83% rename from Assets/Scripts/Test.cs.meta rename to Assets/Scripts/Map.cs.meta index ef9de0e..659896b 100644 --- a/Assets/Scripts/Test.cs.meta +++ b/Assets/Scripts/Map.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 3a2a8e24cbb5e9a4f88abc3055e15cb7 +guid: 052bc569472779a429bfc51246f723d4 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/Scripts/Test.cs b/Assets/Scripts/Test.cs deleted file mode 100644 index 2f5a7ab..0000000 --- a/Assets/Scripts/Test.cs +++ /dev/null @@ -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() - { - - } -} diff --git a/Assets/Scripts/Tile.cs b/Assets/Scripts/Tile.cs new file mode 100644 index 0000000..3de0bc6 --- /dev/null +++ b/Assets/Scripts/Tile.cs @@ -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.sprite = tile_sprite; + } +} diff --git a/Assets/Scripts/Tile.cs.meta b/Assets/Scripts/Tile.cs.meta new file mode 100644 index 0000000..cb27721 --- /dev/null +++ b/Assets/Scripts/Tile.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4df8012d0534aea47b137c1838a5a6af +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: