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