Skip to content
Snippets Groups Projects
GameManager.cs 3.33 KiB
Newer Older
william's avatar
william committed
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour
{
william's avatar
william committed
    private Animator stateMachine;

    [SerializeField] private GameObject ball;
    [SerializeField] private Transform target;
    [SerializeField] private GameObject playerPrefab;
    [SerializeField] private GameObject opponentPrefab;
william's avatar
william committed

    [SerializeField] private Transform[] spawPoints;
    private float currentTime;
    private bool inTurn;
    private GameObject player;
    private PlayerController playerController;
    private GameObject npc;
    private OpponentController npcController;

    private void Start()
    {
        player = Instantiate(playerPrefab, spawPoints[0].position, spawPoints[0].rotation) as GameObject;
        npc = Instantiate(opponentPrefab, spawPoints[1].position, spawPoints[1].rotation) as GameObject;
        playerController = player.GetComponent<PlayerController>();
        npcController = npc.GetComponent<OpponentController>();
        npc.GetComponent<IAMovement>().setPlayerTransform(player.transform);
        FindObjectOfType<ForceBarController>().setPlayer(player);
        inTurn = true;
    }

    private void Update()
    {
        if (inTurn)
        {
            currentTime += Time.deltaTime;
            //Debug.Log(currentTime);
        }
        Debug.Log(1.0f/Time.deltaTime);
    }

    private void FixedUpdate()
    {
        if (currentTime >= 3)
        {
            StartCoroutine(endTurn());
        }
    }

    private IEnumerator endTurn()
    {
        inTurn = false;
        currentTime = 0;
        npcController.enabled = false;
        playerController.enabled = false;
        player.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
        npc.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
        yield return new WaitForSecondsRealtime(1);
        //do something
        StartCoroutine(setupTurn());
    }

    private IEnumerator setupTurn()
    {
        npcController.enabled = true;
        playerController.enabled = true;
        playerController.moveToPosition(spawPoints[0], spawPoints[2]);
        npcController.moveToPosition(spawPoints[3], spawPoints[1]);
        yield return new WaitForSecondsRealtime(1);
        //do something
        StartCoroutine(startTurn());
    }

    private IEnumerator startTurn()
    {
        inTurn = true;
        playerController.changeTurn();
        npcController.changeTurn();
        yield return null;
    }

    public void shoot(float force, Transform throwPoint)
william's avatar
william committed
    {
        float xdistance;
        float time = 0.8f + force;
        xdistance = target.position.x - throwPoint.position.x;
william's avatar
william committed
        float ydistance;
        ydistance = target.position.y - throwPoint.position.y;
william's avatar
william committed

        float throwAngle; 
        throwAngle = Mathf.Atan((ydistance + calculateGravity() * (time * time)) / xdistance);
        float totalVelo = xdistance / (Mathf.Cos(throwAngle) * time);

        float xVelo, yVelo;
        xVelo = totalVelo * Mathf.Cos(throwAngle);
        yVelo = totalVelo * Mathf.Sin(throwAngle);

        GameObject bulletInstance = Instantiate(ball, throwPoint.position, throwPoint.rotation) as GameObject;
william's avatar
william committed
        bulletInstance.GetComponent<Rigidbody2D>().velocity = new Vector2(xVelo, yVelo);
william's avatar
william committed
    }

    private float calculateGravity()
    {
        return Physics2D.gravity.y / -2;
    }
}