-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrayScript
More file actions
209 lines (169 loc) · 5.12 KB
/
Copy pathFrayScript
File metadata and controls
209 lines (169 loc) · 5.12 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FrayScript : MonoBehaviour
{
//health
private int maxHealth = 20;
private int health;
//attacking
private int damage = 1;
public bool inFight = false;
private bool canAttack = true;
//movement variables
public float direction = 1;
private float speed;
private float phaseOneSpeed = 6;
private float phaseTwoSpeed = 8;
private Vector3 yFlip = new Vector3(0, 180, 0);
public bool canFlip = true;
private float dashForce = 20;
private float jumpForce = 7;
//variables for timers
public float timeToFreeze = 1;
private float flipFreezeTime = 1;
private float attackRemaining = 0;
private float timeRemaining = 0;
private float flipTimeRemaining = 0;
//player gameobject
private GameObject player;
// Start is called before the first frame update
void Start()
{
//set stats and get player
speed = phaseOneSpeed;
health = maxHealth;
transform.eulerAngles = Vector3.zero;
player = GameObject.FindGameObjectWithTag("Player");
}
// Update is called once per frame
void Update()
{
if (health == 10)
{
speed = phaseTwoSpeed;
}
if(inFight)
{
GetComponent<Animator>().speed = 1;
}
else
{
GetComponent<Animator>().speed = 0;
}
//timer for freezing after hitting player
if (timeRemaining <= 0 && inFight)
{
transform.Translate(Vector3.right * speed * Time.deltaTime);
}
else
{
timeRemaining -= Time.deltaTime;
}
//timer for flipping around
if (flipTimeRemaining <= 0)
{
canFlip = true;
}
else
{
flipTimeRemaining -= Time.deltaTime;
}
if (attackRemaining <= 0)
{
canAttack = true;
GetComponent<Animator>().Play("Base Layer.Fray Walk Cycle");
}
else
{
attackRemaining -= Time.deltaTime;
}
if (inFight && canAttack)
{
//select an attack based on player positions and such
//set the timer for the attack based on how long it takes to execute
if(player.transform.position.y > 92 && player.transform.position.y < 93)
{
//if the player is on the ground, dash
Dash();
}
else
{
//otherwise move between bounds
}
}
}
private void OnTriggerEnter2D(Collider2D other)
{
//if the enemy is colliding with an attack, take damage (not before the fight has started)
if (other.gameObject.tag == "Attack" && inFight)
{
health -= 1;
player.GetComponent<PlayerController>().PlayHitEnemy();
GetComponent<Rigidbody2D>().AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
if (health <= 0)
{
//enemy died
PlayerController playerCont = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
inFight = false;
playerCont.FrayDied();
}
}
}
private void OnCollisionEnter2D(Collision2D other)
{
//switch directions when something is hit
if (other.gameObject.tag != "Floor")
{
if (canFlip && inFight)
{
if (direction == 1)
{
transform.Rotate(yFlip);
direction = -1;
}
else if (direction == -1)
{
transform.Rotate(yFlip);
direction = 1;
}
canFlip = false;
flipTimeRemaining = flipFreezeTime;
}
}
if (other.gameObject.tag == "Player" && inFight)
{
other.gameObject.GetComponent<PlayerController>().DamagePlayer(damage);
}
}
//Set variables back to their defaults
public void ResetStats()
{
health = maxHealth;
speed = phaseOneSpeed;
transform.eulerAngles = Vector3.zero;
direction = 1;
}
//attacks
//dash quickly in the direction of the player until the wall is hit
private void Dash()
{
canAttack = false;
attackRemaining = 2;
GetComponent<Animator>().Play("Base Layer.Fray Dash");
if (player.transform.position.x < transform.position.x)
{
//dash to the left
direction = -1;
transform.eulerAngles = new Vector3(0, 180, 0);
GetComponent<Rigidbody2D>().AddForce(transform.right * dashForce, ForceMode2D.Impulse);
}
else
{
//dash to the right
direction = 1;
transform.eulerAngles = new Vector3(0, 0, 0);
GetComponent<Rigidbody2D>().AddForce(transform.right * dashForce, ForceMode2D.Impulse);
}
}
}