440 lines
13 KiB
C#
440 lines
13 KiB
C#
// ***********************************************************************
|
|
// Assembly : Game
|
|
// Author : Kimch
|
|
// Created : 2020-09-02
|
|
// Description :
|
|
// Last Modified By :
|
|
// Last Modified On :
|
|
// ***********************************************************************
|
|
// <copyright file= "AI_General" company="Kimch"></copyright>
|
|
// <summary></summary>
|
|
// ***********************************************************************
|
|
using UnityEngine;
|
|
|
|
namespace G
|
|
{
|
|
public class AI_General : MonoBehaviour
|
|
{
|
|
|
|
private Animation _myAnimation;
|
|
private Collider _myCollider;
|
|
|
|
private int maxhp;
|
|
|
|
public int hp;
|
|
|
|
private float power;
|
|
|
|
private int defence;
|
|
|
|
private int dash;
|
|
|
|
private float runspeed;
|
|
|
|
private float atkspeed;
|
|
|
|
private float distance_cha;
|
|
|
|
private float hp_length;
|
|
|
|
private float fb;
|
|
|
|
private bool life = true;
|
|
|
|
private bool call;
|
|
|
|
private bool disable;
|
|
|
|
private float delay_invicibility;
|
|
|
|
private float delay_call;
|
|
|
|
private Vector3 rndpos;
|
|
|
|
private Vector3 directionVector;
|
|
|
|
private Vector3 behitdir;
|
|
|
|
private Transform cha1;
|
|
|
|
private Transform g_hp;
|
|
|
|
private Transform c_weapon;
|
|
|
|
private Transform c_spweapon;
|
|
|
|
private Transform sp_selweapon;
|
|
|
|
|
|
private bool attack_start;
|
|
|
|
private bool superarmor;
|
|
|
|
private bool showme;
|
|
|
|
private int generalmovestat;
|
|
|
|
private int grade;
|
|
|
|
private int atkcount;
|
|
|
|
private int damage;
|
|
|
|
private short unique = -1;
|
|
|
|
private bool efattach;
|
|
|
|
public AudioClip snd_attack;
|
|
|
|
public Transform ef_weapon;
|
|
|
|
public Transform[] sp_weapon = new Transform[2];
|
|
|
|
public Transform direction_arrow;
|
|
|
|
private Vector3 efpos;
|
|
|
|
private Vector3 arrowTargetVector;
|
|
|
|
private Vector3 arrowRotVector;
|
|
|
|
private Quaternion efrot;
|
|
|
|
private GameCamera script_cam;
|
|
|
|
private void Awake()
|
|
{
|
|
_myAnimation = base.GetComponent<Animation>();
|
|
_myCollider = base.GetComponent<Collider>();
|
|
_myAnimation["move"].speed = 0.5f;
|
|
_myAnimation["down"].speed = 0.2f;
|
|
_myAnimation["dead_general"].speed = 0.2f;
|
|
_myAnimation["idle"].speed = 0.25f;
|
|
_myAnimation["down"].layer = 4;
|
|
_myAnimation["dead_general"].layer = 4;
|
|
_myAnimation["m_attack1"].layer = 1;
|
|
_myAnimation["m_attack1_i"].layer = 1;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
cha1 = GameObject.FindWithTag("Player").transform;
|
|
InvokeRepeating(nameof(SetRndPosition), 0.1f, 1f);
|
|
_myAnimation.Play("idle");
|
|
HPgaugeSet();
|
|
script_cam = Camera.main.GetComponent<GameCamera>();
|
|
}
|
|
|
|
public void Set_stat(int _power, int _defence, int _mhp, int _hp, int _dash, float _speed, float _atkspeed, int _grade, short _unique, bool _efattach)
|
|
{
|
|
grade = _grade;
|
|
unique = _unique;
|
|
efattach = _efattach;
|
|
if (unique == -1)
|
|
{
|
|
atkcount = -1;
|
|
}
|
|
sp_selweapon = sp_weapon[(int)(unique * 0.2f)];
|
|
atkspeed = _atkspeed;
|
|
_myAnimation["m_attack1"].speed = atkspeed;
|
|
_myAnimation["m_attack1_i"].speed = atkspeed;
|
|
power = 1f + _power * 0.5f;
|
|
defence = 1 + _defence;
|
|
maxhp = _mhp;
|
|
hp = _hp;
|
|
if (hp > maxhp)
|
|
{
|
|
hp = maxhp;
|
|
}
|
|
runspeed = _speed;
|
|
dash = _dash;
|
|
_myAnimation["move"].speed = runspeed;
|
|
}
|
|
|
|
public void CallGeneral()
|
|
{
|
|
script_cam.LookTarget(transform, 30, 2f);
|
|
}
|
|
|
|
public void SetRndPosition()
|
|
{
|
|
rndpos = Random.onUnitSphere * 0.2f + cha1.position + cha1.forward * 0.3f;
|
|
rndpos.y = 0f;
|
|
distance_cha = Vector3.Distance(transform.position, cha1.position);
|
|
if (distance_cha > 0.35f)
|
|
{
|
|
_myAnimation.CrossFade("move");
|
|
showme = true;
|
|
}
|
|
else
|
|
{
|
|
_myCollider.enabled = true;
|
|
_myAnimation.CrossFade("idle");
|
|
showme = false;
|
|
}
|
|
|
|
Vector3 position = transform.position;
|
|
if (Mathf.Abs(position.x) > 2.6f)
|
|
{
|
|
fb = Mathf.Abs(position.x) / position.x;
|
|
position.x = fb * 2.6f;
|
|
base.transform.position = new Vector3(position.x, position.y, position.z);
|
|
}
|
|
|
|
if (Mathf.Abs(position.z) > 2.5f)
|
|
{
|
|
fb = Mathf.Abs(position.z) / position.z;
|
|
base.transform.position = new Vector3(position.x, position.y, fb * 2.5f);
|
|
}
|
|
}
|
|
|
|
public void Damaged(Vector3 _damageVector)
|
|
{
|
|
if (superarmor || !life)
|
|
{
|
|
return;
|
|
}
|
|
damage = (int)_damageVector.y;
|
|
_damageVector[1] = 0f;
|
|
_myAnimation.Stop();
|
|
this.GetComponent<Rigidbody>().AddForce(_damageVector * 60f);
|
|
damage = Mathf.Max(1, damage - defence);
|
|
hp -= damage;
|
|
_myCollider.enabled = false;
|
|
delay_invicibility = 3.5f;
|
|
superarmor = true;
|
|
HPgaugeSet();
|
|
if (hp <= 0)
|
|
{
|
|
delay_invicibility = 0f;
|
|
_myAnimation.Play("dead_general");
|
|
life = false;
|
|
CancelInvoke();
|
|
showme = false;
|
|
if (c_weapon != null)
|
|
{
|
|
c_weapon.gameObject.SetActive(false);
|
|
c_weapon.transform.position = Vector3.up * 24f;
|
|
}
|
|
g_hp.gameObject.SetActive(false);
|
|
hp = 1;
|
|
}
|
|
else if (!call)
|
|
{
|
|
_myAnimation.Play("down");
|
|
}
|
|
}
|
|
|
|
public void HPgaugeSet()
|
|
{
|
|
hp_length = (1f - hp / (float)maxhp) * 0.25f;
|
|
}
|
|
|
|
public void HPfull()
|
|
{
|
|
hp = maxhp;
|
|
HPgaugeSet();
|
|
InvokeRepeating("SetRndPosition", 0f, 1f);
|
|
disable = false;
|
|
life = true;
|
|
superarmor = false;
|
|
|
|
Vector3 position = cha1.position;
|
|
float x = position.x;
|
|
base.transform.position = new Vector3(x, 0f, position.z);
|
|
_myAnimation.Play("idle");
|
|
_myCollider.enabled = true;
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
if (other.gameObject.layer == GameLayer.UnitLayer)
|
|
{
|
|
AttackOn(other.transform.position);
|
|
_myCollider.enabled = false;
|
|
}
|
|
}
|
|
|
|
public void AttackOn(Vector3 _enemypos)
|
|
{
|
|
if (!life || showme || call)
|
|
{
|
|
_myCollider.enabled = false;
|
|
}
|
|
else if (!attack_start)
|
|
{
|
|
if (unique >= 0)
|
|
{
|
|
atkcount = (atkcount + 1) % (5 - grade);
|
|
}
|
|
directionVector = _enemypos - transform.position;
|
|
_myAnimation.Play("m_attack1");
|
|
var attack_i = _myAnimation.PlayQueued("m_attack1_i");
|
|
attack_i.layer = 1;
|
|
attack_i.speed = atkspeed;
|
|
attack_start = true;
|
|
transform.rotation = Quaternion.LookRotation(directionVector);
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (disable)
|
|
{
|
|
return;
|
|
}
|
|
if (!life)
|
|
{
|
|
delay_invicibility += Time.deltaTime;
|
|
if (delay_invicibility > 2f)
|
|
{
|
|
delay_invicibility = 0f;
|
|
transform.position += Vector3.up * 14f;
|
|
_myAnimation.Stop();
|
|
disable = true;
|
|
}
|
|
return;
|
|
}
|
|
if (superarmor)
|
|
{
|
|
delay_invicibility -= Time.deltaTime;
|
|
if (delay_invicibility < 0f)
|
|
{
|
|
delay_invicibility = 0f;
|
|
superarmor = false;
|
|
_myCollider.enabled = true;
|
|
}
|
|
}
|
|
if (call)
|
|
{
|
|
delay_call += Time.deltaTime;
|
|
if (delay_call > 3f)
|
|
{
|
|
delay_call = 0f;
|
|
call = false;
|
|
}
|
|
}
|
|
if (_myAnimation.IsPlaying("down"))
|
|
{
|
|
attack_start = true;
|
|
}
|
|
else if (_myAnimation.IsPlaying("m_attack1_i"))
|
|
{
|
|
if (generalmovestat == 2)
|
|
{
|
|
return;
|
|
}
|
|
generalmovestat = 2;
|
|
if (atkcount != 0)
|
|
{
|
|
Vector3 myPos = transform.position;
|
|
|
|
Vector3 wpPos = ef_weapon.localPosition;
|
|
Vector3 a = myPos + Vector3.up * wpPos.y;
|
|
|
|
Vector3 forward = transform.forward;
|
|
|
|
Vector3 a2 = a + forward * wpPos.z;
|
|
Vector3 right = transform.right;
|
|
efpos = a2 + right * wpPos.x;
|
|
|
|
Vector3 wpEA = ef_weapon.eulerAngles;
|
|
Vector3 myEA = transform.eulerAngles;
|
|
|
|
efrot = Quaternion.Euler(wpEA.x, wpEA.y + myEA.y, wpEA.z);
|
|
if (c_weapon == null)
|
|
{
|
|
c_weapon = Object.Instantiate(ef_weapon, efpos, efrot);
|
|
if (efattach)
|
|
{
|
|
c_weapon.parent = transform;
|
|
}
|
|
c_weapon.gameObject.SetActive(true);
|
|
c_weapon.GetComponent<Rigidbody>().mass = power;
|
|
}
|
|
else
|
|
{
|
|
c_weapon.position = efpos;
|
|
c_weapon.rotation = efrot;
|
|
c_weapon.gameObject.SetActive(true);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Vector3 myPos = transform.position;
|
|
Vector3 wpPos = sp_selweapon.localPosition;
|
|
|
|
Vector3 a3 = myPos + Vector3.up * wpPos.y;
|
|
Vector3 myForward = transform.forward;
|
|
Vector3 a4 = a3 + myForward * wpPos.z;
|
|
|
|
Vector3 myRight = transform.right;
|
|
|
|
efpos = a4 + myRight * wpPos.x;
|
|
|
|
Vector3 wpEA = sp_selweapon.eulerAngles;
|
|
Vector3 myEA = transform.eulerAngles;
|
|
|
|
efrot = Quaternion.Euler(wpEA.x, wpEA.y + myEA.y, wpEA.z);
|
|
|
|
if (c_spweapon == null)
|
|
{
|
|
c_spweapon = Object.Instantiate(sp_selweapon, efpos, efrot);
|
|
c_spweapon.gameObject.SetActive(true);
|
|
c_spweapon.GetComponent<Rigidbody>().mass = power;
|
|
}
|
|
else
|
|
{
|
|
c_spweapon.position = efpos;
|
|
c_spweapon.rotation = efrot;
|
|
c_spweapon.gameObject.SetActive(true);
|
|
}
|
|
}
|
|
}
|
|
else if (_myAnimation.IsPlaying("m_attack1"))
|
|
{
|
|
if (generalmovestat != 1)
|
|
{
|
|
generalmovestat = 1;
|
|
this.GetComponent<Rigidbody>().AddForce(directionVector * dash);
|
|
if (atkcount == 0)
|
|
{
|
|
delay_invicibility = 1.5f;
|
|
superarmor = true;
|
|
}
|
|
}
|
|
}
|
|
else if (_myAnimation.IsPlaying("move"))
|
|
{
|
|
if (generalmovestat != 0)
|
|
{
|
|
_myCollider.enabled = true;
|
|
generalmovestat = 0;
|
|
}
|
|
attack_start = false;
|
|
directionVector = cha1.position - transform.position;
|
|
if (directionVector != Vector3.zero)
|
|
{
|
|
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(directionVector), Time.deltaTime * 5f);
|
|
}
|
|
transform.position += transform.forward * Time.deltaTime * (runspeed - 0.1f);
|
|
}
|
|
else if (_myAnimation.IsPlaying("idle"))
|
|
{
|
|
if (generalmovestat != 0)
|
|
{
|
|
_myCollider.enabled = true;
|
|
generalmovestat = 0;
|
|
}
|
|
attack_start = false;
|
|
directionVector = rndpos - transform.position;
|
|
if (directionVector != Vector3.zero)
|
|
{
|
|
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(directionVector), Time.deltaTime * 2f);
|
|
}
|
|
transform.position += transform.forward * Time.deltaTime * 0.1f;
|
|
}
|
|
}
|
|
}
|
|
}
|