]> git.lizzy.rs Git - SuperMouseAdventure.git/blob - 2DGame/Assets/Scripts/Maus/MouseController.cs
b3f86a6a8880a20a50e86b42b4e054838eb44a32
[SuperMouseAdventure.git] / 2DGame / Assets / Scripts / Maus / MouseController.cs
1 //Mit diesem Script kann man die Maus steuern.
2 using System.Collections;
3 using System.Collections.Generic;
4 using UnityEngine;
5 public class MouseController : MonoBehaviour
6 {
7     public Rigidbody2D rb;
8     public float speed;
9     public float jumpForce;
10     private float jumpTimeCounter;
11     public float jumptime;
12     private bool isJumping;
13     private float moveInput;
14
15     private bool isGrounded;
16     public Transform groundcheck;
17     public float checkRadius;
18     public LayerMask whatIsGround;
19
20     private int extraJumps;
21     public int extraJumpsValue;
22
23     PowerUps powerUps;
24
25     public bool isFacingLeft;
26     
27     private Animation anim;
28     private bool isShooting;
29
30     private Object bulletRef;
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         extraJumps = extraJumpsValue;
42         
43         //Hier wird der Rigidbody initialisiert
44         rb = GetComponent<Rigidbody2D>();
45
46         anim = GetComponent<Animation>();
47
48         powerUps = GetComponent<PowerUps>();
49     }
50
51     // Update is called once per frame
52     void Update()
53     {
54         if(isGrounded == true)
55         {
56             extraJumps = extraJumpsValue;
57         }
58
59         if(isGrounded == true && Input.GetButtonDown("Jump"))
60         {
61             isJumping = true;
62             jumpTimeCounter = jumptime;
63             rb.velocity = Vector2.up * jumpForce;
64         }
65
66         if (Input.GetButton("Jump") && isJumping == true && extraJumps > 0)
67         {
68             if(jumpTimeCounter > 0)
69             {
70                 rb.velocity = Vector2.up * jumpForce;
71                 jumpTimeCounter -= Time.deltaTime;
72                 FindObjectOfType<AudioManager>().Play("sprung");
73             }
74             extraJumps--;
75         }
76         else if (Input.GetButton("Jump") && extraJumps == 0)
77         {
78             if (jumpTimeCounter > 0)
79             {
80                 rb.velocity = Vector2.up * jumpForce;
81                 jumpTimeCounter -= Time.deltaTime;
82                 FindObjectOfType<AudioManager>().Play("sprung");
83             }
84             else
85             {
86                 isJumping = false;
87             }
88         }
89
90         if (Input.GetButtonUp("Jump"))
91         {
92             isJumping = false;
93         }
94
95         if (rb.velocity.x < 0)
96         {
97             transform.localScale = new Vector3(-1, 1, 1);
98             isFacingLeft = true;
99         }
100         else if (rb.velocity.x > 0)
101         {
102             transform.localScale = new Vector3(1, 1, 1);
103             isFacingLeft = false;
104         }
105
106         if(powerUps.mouseIsGardener == true)
107         {
108             if (Input.GetButtonDown("Fire1"))
109             {
110                 if (isShooting) return;
111
112                 isShooting = true;
113
114                 GameObject b = Instantiate(bullet);
115                 b.GetComponent<Scissors>().StartShoot(isFacingLeft);
116                 b.transform.position = bulletSpawnPos.transform.position;
117
118                 Invoke("ResetShoot", shootDelay);
119             }
120         }
121     }
122
123     void FixedUpdate() 
124     {
125         //Hier wird ein Kreis unter der Maus erzeugt, der prüft, ob die Maus den Boden berührt
126         isGrounded = Physics2D.OverlapCircle(groundcheck.position, checkRadius, whatIsGround);
127
128         //Wenn a und d oder Pfeiltaste links und rechts gedrückt werden, ist der Wert von moveInput -1 oder 1;
129         moveInput = Input.GetAxisRaw("Horizontal");
130         rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
131     }
132
133     void ResetShoot()
134     {
135         isShooting = false;
136     }
137 }