-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlayerCombat.cs
More file actions
190 lines (148 loc) · 5.63 KB
/
PlayerCombat.cs
File metadata and controls
190 lines (148 loc) · 5.63 KB
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
public class PlayerCombat : MonoBehaviour
{
[SerializeField] private Transform attackPoint;
public bool canAttack = true;
[Header("Attack")]
[SerializeField] private float attackRange = 0.5f;
[SerializeField] private LayerMask enemyLayer;
[SerializeField] private LayerMask destructibleLayer;
[SerializeField] private int damage = 20;
[SerializeField] private float attackInterval = 0.2f;
private float currAttackTime = 0;
[SerializeField] private AudioSource meleeSound;
[Header("Shoot")]
[SerializeField] private GameObject bulletPrefab;
[SerializeField] private float shootInterval = 0.5f;
private float currShootTime = 0;
[SerializeField] private AntibodyReload antibodyUI;
[SerializeField] private AudioSource shootSound;
[Header("Ground Pound")]
private CharacterController2D controller;
private PlayerMovement player;
private Rigidbody2D rb;
private bool doGroundPound = false;
private bool groundPound = false;
private float gravityScale;
[SerializeField] private Transform GroundPoundPoint;
[SerializeField] private float smashRadius = 2f;
[SerializeField] private float stopTime = 0.3f;
[SerializeField] private float dropForce = 50f;
[SerializeField] private int smashDamage = 80;
[SerializeField] private AudioSource groundPound_lift;
[SerializeField] private AudioSource groundPound_smash;
private void Start() {
currAttackTime = 0;
currShootTime = 0;
if (antibodyUI != null) antibodyUI.SetMaxTime(shootInterval);
controller = GetComponent<CharacterController2D>();
player = GetComponent<PlayerMovement>();
rb = GetComponent<Rigidbody2D>();
gravityScale = rb.gravityScale;
}
// Update is called once per frame
void Update() {
// CanAttack();
if (!canAttack) return;
// Melee Attack
if (Input.GetButtonDown("Fire1") && currAttackTime <= 0) {
Attack();
currAttackTime = attackInterval;
} else {
currAttackTime -= Time.deltaTime;
}
// Shoot
if (Input.GetButtonDown("Fire2") && currShootTime <= 0) {
Shoot();
currShootTime = shootInterval;
} else {
currShootTime -= Time.deltaTime;
}
// Ground Pound
if (!doGroundPound && !controller.IsGrounded() && Input.GetButtonDown("GroundPound")) {
doGroundPound = true;
// groundPound_lift.Play();
}
if (antibodyUI != null) antibodyUI.SetTime(shootInterval - currShootTime);
}
private void FixedUpdate() {
if (doGroundPound) {
GroundPound();
}
doGroundPound = false;
}
private void Shoot() {
Instantiate(bulletPrefab, attackPoint.position, attackPoint.rotation);
shootSound.Play();
}
private void Attack() {
Collider2D[] enemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayer);
meleeSound.Play();
foreach (Collider2D enemy in enemies) {
if (!enemy.isTrigger) {
if (enemy.GetComponent<Enemy>() != null) enemy.GetComponent<Enemy>().TakeDamage(damage);
else enemy.GetComponent<Boss>().TakeDamage(damage);
}
}
Collider2D[] destructibles = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, destructibleLayer);
foreach(Collider2D destructible in destructibles) {
destructible.GetComponent<DestructibleTile>().TakeDamage(damage);
}
}
private void GroundPound() {
groundPound = true;
player.canMove = false;
StopInAir();
StartCoroutine("Smash");
player.canMove = true;
// Reset gravity scale
rb.gravityScale = gravityScale;
}
private void StopInAir() {
// Stop
rb.velocity = Vector2.zero;
rb.angularVelocity = 0;
// Halt in air
rb.gravityScale = 0;
}
private IEnumerator Smash() {
yield return new WaitForSeconds(stopTime);
// Push downwards
rb.AddForce(Vector2.down * dropForce, ForceMode2D.Impulse);
}
private void OnCollisionEnter2D(Collision2D other) {
// If the collided object is below the player
if (groundPound && other.contacts[0].normal.y > 0.5) {
CompleteGroundPound();
}
}
private void CompleteGroundPound() {
if (groundPound_smash != null) groundPound_smash.Play();
// Destroy destructible tiles
Collider2D[] destructibles = Physics2D.OverlapCircleAll(GroundPoundPoint.position, smashRadius, destructibleLayer);
foreach(Collider2D destructible in destructibles) {
Destroy(destructible.gameObject);
}
// Damage surrounding enemies
Collider2D[] enemies = Physics2D.OverlapCircleAll(GroundPoundPoint.position, smashRadius, enemyLayer);
foreach(Collider2D enemy in enemies) {
enemy.GetComponent<Enemy>().TakeDamage(smashDamage);
}
groundPound = false;
}
private void OnDrawGizmos() {
if (attackPoint == null) return;
Gizmos.DrawWireSphere(attackPoint.position, attackRange);
if (GroundPoundPoint == null) return;
Gizmos.DrawWireSphere(GroundPoundPoint.position, smashRadius);
}
public void ChangeBullet(GameObject bullet) {
bulletPrefab = bullet;
}
public GameObject GetBullet() {
return bulletPrefab;
}
}