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