Repository navigation
Expand file tree
/
Copy pathBoss.cs
More file actions
143 lines (127 loc) · 4.18 KB
/
Copy pathBoss.cs
File metadata and controls
143 lines (127 loc) · 4.18 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
using System;
using System.Collections.Generic;
using UnityEngine;
public class Boss: Enemy {
public static Boss I {
get {
if (!_instance) {
_instance = FindFirstObjectByType<Boss>();
if (!_instance) {
throw new NullReferenceException("Boss has not yet been spawned");
}
}
return _instance;
}
}
private static Boss _instance;
public Health Health => health;
protected override float AttackDistance => 1.6f;
protected override SoundEffect AttackSound => base.AttackSound;
protected override float MoveSpeed => 35f;
protected override float RotationSpeed => base.RotationSpeed;
protected override (bool, float) MoveWhileAttacking => (jumping, 3f);
public static readonly int baseHealth = 10;
protected override float AttackCooldown => 1.65f;
const string animSwingTrigger = "Attack_Swing";
const string animSweepTrigger = "Attack_Sweep";
const string animSmashTrigger = "Attack_Smash";
const string animJumpTrigger = "Attack_Jump";
struct AttackSettings {
public string animString;
public float attackDuration;
public float attackCooldown;
public SoundEffect attackSound;
public float soundDelay;
public AttackSettings(
string animString,
float attackDuration,
float attackCooldown,
SoundEffect attackSound,
float soundDelay = 0.5f
) {
this.animString = animString;
this.attackDuration = attackDuration;
this.attackCooldown = attackCooldown;
this.attackSound = attackSound;
this.soundDelay = soundDelay;
}
}
protected override void Awake() {
base.Awake();
ChooseNextAttack();
health.DeathDelay = 2f;
}
// Handles choosing attack and setup, each attack method controls the cooldown and animations
protected override void HandleAttack() {
if (!attackReady) return;
var distToPlayer = Vector3.Distance(transform.position, playerTransform.position);
// Boss can do jump attack even outside of range
if (distToPlayer >= AttackDistance && NextAttack != AttackJump) {
return;
}
InvokeOnAttack();
var attack = NextAttack();
attacking = true;
attackReady = false;
animator.SetTrigger(attack.animString);
new Timeout(() => attacking = false, attack.attackDuration, this);
new Timeout(() => {
if (health.Dead) return;
Sound.PlayClip(audioSource, attack.attackSound);
}, attack.soundDelay, this);
new Timeout(() => attackReady = true, attack.attackCooldown, this);
ChooseNextAttack();
}
Func<AttackSettings> NextAttack = null;
void ChooseNextAttack() {
if (NextAttack == null) {
NextAttack = AttackSwing;
return;
}
var availableAttacks = new List<Func<AttackSettings>>(){
AttackSwing,
AttackSweep,
AttackSmash,
AttackJump
};
availableAttacks.Remove(NextAttack);
NextAttack = availableAttacks.Random();
}
AttackSettings AttackSwing() {
return new AttackSettings(
animSwingTrigger,
0.9f,
AttackCooldown,
SoundEffects.I.HeavySwing
);
}
AttackSettings AttackSweep() {
return new AttackSettings(
animSweepTrigger,
1.1f,
AttackCooldown,
SoundEffects.I.HeavySwing
);
}
AttackSettings AttackSmash() {
return new AttackSettings(
animSmashTrigger,
1.2f,
AttackCooldown,
SoundEffects.I.Smash,
0.6f
);
}
bool jumping = false;
AttackSettings AttackJump() {
new Timeout(() => jumping = true, 0.4f, this);
new Timeout(() => jumping = false, 1.2f, this);
return new AttackSettings(
animJumpTrigger,
2.9f,
3.3f,
SoundEffects.I.Smash,
1.4f
);
}
}