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