]> git.lizzy.rs Git - SuperMouseAdventure.git/blob - 2DGame/Assets/Scripts/Maus/MouseController.cs
Maus kann nicht mehr am Pilz haengen bleiben
[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     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     PowerUps powerUps;
21
22     public bool isFacingLeft;
23     
24     private bool isShooting;
25
26     [SerializeField]
27     GameObject bullet;
28     [SerializeField]
29     Transform bulletSpawnPos;
30     [SerializeField]
31     private float shootDelay = 0.5f;
32
33     Checkpoint checkpoint;
34
35     // Start is called before the first frame update
36     void Start()
37     {   
38         //Hier wird der Rigidbody initialisiert
39         rb = GetComponent<Rigidbody2D>();
40
41         powerUps = GetComponent<PowerUps>();
42
43         checkpoint = GetComponent<Checkpoint>();
44
45         transform.position = checkpoint.CheckPoints[3].transform.position;
46     }
47
48     // Update is called once per frame
49     void Update()
50     {
51         if(isGrounded == true && Input.GetButtonDown("Jump"))
52         {
53             isJumping = true;
54             jumpTimeCounter = jumptime;
55             rb.velocity = Vector2.up * jumpForce;
56         }
57
58         if (Input.GetButton("Jump") && isJumping == true)
59         {
60             if(jumpTimeCounter > 0)
61             {
62                 rb.velocity = Vector2.up * jumpForce;
63                 jumpTimeCounter -= Time.deltaTime;
64                 FindObjectOfType<AudioManager>().Play("sprung");
65             }
66         }
67
68         if (Input.GetButtonUp("Jump"))
69         {
70             isJumping = false;
71         }
72
73         if (rb.velocity.x < 0)
74         {
75             transform.localScale = new Vector3(-1, 1, 1);
76             isFacingLeft = true;
77         }
78         else if (rb.velocity.x > 0)
79         {
80             transform.localScale = new Vector3(1, 1, 1);
81             isFacingLeft = false;
82         }
83
84         if(powerUps.mouseIsGardener == true)
85         {
86             if (Input.GetButtonDown("Fire1"))
87             {
88                 if (isShooting) return;
89
90                 isShooting = true;
91
92                 GameObject b = Instantiate(bullet);
93                 b.GetComponent<Scissors>().StartShoot(isFacingLeft);
94                 b.transform.position = bulletSpawnPos.transform.position;
95
96                 Invoke("ResetShoot", shootDelay);
97             }
98         }
99
100         moveInput = Input.GetAxisRaw("Horizontal");
101
102         if(moveInput == 0)
103         {
104
105         }
106     }
107
108     void FixedUpdate() 
109     {
110         //Hier wird ein Kreis unter der Maus erzeugt, der prüft, ob die Maus den Boden berührt
111         isGrounded = Physics2D.OverlapCircle(groundcheck.position, checkRadius, whatIsGround);
112
113         //Wenn a und d oder Pfeiltaste links und rechts gedrückt werden, ist der Wert von moveInput -1 oder 1;
114         rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
115     } 
116
117     void ResetShoot()
118     {
119         isShooting = false;
120     }
121 }