168 lines
5.0 KiB
C#
168 lines
5.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
|
|
public class StageController : MonoBehaviour
|
|
{
|
|
public Train Train;
|
|
public int FocusedWagon = 0;
|
|
public int FocusedLevel = 0;
|
|
|
|
Stage Stage { get; set; }
|
|
|
|
public Sprite FloorSprite;
|
|
public Sprite WallSprite;
|
|
|
|
GameObject[,] AllTilesGameObjects;
|
|
GameObject[,] HorizontalWallsGameObjects;
|
|
GameObject[,] VerticalWallsGameObjects;
|
|
|
|
public bool ShouldRandomize = false;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
if (Train == null)
|
|
{
|
|
Train = new Train();
|
|
Train.Generate();
|
|
}
|
|
FocusCurrentStage();
|
|
|
|
AllTilesGameObjects = new GameObject[Stage.Width, Stage.Height];
|
|
HorizontalWallsGameObjects = new GameObject[Stage.Width, Stage.Height+1];
|
|
VerticalWallsGameObjects = new GameObject[Stage.Width+1, Stage.Height];
|
|
|
|
//Tiles creation
|
|
for(int x=0; x<Stage.Width; x++)
|
|
for(int y=0; y<Stage.Height; y++)
|
|
{
|
|
var tile = Stage[x, y];
|
|
|
|
GameObject tile_go = new GameObject();
|
|
AllTilesGameObjects[x, y] = tile_go;
|
|
tile_go.name = "tile_" + x + "_" + y;
|
|
tile_go.transform.position = new Vector3(x, y, 0);
|
|
tile_go.transform.SetParent(this.transform, true);
|
|
|
|
tile_go.AddComponent<SpriteRenderer>();
|
|
OnTileUpdated(tile, tile_go);
|
|
}
|
|
|
|
Stage.OnTileChanged = (Tile tile) =>
|
|
{
|
|
var go = AllTilesGameObjects[tile.X, tile.Y];
|
|
OnTileUpdated(tile, go);
|
|
};
|
|
|
|
|
|
// wall creation
|
|
for (int x = 0; x < Stage.Width; x++)
|
|
for (int y = 0; y <= Stage.Height; y++)
|
|
{
|
|
var wall = Stage[WallOrientation.Horizontal, x, y];
|
|
|
|
GameObject wallh_go = new GameObject();
|
|
HorizontalWallsGameObjects[x, y] = wallh_go;
|
|
wallh_go.name = "hwall_" + x + "_" + y;
|
|
wallh_go.transform.position = new Vector3(-0f+x, -0.5f+y, -1);
|
|
wallh_go.transform.localScale = new Vector3(1f, 0.1f, 1);
|
|
wallh_go.transform.SetParent(this.transform, true);
|
|
|
|
wallh_go.AddComponent<SpriteRenderer>();
|
|
OnWallUpdated(wall, wallh_go);
|
|
}
|
|
for (int x = 0; x <= Stage.Width; x++)
|
|
for (int y = 0; y < Stage.Height; y++)
|
|
{
|
|
var wall = Stage[WallOrientation.Vertical, x, y];
|
|
|
|
GameObject wallv_go = new GameObject();
|
|
VerticalWallsGameObjects[x, y] = wallv_go;
|
|
wallv_go.name = "vwall_" + x + "_" + y;
|
|
wallv_go.transform.position = new Vector3(-0.5f + x, 0f + y, -1);
|
|
wallv_go.transform.localScale = new Vector3(0.1f, 1f, 1);
|
|
wallv_go.transform.SetParent(this.transform, true);
|
|
|
|
wallv_go.AddComponent<SpriteRenderer>();
|
|
OnWallUpdated(wall, wallv_go);
|
|
}
|
|
|
|
Stage.OnWallChanged = (Wall wall) =>
|
|
{
|
|
if(wall.Orientation == WallOrientation.Horizontal)
|
|
{
|
|
var go = HorizontalWallsGameObjects[wall.X, wall.Y];
|
|
OnWallUpdated(wall, go);
|
|
}
|
|
|
|
if (wall.Orientation == WallOrientation.Vertical)
|
|
{
|
|
var go = VerticalWallsGameObjects[wall.X, wall.Y];
|
|
OnWallUpdated(wall, go);
|
|
}
|
|
};
|
|
}
|
|
|
|
float randomizeTileTimer = 2f;
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (Stage == null)
|
|
return;
|
|
|
|
randomizeTileTimer -= Time.deltaTime;
|
|
|
|
if(randomizeTileTimer < 0f)
|
|
{
|
|
if(ShouldRandomize)
|
|
Stage.Randomize();
|
|
randomizeTileTimer += 2f;
|
|
}
|
|
}
|
|
|
|
public void FocusCurrentStage()
|
|
{
|
|
Stage = Train.GetWagon(FocusedWagon).GetDeck(FocusedLevel);
|
|
}
|
|
|
|
private void OnTileUpdated(Tile tile, GameObject go)
|
|
{
|
|
Debug.Log($"Updating tile {tile.X},{tile.Y}.");
|
|
if (tile.Type == TileType.Empty)
|
|
{
|
|
go.GetComponent<SpriteRenderer>().sprite = null;
|
|
return;
|
|
}
|
|
|
|
if (tile.Type == TileType.Floor)
|
|
{
|
|
go.GetComponent<SpriteRenderer>().sprite = FloorSprite;
|
|
return;
|
|
}
|
|
|
|
throw new System.NotImplementedException($"Tile type {tile.Type} not draw implemented");
|
|
}
|
|
|
|
private void OnWallUpdated(Wall wall, GameObject go)
|
|
{
|
|
Debug.Log($"Updating wall {wall.X},{wall.Y}.");
|
|
|
|
if (wall.Type == WallType.Empty)
|
|
{
|
|
go.GetComponent<SpriteRenderer>().sprite = null;
|
|
return;
|
|
}
|
|
|
|
if (wall.Type == WallType.Full)
|
|
{
|
|
go.GetComponent<SpriteRenderer>().sprite = WallSprite;
|
|
return;
|
|
}
|
|
|
|
throw new System.NotImplementedException($"Wall type {wall.Type} not draw implemented");
|
|
}
|
|
}
|