2025-05-18 01:04:31 +08:00

205 lines
4.1 KiB
C#

// ***********************************************************************
// Assembly : Unity
// Author : Kimch
// Created : 2017-11-14
//
// Last Modified By : Kimch
// Last Modified On :
// ***********************************************************************
// <copyright file= "EntityTrap" company=""></copyright>
// <summary></summary>
// ***********************************************************************
using UnityEngine;
namespace G
{
public class EntityTrap : Entity
{
private Collider mycollider;
public float firerate = 4f;
public float duration = 1f;
public float startdelay = 0.5f;
public bool rndRotation;
public float startPosY;
public Vector3 startScale;
public float growScaleSpeed;
public Vector3 movedir;
public float accuracy = 0.1f;
public bool playAutomatically;
public Transform liveObj;
private bool trapOn;
private Vector3 originTrapScale;
private Transform target;
private bool scaleUp;
private bool scaleDown;
private bool moveOn;
private int damage = 1;
private void Awake()
{
mycollider = base.GetComponent<Collider>();
originTrapScale = transform.localScale;
target = GameObject.FindWithTag("Player").transform;
transform.position = Vector3.up * 56f;
transform.rotation = Quaternion.identity;
mycollider.enabled = false;
}
public void StopActive()
{
if (liveObj)
{
liveObj.SendMessage("FisnishShoot", SendMessageOptions.DontRequireReceiver);
}
CancelInvoke();
trapOn = false;
scaleUp = false;
scaleDown = false;
moveOn = false;
mycollider.enabled = false;
transform.position = Vector3.up * 45f;
}
public void SetDamage(int _damage)
{
if (liveObj)
{
InvokeRepeating(nameof(LiveObjOn), 3f, firerate);
}
damage = _damage;
Invoke(nameof(Ready_trap), firerate + Random.Range(0f, 2f) + 3f);
trapOn = true;
}
private void LiveObjOn()
{
if (liveObj)
liveObj.SendMessage("StartShoot", SendMessageOptions.DontRequireReceiver);
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.layer == GameLayer.AllyLayer)
{
Vector3 vector = Vector3.Normalize(other.transform.position - transform.position);
vector.y = damage;
other.transform.root.SendMessage("Damaged", vector);
}
}
public void DirectFire(Vector3 _pos)
{
if (trapOn)
{
transform.position = _pos;
Emit_trap();
if (rndRotation)
{
transform.rotation = Quaternion.Euler(0f, GlobalUtils.RndAngle, 0f);
}
}
}
private void Emit_trap()
{
if (trapOn)
{
transform.localScale = originTrapScale;
if (growScaleSpeed != 0f)
{
scaleUp = true;
}
if (movedir != Vector3.zero)
{
moveOn = true;
}
mycollider.enabled = true;
Invoke(nameof(Disappear_trap), duration);
}
}
private void Disappear_trap()
{
if (trapOn)
{
scaleUp = false;
scaleDown = true;
moveOn = false;
if (playAutomatically)
{
Invoke(nameof(Ready_trap), firerate + Random.Range(0f, 2f));
}
mycollider.enabled = false;
}
}
private void Ready_trap()
{
if (trapOn)
{
Vector3 position = target.position + Random.onUnitSphere * accuracy;
if (startPosY != 0f)
{
position.y = startPosY;
}
else
{
position.y = 0f;
}
transform.position = position;
transform.localScale = startScale;
if (rndRotation)
{
transform.rotation = Quaternion.Euler(0f, GlobalUtils.RndAngle, 0f);
}
Invoke(nameof(Emit_trap), startdelay);
}
}
private void Update()
{
if (scaleUp)
{
transform.localScale = Vector3.MoveTowards(transform.localScale, originTrapScale, growScaleSpeed * Time.deltaTime);
}
else if (scaleDown)
{
transform.localScale = Vector3.MoveTowards(transform.localScale, Vector3.zero, Time.deltaTime);
Vector3 localScale = transform.localScale;
if (localScale.x < 0.01f)
{
transform.position = Vector3.up * 45f;
scaleDown = false;
}
}
if (moveOn)
{
Vector3 position = transform.position;
if (position.y > 0f)
{
transform.position += movedir * Time.deltaTime;
}
}
}
}
}