-
Notifications
You must be signed in to change notification settings - Fork 1
/
PlayerMovement.cs
183 lines (167 loc) · 8.79 KB
/
PlayerMovement.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
[Header("Player")]
[SerializeField] float runSpeed = 10f;
[SerializeField] float jumpSpeed = 5f;
[SerializeField] float direction = -1;
Vector2 moveInput;
Rigidbody2D myRigidbody;
Animator myAnimator;
CapsuleCollider2D myCapsuleCollider;
BoxCollider2D myBoxCollider;
bool invincibilityFrames;
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
myCapsuleCollider = GetComponent<CapsuleCollider2D>();
myBoxCollider = GetComponent<BoxCollider2D>();
myAnimator.SetBool("isRunning", false);
myAnimator.SetFloat("directionLR", 1);
myAnimator.SetInteger("directionUD", 0);
myAnimator.SetBool("isDead", false);
invincibilityFrames = false;
}
// Update is called once per frame
void Update()
{
Reshape();
Move();
FlipSprite();
if (gameObject.GetComponent<PlayerItems>().GetMissiles() <= 0 && myAnimator.GetInteger("missile") == 1) { Switch(); }
else if (gameObject.GetComponent<PlayerItems>().GetSuperMissiles() <= 0 && myAnimator.GetInteger("missile") == 2) { Switch(); }
else if (gameObject.GetComponent<PlayerItems>().GetPowerBombs() <= 0 && myAnimator.GetInteger("missile") == 3) { Switch(); }
RaycastHit2D hit1 = Physics2D.Raycast(transform.position, Vector2.down, 2f, LayerMask.GetMask("Ground"));
RaycastHit2D hit2 = Physics2D.Raycast(transform.position, Vector2.down, 2f, LayerMask.GetMask("Interactable"));
RaycastHit2D hit3 = Physics2D.Raycast(transform.position, Vector2.down, 2f, LayerMask.GetMask("Enemy Hit"));
if (myAnimator.GetInteger("directionUD")==-2 || myAnimator.GetInteger("directionUD") == -1||(Mathf.Abs(myRigidbody.velocity.y) < 5f&& (hit1.collider != null || hit2.collider!=null || hit3.collider != null))) { myAnimator.SetInteger("jumpDirection", 0); }
else if (myRigidbody.velocity.y > 0.1f) { myAnimator.SetInteger("jumpDirection", 1); }
else{ myAnimator.SetInteger("jumpDirection", -1); }
if (gameObject.GetComponent<PlayerItems>().GetEnergy() <= 0) { myAnimator.SetInteger("directionUD", 0); myAnimator.SetFloat("directionLR", 0f); }
}
private void OnTriggerEnter2D(Collider2D other)
{
if (Mathf.Abs(myRigidbody.velocity.y) < 1f &&(other.GetComponent<Collider2D>().gameObject.layer == LayerMask.NameToLayer("Ground") || other.GetComponent<Collider2D>().gameObject.layer == LayerMask.NameToLayer("Interactable"))) { myAnimator.SetInteger("jumpDirection", 0); myAnimator.SetInteger("jumpType", 0); return; }
if (other.GetComponent<Collider2D>().gameObject.layer == LayerMask.NameToLayer("Enemy Hit"))
{
Enemy enemy = other.transform.parent.gameObject.GetComponent<Enemy>();
if (!enemy) { return; }
else if (other.transform.parent.gameObject.GetComponent<Enemy>().GetIsFrozen() && Mathf.Abs(myRigidbody.velocity.y)<1f) { myAnimator.SetInteger("jumpDirection", 0); myAnimator.SetInteger("jumpType", 0); return; }
ProcessHit(enemy);
}
}
private void OnCollisionEnter2D(Collision2D other)
{
//if (other.collider.gameObject.layer == LayerMask.NameToLayer("Ground")) { myAnimator.SetInteger("jumpType", 0); }
}
//KEYBOARD MOVEMENT
void OnMove(InputValue value)
{
moveInput = value.Get<Vector2>();
//Debug.Log(moveInput);
}
void OnUp(InputValue value)
{
if (myAnimator.GetInteger("directionUD") == -2) { myAnimator.SetInteger("directionUD", -1); }
else if (myAnimator.GetInteger("directionUD") == -1) { myAnimator.SetInteger("directionUD", 0); }
else if (myAnimator.GetInteger("directionUD") == 0) { myAnimator.SetInteger("directionUD", 1); }
}
void OnUpRelease(InputValue value)
{
if (myAnimator.GetInteger("directionUD") == 1) { myAnimator.SetInteger("directionUD", 0); }
}
void OnDown(InputValue value)
{
if (myAnimator.GetInteger("directionUD") == 0) { myAnimator.SetInteger("directionUD", -1); }
else if (myAnimator.GetInteger("directionUD") == -1 && GetComponent<PlayerItems>().GetHasMorphBall()) { myAnimator.SetInteger("directionUD", -2); }
}
void OnDiagonal(InputValue value)
{
if (value.Get<Vector2>().y > 0) { myAnimator.SetInteger("diagonalUD", 1); }
else if (value.Get<Vector2>().y < 0) { myAnimator.SetInteger("diagonalUD", -1); }
else { myAnimator.SetInteger("diagonalUD", 0); }
}
/// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//KEYBOARD ACTIONS
void OnSwitch(InputValue value)
{
Switch();
}
void OnJump(InputValue value)
{
if (!value.isPressed || (!myBoxCollider.IsTouchingLayers(LayerMask.GetMask("Ground")) && !myBoxCollider.IsTouchingLayers(LayerMask.GetMask("Interactable")) && !myBoxCollider.IsTouchingLayers(LayerMask.GetMask("Enemy Hit")))) { return; }
else if (!GetComponent<PlayerItems>().GetHasHighJump()) { myRigidbody.velocity += new Vector2(0f, jumpSpeed); }
else { myRigidbody.velocity += new Vector2(0f, 1.5f * jumpSpeed); }
if (myRigidbody.velocity.x<0.1f && myRigidbody.velocity.x > -0.1f) { myAnimator.SetInteger("jumpType", 1); }
else { myAnimator.SetInteger("jumpType", 2); }
}
//GENERAL METHODS
private void Reshape()
{
GetComponent<CapsuleCollider2D>().size = GetComponent<Renderer>().bounds.size - new Vector3(0.05f, 0.05f, 0f);
}
void Move()
{
float playerVelocityX = moveInput.x * runSpeed;
if (myAnimator.GetInteger("jumpType") == 1 && myAnimator.GetInteger("jumpDirection") != 0) { playerVelocityX *= 0.75f; }
else if (myAnimator.GetInteger("jumpType") == 2 && myAnimator.GetInteger("jumpDirection") != 0) { playerVelocityX *=1.2f ; }
myRigidbody.velocity = new Vector2(playerVelocityX, myRigidbody.velocity.y);
bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;
myAnimator.SetBool("isRunning", playerHasHorizontalSpeed);
if (myAnimator.GetInteger("directionUD") == -1 && playerHasHorizontalSpeed) { myAnimator.SetInteger("directionUD", 0); }
}
void FlipSprite()
{
bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;
if (playerHasHorizontalSpeed)
{
myAnimator.SetFloat("directionLR", 1 * Mathf.Sign(myRigidbody.velocity.x));
direction = 1 * Mathf.Sign(myRigidbody.velocity.x);
}
}
private void ProcessHit(Enemy enemy)
{
if (!invincibilityFrames)
{
gameObject.GetComponent<PlayerItems>().SetEnergy(gameObject.GetComponent<PlayerItems>().GetEnergy() - enemy.GetDamage());
if (GetComponent<PlayerItems>().GetEnergy() <= 0)
{
myAnimator.SetBool("isDead", true);
GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezePositionY | RigidbodyConstraints2D.FreezeRotation;
Destroy(gameObject, 2.1f);
}
StartCoroutine(Kickback((transform.position.x - enemy.transform.position.x) / Mathf.Abs(transform.position.x - enemy.transform.position.x)));
}
}
void Switch()
{
int grapple = 0;
if (gameObject.GetComponent<PlayerItems>().GetHasGrappleBeam()) { grapple = 1; }
int[] counts = new int[] { 1, gameObject.GetComponent<PlayerItems>().GetMissiles(), gameObject.GetComponent<PlayerItems>().GetSuperMissiles(), gameObject.GetComponent<PlayerItems>().GetPowerBombs(), grapple };
while (true)
{
myAnimator.SetInteger("missile", myAnimator.GetInteger("missile") + 1);
if (myAnimator.GetInteger("missile") > 3) { myAnimator.SetInteger("missile", 0); }
if (counts[myAnimator.GetInteger("missile")] > 0) { break; }
}
}
//GETTERS
public float GetDirection() { return direction; }
public void SetDirection(float newDirection) { myAnimator.SetFloat("directionLR", newDirection); }
IEnumerator Kickback(float direction)
{
invincibilityFrames = true;
myRigidbody.AddForce(new Vector2(0f, 1000f), ForceMode2D.Force);
for(int i = 0; i < 30; i++)
{
transform.position += new Vector3(direction * 0.15f, 0f);
yield return new WaitForSeconds(0.01f);
}
yield return new WaitForSeconds(2f);
invincibilityFrames = false;
}
}