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