]> git.lizzy.rs Git - SuperMouseAdventure.git/blob - 2DGame/Assets/Scripts/Mouse/MouseController.cs
coyote time
[SuperMouseAdventure.git] / 2DGame / Assets / Scripts / Mouse / MouseController.cs
1 //Mit diesem Script kann man die Maus steuern.
2 using System;
3 using System.Collections;
4 using System.Collections.Generic;
5 using UnityEngine;
6
7 public class MouseController : MonoBehaviour
8 {
9     Rigidbody2D rb;
10     public float speed;
11     public float jumpForce;
12     private float jumpTimeCounter;
13     public float jumptime;
14     private bool isJumping;
15     private float moveInput;
16     public float coyoteTime;
17     private bool jumpAllowed;
18
19     [HideInInspector]
20     public bool isGrounded;
21     public Transform groundcheck;
22     public float checkRadius;
23     public LayerMask whatIsGround;
24
25     PowerUps powerUps;
26
27     public bool isFacingLeft;
28
29     private bool isShooting;
30
31     [SerializeField]
32     GameObject bullet;
33     [SerializeField]
34     Transform bulletSpawnPos;
35     [SerializeField]
36     private float shootDelay = 0.5f;
37
38     // Start is called before the first frame update
39     void Start()
40     {
41         //Hier wird der Rigidbody initialisiert
42         rb = GetComponent<Rigidbody2D>();
43
44         powerUps = GetComponent<PowerUps>();
45     }
46
47     // Update is called once per frame
48     void Update()
49     {
50         if (!isGrounded)
51         {
52             StartCoroutine(CoyoteTime());
53         }
54         else
55         {
56             jumpAllowed = true;
57         }
58
59                 if (jumpAllowed && Input.GetButtonDown("Jump"))
60         {
61             isJumping = true;
62             jumpTimeCounter = jumptime;
63             rb.velocity = new Vector2(rb.velocity.x, jumpForce);
64         }
65
66         if (Input.GetButton("Jump") && isJumping)
67         {
68             if (jumpTimeCounter > 0)
69             {
70                 rb.velocity = new Vector2(rb.velocity.x, jumpForce);
71                 jumpTimeCounter -= Time.deltaTime;
72                 FindObjectOfType<AudioManager>().Play("sprung");
73             }
74         }
75
76         if (Input.GetButtonUp("Jump"))
77         {
78             isJumping = false;
79         }
80
81         if (rb.velocity.x < 0)
82         {
83             transform.localScale = new Vector3(-1, 1, 1);
84             isFacingLeft = true;
85         }
86         else if (rb.velocity.x > 0)
87         {
88             transform.localScale = new Vector3(1, 1, 1);
89             isFacingLeft = false;
90         }
91
92         if (powerUps.mouseIsGardener == true)
93         {
94             if (Input.GetButtonDown("Fire1") && ! isShooting)
95             {
96                 isShooting = true;
97
98                 GameObject b = Instantiate(bullet);
99                 b.GetComponent<Scissors>().StartShoot(isFacingLeft, rb.velocity);
100                 b.transform.position = bulletSpawnPos.transform.position;
101
102                 Invoke("ResetShoot", shootDelay);
103             }
104         }
105
106         moveInput = Input.GetAxisRaw("Horizontal");
107     }
108
109     bool HasReachedTV(float acceleration)
110     {
111                 if (acceleration < 0)
112                 {
113                         if (rb.velocity.x <= -speed)
114                                 return true;
115                 }
116                 else if (acceleration > 0)
117                 {
118                         if (rb.velocity.x >= speed)
119                                 return true;
120                 }
121
122                 return false; 
123         }
124
125     void FixedUpdate()
126     {
127                  //Hier wird ein Kreis unter der Maus erzeugt, der prueft, ob die Maus den Boden beruehrt
128         isGrounded = Physics2D.OverlapCircle(groundcheck.position, checkRadius, whatIsGround);
129
130                 //Wenn a und d oder Pfeiltaste links und rechts gedrueckt werden, ist der Wert von moveInput -1 oder 1;
131
132                 float acceleration = speed;
133
134                 if (moveInput != 0)
135                 {
136                         acceleration *= moveInput * 4;
137                 }
138                 else if (isGrounded)
139                 {
140                         acceleration *= -Math.Sign(rb.velocity.x) * 16;
141                 }
142                 else
143                 {
144                         acceleration = 0;
145                 }
146
147                 if (! HasReachedTV(acceleration))
148                 {
149                         int oldSign = Math.Sign(rb.velocity.x);
150                         rb.velocity += new Vector2(acceleration * Time.fixedDeltaTime, 0);
151
152                         if (HasReachedTV(acceleration))
153                                 rb.velocity = new Vector2(Math.Sign(rb.velocity.x) * speed, rb.velocity.y);
154                         if (oldSign == -Math.Sign(rb.velocity.x))
155                                 rb.velocity = new Vector2(0, rb.velocity.y);
156                 }
157     }
158
159     IEnumerator CoyoteTime()
160     {
161         yield return new WaitForSeconds(coyoteTime);
162         jumpAllowed = false;
163     }
164
165     void ResetShoot()
166     {
167         isShooting = false;
168     }
169 }