]> git.lizzy.rs Git - SuperMouseAdventure.git/blobdiff - 2DGame/Assets/Scripts/Maus/MouseController.cs
Komplexeres Physiksystem und Knockback
[SuperMouseAdventure.git] / 2DGame / Assets / Scripts / Maus / MouseController.cs
index b3f86a6a8880a20a50e86b42b4e054838eb44a32..b09c44fe978be5319d42ca71308a7ca1622d8e6f 100644 (file)
@@ -1,10 +1,11 @@
 //Mit diesem Script kann man die Maus steuern.
+using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 public class MouseController : MonoBehaviour
 {
-    public Rigidbody2D rb;
+    Rigidbody2D rb;
     public float speed;
     public float jumpForce;
     private float jumpTimeCounter;
@@ -17,17 +18,12 @@ public class MouseController : MonoBehaviour
     public float checkRadius;
     public LayerMask whatIsGround;
 
-    private int extraJumps;
-    public int extraJumpsValue;
-
     PowerUps powerUps;
 
     public bool isFacingLeft;
-    
-    private Animation anim;
+
     private bool isShooting;
 
-    private Object bulletRef;
     [SerializeField]
     GameObject bullet;
     [SerializeField]
@@ -38,55 +34,15 @@ public class MouseController : MonoBehaviour
     // Start is called before the first frame update
     void Start()
     {
-        extraJumps = extraJumpsValue;
-        
         //Hier wird der Rigidbody initialisiert
         rb = GetComponent<Rigidbody2D>();
 
-        anim = GetComponent<Animation>();
-
         powerUps = GetComponent<PowerUps>();
     }
 
     // Update is called once per frame
     void Update()
     {
-        if(isGrounded == true)
-        {
-            extraJumps = extraJumpsValue;
-        }
-
-        if(isGrounded == true && Input.GetButtonDown("Jump"))
-        {
-            isJumping = true;
-            jumpTimeCounter = jumptime;
-            rb.velocity = Vector2.up * jumpForce;
-        }
-
-        if (Input.GetButton("Jump") && isJumping == true && extraJumps > 0)
-        {
-            if(jumpTimeCounter > 0)
-            {
-                rb.velocity = Vector2.up * jumpForce;
-                jumpTimeCounter -= Time.deltaTime;
-                FindObjectOfType<AudioManager>().Play("sprung");
-            }
-            extraJumps--;
-        }
-        else if (Input.GetButton("Jump") && extraJumps == 0)
-        {
-            if (jumpTimeCounter > 0)
-            {
-                rb.velocity = Vector2.up * jumpForce;
-                jumpTimeCounter -= Time.deltaTime;
-                FindObjectOfType<AudioManager>().Play("sprung");
-            }
-            else
-            {
-                isJumping = false;
-            }
-        }
-
         if (Input.GetButtonUp("Jump"))
         {
             isJumping = false;
@@ -103,7 +59,7 @@ public class MouseController : MonoBehaviour
             isFacingLeft = false;
         }
 
-        if(powerUps.mouseIsGardener == true)
+        if (powerUps.mouseIsGardener == true)
         {
             if (Input.GetButtonDown("Fire1"))
             {
@@ -118,16 +74,75 @@ public class MouseController : MonoBehaviour
                 Invoke("ResetShoot", shootDelay);
             }
         }
+
+        moveInput = Input.GetAxisRaw("Horizontal");
     }
 
-    void FixedUpdate() 
+    bool HasReachedTV(float acceleration)
+    {
+               if (acceleration < 0)
+               {
+                       if (rb.velocity.x <= -speed)
+                               return true;
+               }
+               else if (acceleration > 0)
+               {
+                       if (rb.velocity.x >= speed)
+                               return true;
+               }
+
+               return false;
+       }
+
+    void FixedUpdate()
     {
-        //Hier wird ein Kreis unter der Maus erzeugt, der prüft, ob die Maus den Boden berührt
+                //Hier wird ein Kreis unter der Maus erzeugt, der prüft, ob die Maus den Boden berührt
         isGrounded = Physics2D.OverlapCircle(groundcheck.position, checkRadius, whatIsGround);
 
-        //Wenn a und d oder Pfeiltaste links und rechts gedrückt werden, ist der Wert von moveInput -1 oder 1;
-        moveInput = Input.GetAxisRaw("Horizontal");
-        rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
+               //Wenn a und d oder Pfeiltaste links und rechts gedrückt werden, ist der Wert von moveInput -1 oder 1;
+
+               float acceleration = speed;
+
+               if (moveInput != 0)
+               {
+                       acceleration *= moveInput * 4;
+               }
+               else if (isGrounded)
+               {
+                       acceleration *= -Math.Sign(rb.velocity.x) * 16;
+               }
+               else
+               {
+                       acceleration = 0;
+               }
+
+               if (! HasReachedTV(acceleration))
+               {
+                       int oldSign = Math.Sign(rb.velocity.x);
+                       rb.velocity += new Vector2(acceleration * Time.fixedDeltaTime, 0);
+
+                       if (HasReachedTV(acceleration))
+                               rb.velocity = new Vector2(Math.Sign(rb.velocity.x) * speed, rb.velocity.y);
+                       if (oldSign == -Math.Sign(rb.velocity.x))
+                               rb.velocity = new Vector2(0, rb.velocity.y);
+               }
+
+        if (isGrounded == true && Input.GetButtonDown("Jump"))
+        {
+            isJumping = true;
+            jumpTimeCounter = jumptime;
+            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
+        }
+
+        if (Input.GetButton("Jump") && isJumping == true)
+        {
+            if (jumpTimeCounter > 0)
+            {
+                rb.velocity = new Vector2(rb.velocity.x, jumpForce);
+                jumpTimeCounter -= Time.fixedDeltaTime;
+                FindObjectOfType<AudioManager>().Play("sprung");
+            }
+        }
     }
 
     void ResetShoot()