-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerScript_Melee.cs
280 lines (256 loc) · 8.46 KB
/
PlayerScript_Melee.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using UnityEngine.SceneManagement;
public class PlayerScript_Melee : MonoBehaviour
{
[SerializeField]
private float runSpeed;
[SerializeField]
public Transform groundCheck;
[SerializeField]
private float jumpForce;
private Animator animator;
Collider2D col2D;
private string currentAnimaton;
private float xAxis;
private float yAxis;
private Rigidbody2D rb2d;
private bool isJumpPressed;
private bool isGrounded;
private bool isAttackPressed;
private bool isAttacking;
private bool isntDead;
private bool TakingDamage;
private bool ControlPressedLeft;
private bool ControlPressedRight;
[SerializeField]
AudioSource StabMusic;
[SerializeField]
AudioSource DodgeMusic;
[SerializeField]
AudioSource DamageMusic;
public AudioSource BackGroundM;
public bool isFacingLeft = false;
[SerializeField]
public Transform Damage_Box;
public float attackRange;
public LayerMask Enemies;
[SerializeField]
private float attackDelay = 0f;
private float damageDelay;
public int Playerhealth;
public int Playerblood = 0;
public int damage = 1;
private Vector3 dashVc3;
//Animation States
const string PLAYER_IDLE = "Player_Idle";
const string PLAYER_DODGEBACKWARD = "Player_DodgeBackward";
const string PLAYER_DODGEFORWARD = "Player_DodgeForward";
const string PLAYER_RUN = "Player_Run";
const string PLAYER_JUMP = "Player_Jump_Gun";
const string PLAYER_ATTACK = "Player_Attack";
const string PLAYER_AIR_ATTACK = "Player_Jump_Firing";
const string PLAYER_DEATH = "Player_Death";
const string PLAYER_TAKEDAMAGE = "Player_TakeDamage";
//=====================================================
// Start is called before the first frame update
//=====================================================
void Start()
{
isntDead = true;
rb2d = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
AudioSource StabMusic = GameObject.Find("StabMusic").GetComponent<AudioSource>();
AudioSource DamageMusic = GameObject.Find("DamageMusic").GetComponent<AudioSource>();
AudioSource DodgeMusic = GameObject.Find("DodgeMusic").GetComponent<AudioSource>();
AudioSource BackGroundM = GameObject.Find("BackGroundMusic").GetComponent<AudioSource>();
}
void Update()
{
if (Playerhealth <= 0)
{
isntDead = false;
ChangeAnimationState(PLAYER_DEATH);
Invoke("Die", 2f);
SceneManager.LoadScene(0);
}
//Checking for inputs
xAxis = Input.GetAxisRaw("Horizontal");
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
if (!isAttacking && !TakingDamage)
{
rb2d.velocity = new Vector2(runSpeed, rb2d.velocity.y);
transform.localScale = new Vector3(1, 1, 1);
isFacingLeft = false;
}
}
else if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
if (!isAttacking && !TakingDamage)
{
rb2d.velocity = new Vector2(-runSpeed, rb2d.velocity.y);
transform.localScale = new Vector3(-1, 1, 1);
isFacingLeft = true;
}
}else if (isGrounded)
{
rb2d.velocity = new Vector2(0, rb2d.velocity.y);
}
//space jump key pressed?
if (Input.GetKeyDown(KeyCode.Space))
{
isJumpPressed = true;
}
if (Input.GetKeyDown(KeyCode.Q))
{
ControlPressedLeft = true;
}
if (Input.GetKeyDown(KeyCode.E))
{
ControlPressedRight = true;
}
//space Atatck key pressed?
if (Input.GetKeyDown(KeyCode.Mouse0))
{
isAttackPressed = true;
Debug.Log("attackpressed");
}
}
//=====================================================
// Physics based time step loop
//=====================================================
private void FixedUpdate()
{
if (Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("ground")))
{
isGrounded = true;
Debug.Log("isGROUNDED");
}
else
{
isGrounded = false;
}
//------------------------------------------
if (isGrounded && !isAttacking && isntDead && !TakingDamage)
{
if (ControlPressedLeft)
{
float dash = transform.position.x;
transform.position = new Vector3(dash + -10f,transform.position.y,transform.position.z);
Debug.Log("DASHED_Player");
BackGroundM.volume = 0.6f;
DodgeMusic.Play();
ChangeAnimationState(PLAYER_DODGEBACKWARD);
ControlPressedLeft = false;
BackGroundM.volume = 1f;
}
if (ControlPressedRight)
{
float dash = transform.position.x;
transform.position = new Vector3(dash + 10f,transform.position.y,transform.position.z);
BackGroundM.volume = 0.6f;
DodgeMusic.Play();
ChangeAnimationState(PLAYER_DODGEFORWARD);
ControlPressedRight = false;
BackGroundM.volume = 1f;
}
if (xAxis != 0)
{
ChangeAnimationState(PLAYER_RUN);
}
else
{
ChangeAnimationState(PLAYER_IDLE);
}
}
//Check if trying to jump
if (isJumpPressed && isGrounded)
{
rb2d.AddForce(new Vector2(0, jumpForce));
isJumpPressed = false;
ChangeAnimationState(PLAYER_JUMP);
}
//attack
if (isAttackPressed)
{
isAttackPressed = false;
if (!isAttacking)
{
isAttacking = true;
Collider2D[] enemiesInRange = Physics2D.OverlapCircleAll(Damage_Box.position, attackRange, Enemies);
//for giving every one of enemies damage.
for (int i = 0; i < enemiesInRange.Length; i++)
{
enemiesInRange[i].GetComponent<CommonEnemy>().TakeDamage(damage);
Debug.Log("damage givenby player");
}
BackGroundM.volume = 0.6f;
StabMusic.Play();
if(isGrounded)
{
ChangeAnimationState(PLAYER_ATTACK);
}
else
{
ChangeAnimationState(PLAYER_AIR_ATTACK);
}
attackDelay = animator.GetCurrentAnimatorStateInfo(0).length + 0.2f;
Invoke("AttackComplete", attackDelay);
}
}
}
/*IEnumerator AttackStart()
{
Damage_Box.SetActive(true);
yield return new WaitForSeconds(.4f);
Damage_Box.SetActive(false);
isAttacking = false;
}*/
void AttackComplete()
{
isAttacking = false;
BackGroundM.volume = 1f;
Debug.Log("ATTACKCOMPLETE");
}
public void Die()
{
Destroy(gameObject);
}
public void PlayerTakeDamage(int damage)
{
BackGroundM.volume = 0.6f;
TakingDamage = true;
Playerhealth -= damage;
Debug.Log("damageTakenbyplayer");
DamageMusic.Play();
ChangeAnimationState(PLAYER_TAKEDAMAGE);
damageDelay = animator.GetCurrentAnimatorStateInfo(0).length + 1f;
Invoke("DamageDelayComplete", damageDelay);
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(Damage_Box.position, attackRange);
}
void DamageDelayComplete()
{
BackGroundM.volume = 1f;
TakingDamage = false;
}
//=====================================================
// mini animation manager
//=====================================================
void ChangeAnimationState(string newAnimation)
{
if (currentAnimaton == newAnimation) return;
animator.Play(newAnimation);
currentAnimaton = newAnimation;
}
}