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