0

Sorry if the question seems vague but I am creating a turn based, 2D roguelike game for coding practice and want the player to be able to move twice while the enemy moves once when the user clicks a button. I have tried using multiple booleans but to no avail.

The players turn is an integer which once it reaches 0, it becomes the enemies turn. The code for when the user clicks the button:

public void ButtonClicked()
{
    GameManager.instance.playersTurn = 2;
}

the movement code inside my player manager(this is where the error lies)

void Update () {

    if (0 > GameManager.instance.playersTurn)
        return;

    horizontal = (int)Input.GetAxisRaw ("Horizontal");
    vertical = (int)Input.GetAxisRaw ("Vertical");
    //dont allow vertical movement
    if (horizontal != 0)
        vertical = 0;

        //if a key is pressed:
        if (vertical != 0 || horizontal != 0) 
            AttemptMove<Wall> (horizontal, vertical);

    }

protected override void AttemptMove <T>(int xDir, int yDir)
{
        food--;
        foodText.text = "Food: " + food;
        base.AttemptMove<T> (xDir, yDir);

        CheckIfGameOver ();
    GameManager.instance.playersTurn--;

}

the GameManager.instance.playersTurn--; decrements until zero even if I have only moved once. How can I decrement it once per movement? I think the problem is that this is being called in update. Any pointers will be a great help, thanks guys.

Here is the other code from my parent class in case the error might lie here:

protected bool Move(int xDir, int yDir, out RaycastHit2D hit)
{
    Vector2 start = transform.position;
    Vector2 end = start + new Vector2 (xDir, yDir);
    boxCollider.enabled = false;
    hit = Physics2D.Linecast (start, end, blockingLayer);
    boxCollider.enabled= true;

    if (hit.transform == null) {

        StartCoroutine (SmoothMovement (end));
        return true;
    }
    return false;

}

protected virtual void AttemptMove<T> (int xDir, int yDir)
    where T: Component
{
    RaycastHit2D hit; 
    bool canMove= Move (xDir, yDir, out hit);
    if (hit.transform == null)
        return;

    T hitComponent = hit.transform.GetComponent<T> ();

    if (!canMove && hitComponent != null)
        OnCantMove (hitComponent); 
}

protected IEnumerator SmoothMovement(Vector3 end)
{
    float sqrRemainingDistance = (transform.position - end).sqrMagnitude;
    while (sqrRemainingDistance > float.Epsilon) {

        Vector3 newPosition = Vector3.MoveTowards (rb2d.position, end, inverseMoveTime * Time.deltaTime); 
        rb2d.MovePosition (newPosition);
        sqrRemainingDistance = (transform.position - end).sqrMagnitude;
        yield return null;
    }


}
Damian
  • 19
  • 6
  • Is there any reason why you want to have it in the update? – I.B Mar 15 '17 at 21:21
  • no not particularly but I was following a video series and thats how they did it. @CNuts – Damian Mar 15 '17 at 21:30
  • Okay so you're following the 2D Roguelike tutorial of unity but you wanted to change it so the player can have 2 turns? – I.B Mar 15 '17 at 21:36
  • Yes that is exactly what I am trying to do. @CNuts – Damian Mar 15 '17 at 21:39
  • A few observations but no solution: 1) When you check for 0>playersTurn then you return when playersTurn becomes negative and not when it becomes zero 2) the problem isn't where you say it is, probably. I suspect it's where the calling code decides whose turn it is. 3) Decrement always subtracts one. Always. If playersTurn is going to zero then it is being decremented more than once. – Ray Fischer Mar 15 '17 at 21:42
  • Thanks for your input @RayFischer. 1) I changed it to zero now. 2) It can only become the enemies turn once playerTurn = 0. 3) It is being decremented more than once but I cannot for the life of me find why it is being decremented twice. – Damian Mar 15 '17 at 21:47
  • Let me clarify. If it is being decremented twice then the Update function is being called twice, or it's being decremented in more than one place, or it's being set to zero someplace. This feature of Visual Studio may be helpful: http://stackoverflow.com/questions/6333380/how-do-i-run-until-this-variable-changes-when-debugging – Ray Fischer Mar 15 '17 at 21:53
  • If you put `Debug.Log(GameManager.instance.playersTurn)` before its decremented and you only try to move once what does it give? – I.B Mar 15 '17 at 23:20
  • Before I decrement it, it gives me a value of 1 which is correct. Then I click the button(therefore playersTurn = 2) and once I move it immediately goes down to 0. @CNuts – Damian Mar 16 '17 at 00:26

0 Answers0