1
0
Fork 0
Bildschirmflausch-LD41/Assets/Scripts/GameController.cs

345 lines
11 KiB
C#
Raw Normal View History

using Assets.Scripts.Entities;
using System.Collections;
2018-04-21 12:22:17 +02:00
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour {
private Room start;
private Room finish;
2018-04-23 14:59:34 +02:00
public enum EndedCause {
WIN, DIED
}
// Enemy Prefabs
[Header("Enemys")]
[SerializeField]
GameObject scorpion;
2018-04-23 19:32:36 +02:00
[SerializeField]
GameObject bug;
[Space(10)]
2018-04-22 02:23:13 +02:00
// Generation Settings
[Header("Tile Prefabs")]
[SerializeField]
GameObject BorderOuter;
[SerializeField]
GameObject BorderInner;
[SerializeField]
GameObject BorderSingle;
[SerializeField]
2018-04-23 15:54:31 +02:00
GameObject Ground0;
[SerializeField]
GameObject Ground1;
[SerializeField]
GameObject Ground2;
[SerializeField]
GameObject Ground3;
2018-04-22 02:23:13 +02:00
[SerializeField]
2018-04-23 14:33:27 +02:00
GameObject DoorInner;
[SerializeField]
GameObject DoorOuter;
2018-04-22 02:23:13 +02:00
[SerializeField]
GameObject Rock;
[SerializeField]
GameObject RockL;
[SerializeField]
GameObject RockU;
[SerializeField]
GameObject RockR;
[SerializeField]
GameObject RockD;
[SerializeField]
GameObject RockLU;
[SerializeField]
GameObject RockLR;
[SerializeField]
GameObject RockLD;
[SerializeField]
GameObject RockUR;
[SerializeField]
GameObject RockUD;
[SerializeField]
GameObject RockRD;
[SerializeField]
GameObject RockLURD;
[SerializeField]
GameObject RockLUD;
[SerializeField]
GameObject RockLUR;
[SerializeField]
2018-04-23 19:39:37 +02:00
GameObject RockURD;
2018-04-22 02:23:13 +02:00
[SerializeField]
2018-04-23 19:39:37 +02:00
GameObject RockLRD;
[SerializeField]
GameObject Flag;
2018-04-22 02:23:13 +02:00
private Dictionary<GenerationProcessor.ExtendedTileType, GameObject> genPrefabs;
private Dictionary<Enemy.Enemys, GameObject> enemyPrefabs;
2018-04-22 19:07:50 +02:00
[Space(10)]
[Header("References")]
[SerializeField]
private GameObject playerPrefab;
2018-04-22 00:22:56 +02:00
[SerializeField]
private GameObject ui;
[SerializeField]
private GameObject cam;
[SerializeField]
private GameObject mapRoot;
private bool engineInitDone;
private Player player;
public static GameController instance;
public GameController() {
instance = this;
}
2018-04-21 13:10:01 +02:00
public enum GameState { UNSET, INIT, STARTING, RUNNING, ENDED };
2018-04-23 14:59:34 +02:00
private EndedCause endCause = EndedCause.DIED;
2018-04-21 13:10:01 +02:00
private GameState state = GameState.UNSET;
2018-04-21 12:22:17 +02:00
// Use this for initialization
void Start () {
genPrefabs = new Dictionary<GenerationProcessor.ExtendedTileType, GameObject> {
{ GenerationProcessor.ExtendedTileType.BorderOuter, BorderOuter },
{ GenerationProcessor.ExtendedTileType.BorderInner, BorderInner },
{ GenerationProcessor.ExtendedTileType.BorderSingle, BorderSingle },
{ GenerationProcessor.ExtendedTileType.Rock, Rock },
{ GenerationProcessor.ExtendedTileType.RockL, RockL },
{ GenerationProcessor.ExtendedTileType.RockU, RockU },
{ GenerationProcessor.ExtendedTileType.RockR, RockR },
{ GenerationProcessor.ExtendedTileType.RockD, RockD },
{ GenerationProcessor.ExtendedTileType.RockLU, RockLU },
{ GenerationProcessor.ExtendedTileType.RockLR, RockLR },
{ GenerationProcessor.ExtendedTileType.RockLD, RockLD },
{ GenerationProcessor.ExtendedTileType.RockLURD, RockLURD },
{ GenerationProcessor.ExtendedTileType.RockRD, RockRD },
{ GenerationProcessor.ExtendedTileType.RockUR, RockUR },
{ GenerationProcessor.ExtendedTileType.RockUD, RockUD },
{ GenerationProcessor.ExtendedTileType.RockLUD, RockLUD },
{ GenerationProcessor.ExtendedTileType.RockLUR, RockLUR },
{ GenerationProcessor.ExtendedTileType.RockURD, RockURD },
2018-04-23 17:21:41 +02:00
{ GenerationProcessor.ExtendedTileType.RockLRD, RockLRD },
{ GenerationProcessor.ExtendedTileType.Ground0, Ground0 },
{ GenerationProcessor.ExtendedTileType.Ground1, Ground1 },
{ GenerationProcessor.ExtendedTileType.Ground2, Ground2 },
{ GenerationProcessor.ExtendedTileType.Ground3, Ground3 },
2018-04-23 19:39:37 +02:00
{ GenerationProcessor.ExtendedTileType.DoorInner, DoorInner },
{ GenerationProcessor.ExtendedTileType.DoorOuter, DoorOuter },
{ GenerationProcessor.ExtendedTileType.Flag, Flag }
};
enemyPrefabs = new Dictionary<Enemy.Enemys, GameObject> {
2018-04-23 19:32:36 +02:00
{ Enemy.Enemys.SCORPION, scorpion },
{ Enemy.Enemys.BUG, bug }
};
}
// Update is called once per frame
void Update() {
if ( !engineInitDone ) {
engineInitDone = true;
Debug.Log("First Frame");
ChangeState(GameState.INIT);
}
}
2018-04-21 13:10:01 +02:00
2018-04-22 00:22:56 +02:00
public void ChangeState(GameState nextState) {
if ( nextState != state ) {
2018-04-21 13:10:01 +02:00
state = nextState;
StateLogic(nextState);
}
}
void StateLogic(GameState nstate) {
switch ( nstate ) {
2018-04-21 13:10:01 +02:00
case GameState.INIT:
Init();
ChangeState(GameState.STARTING);
2018-04-21 13:10:01 +02:00
break;
case GameState.STARTING:
Starting();
ChangeState(GameState.RUNNING);
2018-04-21 13:10:01 +02:00
break;
case GameState.RUNNING:
Running();
break;
case GameState.ENDED:
Ended();
break;
default:
print("Wrong GameState for StateLogic() call!");
break;
}
}
private void Init() {
// Generation
DungeonGenerator dg = new DungeonGenerator();
GenerationProcessor gp = new GenerationProcessor(genPrefabs);
dg.Generate();
2018-04-23 14:33:27 +02:00
// Start room
GameObject goStart = gp.ProcessRoom(dg.start.tiles);
goStart.name = "Start";
List<Transform> lt = new List<Transform>(goStart.GetComponentsInChildren<Transform>());
GameObject doorRoot = new GameObject();
doorRoot.name = "Doors";
doorRoot.transform.SetParent(goStart.transform);
start = goStart.AddComponent<Room>();
start.SetCenter(dg.start.GetCenter());
lt = lt.FindAll(x => x.tag == "door");
lt.ForEach(x => {
x.SetParent(doorRoot.transform);
x.gameObject.GetComponent<Door>().SetParent(start);
});
start.SetDoorsRootObject(doorRoot);
// Spawnpoint
GameObject spawnpointRoot = new GameObject();
spawnpointRoot.name = "Spawnpoints";
spawnpointRoot.transform.SetParent(goStart.transform);
spawnpointRoot.transform.position = new Vector3(dg.start.roomPosition.x, dg.start.roomPosition.y, 0);
GameObject spawn = new GameObject();
spawn.transform.SetParent(spawnpointRoot.transform);
2018-04-23 02:23:06 +02:00
spawn.transform.position = new Vector3(dg.start.GetCenter().x, dg.start.GetCenter().y, 0);
start.SetSpawnPointsRootObject(spawnpointRoot);
start.Reload();
start.transform.SetParent(mapRoot.transform);
// Finish room
GameObject goFinish = gp.ProcessRoom(dg.end.tiles);
goFinish.name = "Finish";
List<Transform> ltf = new List<Transform>(goFinish.GetComponentsInChildren<Transform>());
GameObject doorRootf = new GameObject();
doorRootf.name = "Doors";
doorRootf.transform.SetParent(goFinish.transform);
ltf = ltf.FindAll(x => x.tag == "door");
finish = goFinish.AddComponent<Room>();
finish.SetCenter(dg.end.GetCenter());
2018-04-23 14:59:34 +02:00
ltf.ForEach(x => {
x.SetParent(doorRootf.transform);
x.gameObject.GetComponent<Door>().SetParent(finish);
});
finish.SetDoorsRootObject(doorRootf);
finish.Reload();
finish.transform.SetParent(mapRoot.transform);
2018-04-23 19:39:37 +02:00
gp.CreateGOFromType(finish.GetCenter(), 0, Room.TileType.DOOR, GenerationProcessor.ExtendedTileType.Flag, goFinish);
2018-04-23 19:39:37 +02:00
// Other Rooms
foreach (GenRoom gr in dg.rooms) {
GameObject groom = gp.ProcessRoom(gr.tiles);
List<Transform> ltg = new List<Transform>(groom.GetComponentsInChildren<Transform>());
// Doors
GameObject doorRootg = new GameObject();
doorRootg.name = "Doors";
doorRootg.transform.SetParent(groom.transform);
Room grom = groom.AddComponent<Room>();
grom.SetCenter(gr.GetCenter());
2018-04-23 14:59:34 +02:00
ltg = ltg.FindAll(x => x.tag == "door");
ltg.ForEach(x => {
x.SetParent(doorRootg.transform);
x.gameObject.GetComponent<Door>().SetParent(grom);
});
// Spawnpoints
GameObject tSpawnpointRoot = new GameObject();
tSpawnpointRoot.name = "Spawnpoints";
tSpawnpointRoot.transform.SetParent(groom.transform);
tSpawnpointRoot.transform.position = new Vector3(gr.roomPosition.x, gr.roomPosition.y, 0);
foreach(Vector2Int v in gr.spawnpoints) {
GameObject tspawn = new GameObject();
tspawn.transform.SetParent(tSpawnpointRoot.transform);
tspawn.transform.position = new Vector3(v.x, v.y, 0); // is this the center or the top left corner of a block?
}
grom.SetSpawnPointsRootObject(tSpawnpointRoot);
grom.SetDoorsRootObject(doorRootg);
grom.Reload();
DungeonGenerator.GenerateObjective(grom);
groom.transform.SetParent(mapRoot.transform);
}
2018-04-22 18:17:44 +02:00
// Hallways
2018-04-22 18:17:44 +02:00
GameObject goHallways = gp.ProcessRoom(dg.path.tiles);
goHallways.name = "Hallways";
2018-04-22 18:17:44 +02:00
goHallways.AddComponent<Room>();
goHallways.transform.SetParent(mapRoot.transform);
}
private void Starting() {
StartObjective goal = new StartObjective(start, playerPrefab);
start.SetObjective(goal);
start.OnPlayerEnter(player);
2018-04-23 22:01:07 +02:00
player = goal.GetPlayer();
cam.GetComponent<AudioControl>().LevelBgm();
if ( player != null ) {
cam.GetComponent<CameraControl>().SetFollow(player.gameObject);
2018-04-23 01:25:50 +02:00
GetUI().InitHealthController(player);
GetUI().InitBrakeController(player);
} else {
Debug.Log("No Player spawned!");
2018-04-23 22:01:07 +02:00
}
2018-04-23 14:59:34 +02:00
finish.SetObjective(new FinishObjective(finish));
2018-04-23 22:01:07 +02:00
2018-04-21 13:10:01 +02:00
}
private void Running() {
2018-04-21 13:10:01 +02:00
}
private void Ended() {
2018-04-22 00:22:56 +02:00
Debug.Log("Game ended");
//Time.timeScale = 0;
if ( ui != null ) {
2018-04-23 14:59:34 +02:00
Debug.Log("show end UI");
if(endCause == EndedCause.DIED) {
2018-04-23 15:06:50 +02:00
cam.GetComponent<AudioControl>().GameOverBgm();
2018-04-23 14:59:34 +02:00
ui.GetComponent<UIController>().ShowGameOverUI();
} else if(endCause == EndedCause.WIN) {
2018-04-23 19:11:12 +02:00
//cam.GetComponent<AudioControl>().SfxPlay(2);
player.InflictDamage(int.MaxValue/2);
2018-04-23 14:59:34 +02:00
ui.GetComponent<UIController>().ShowWinUI();
}
2018-04-22 00:22:56 +02:00
} else {
Debug.Log("No UI specified");
2018-04-22 00:22:56 +02:00
}
2018-04-21 13:10:01 +02:00
}
2018-04-23 01:47:07 +02:00
public AudioControl GetAudioControl() {
return cam.GetComponent<AudioControl>();
}
2018-04-22 19:07:50 +02:00
public UIController GetUI() {
return ui.GetComponent<UIController>();
}
2018-04-23 13:17:59 +02:00
public Dictionary<Enemy.Enemys, GameObject> GetEnemyPrefabs() {
return enemyPrefabs;
}
2018-04-23 13:17:59 +02:00
public bool GameEnded() {
return state == GameState.ENDED;
}
2018-04-23 14:59:34 +02:00
public void EndGame(EndedCause cause) {
2018-04-23 21:25:35 +02:00
if (state == GameState.ENDED)
2018-04-23 19:11:12 +02:00
return; // Already ended game
2018-04-23 14:59:34 +02:00
endCause = cause;
ChangeState(GameState.ENDED);
}
2018-04-21 12:22:17 +02:00
}