How to make a turn-based system where each turn has the same duration ?
Hello everyone !
I'm trying to make a game based on the 2D Roguelike Tutorial to learn Unity. But I'm stuck since I tried to add attack animation (which is not in the tutorial)
I don't fully understand coroutines, so I manage turns like this :
(GameManager.cs)
void Update () {
if (gameOver || roomChange) { return; }
switch (TurnSystem) {
case GameState.PlayerTurn:
if (player.Action()) {
TurnSystem = GameState.PlayerAnim;
}
break;
case GameState.PlayerAnim:
waitTimeEOT += Time.deltaTime;
if (waitTimeEOT >= turnDelay) {
TurnSystem = GameState.EnemyTurn;
waitTimeEOT = 0f;
}
break;
case GameState.EnemyTurn:
for (int i = 0; i < boardScript.entitiesInCurrentRoom.Count; i++) {
if (boardScript.entitiesInCurrentRoom[i].GetComponent<Enemy>().life > 0) {
boardScript.entitiesInCurrentRoom[i].GetComponent<Enemy>().Action(player.position);
}
}
TurnSystem = GameState.EnemyAnim;
break;
case GameState.EnemyAnim:
waitTimeEOT += Time.deltaTime;
if (waitTimeEOT >= turnDelay) {
TurnSystem = GameState.PlayerTurn;
waitTimeEOT = 0f;
}
break;
}
}
I have animations for the player attack and taking damage, set up in Mecanim to last 0.2s (turnDelay). (For exemple, player attack animation has 7frames, so "Samples" property is set at 35 frames per seconds) Move animation is not like in the tutorial because of the co-routine (I still doesn't understand it, so I tried to do without): Instead, I use the update function of my player script (enemy script have the same code) :
(Player.cs)
public void Move(Coord targetPosition) {
this.animFinish = false;
this.position = targetPosition;
}
void Update() {
if (GameManager.instance.gameOver || GameManager.instance.roomChange) { return; }
if (!this.animFinish && this.transform.position != CoordToTransform(targetPos)) {
this.transform.position = Vector3.MoveTowards(this.transform.position, CoordToTransform(targetPos), this.speed * Time.deltaTime);
if (this.transform.position == CoordToTransform(targetPos)) {
this.animFinish = true;
}
}
}
This is certainly a terrible way to do this. But it's kinda work... ¯\_(ツ)_/¯
The main problem is the fact that turns where player and enemies move lasts longer than turns with fights. If I keep input key down, the player stop between moves. In fight, the turns run without pause.
So my question is : How do I make a turn-based system where each turn has the same duration ?
Even if it's need some co-routines, I'm still learning so I'll try.
Thanks in advance for your help.