Skip to content
Snippets Groups Projects
PlayerMovement.cs 3.29 KiB
Newer Older
/*
 *      Script par Valentin Bloch      
 *
 */

public class PlayerMovement : MonoBehaviour
{
    public float speed;
    private Rigidbody2D myRigidbody;
    private Vector2 moveDirection;
    public PlayerState currentState;
v.bloch's avatar
v.bloch committed
    public int stage = 0;
    public GameObject player;
v.bloch's avatar
v.bloch committed
    public bool[] ispassed;
v.bloch's avatar
v.bloch committed

    // Start is called before the first frame update
    void Start()
    {
        animator = GetComponent<Animator>();
        myRigidbody = GetComponent<Rigidbody2D>();

        animator.SetFloat("moveX", 0);
        animator.SetFloat("moveY", -1);

        ispassed = new bool[] { false, false, false, false };
    }

    // Update is called once per frame
    void Update()
    {
        if(currentState == PlayerState.walk)
        {
            UpdateAnimationAndMove();
        }

v.bloch's avatar
v.bloch committed
        Vector3 pos = Camera.main.WorldToViewportPoint(transform.position);
        pos.x = Mathf.Clamp01(pos.x);
        pos.y = Mathf.Clamp01(pos.y);
        transform.position = Camera.main.ViewportToWorldPoint(pos);
v.bloch's avatar
v.bloch committed

        gameIntelligence();
    }

    private void FixedUpdate()
    {
        moveDirection.Normalize();
        myRigidbody.velocity = new Vector2(moveDirection.x * speed, moveDirection.y * speed);
    }

    private void ProcessInputs()
    {
        float moveX = Input.GetAxisRaw("Horizontal");
        float moveY = Input.GetAxisRaw("Vertical");
        moveDirection = new Vector2(moveX, moveY);
    }

    void UpdateAnimationAndMove()
    {
        if (moveDirection != Vector2.zero)
        {

            animator.SetFloat("moveX", moveDirection.x);
            animator.SetFloat("moveY", moveDirection.y);

            animator.SetBool("moving", true);
        }
        else
        {
            animator.SetBool("moving", false);
        }
    }

    void gameIntelligence()
    {
v.bloch's avatar
v.bloch committed
        if (this.GetComponent<PlayerNature>().currentNature >= 100 && stage == 0)
        {
            GenerateMap.instance.generateBoutique(0, 1, "chaussure");
            stage++;
        }
v.bloch's avatar
v.bloch committed
        else if (stage == 1 && ispassed[0] == true)
v.bloch's avatar
v.bloch committed
        {
            GenerateMap.instance.generateUsine(1, 1);
            stage++;
        }
        else if (this.GetComponent<PlayerNature>().currentNature >= 100 && stage == 2)
        {
            GenerateMap.instance.generateBoutique(0, 2, "baton");
            stage++;
        }
v.bloch's avatar
v.bloch committed
        else if (stage == 3 && ispassed[1] == true)
v.bloch's avatar
v.bloch committed
        {
            GenerateMap.instance.generateUsine(1, 2);
            stage++;
        }
        else if (this.GetComponent<PlayerNature>().currentNature >= 100 && stage == 4)
        {
            GenerateMap.instance.generateBoutique(2, 0, "rechaud");
            stage++;
        }
v.bloch's avatar
v.bloch committed
        else if (stage == 5 && ispassed[2] == true)
v.bloch's avatar
v.bloch committed
        {
            GenerateMap.instance.generateUsine(2, 1);
            stage++;
        }
        else if (this.GetComponent<PlayerNature>().currentNature >= 100 && stage == 6)
        {
            GenerateMap.instance.generateBoutique(0, 0, "tente");
            stage++;
        }
v.bloch's avatar
v.bloch committed
        else if (stage == 7 && ispassed[3] == true)
v.bloch's avatar
v.bloch committed
        {
            GenerateMap.instance.generateUsine(1, 0);
            stage++;
        }
v.bloch's avatar
v.bloch committed
    }