2020-11-07 01:21:50 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
2020-11-06 21:30:54 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
2020-11-07 01:21:50 +00:00
|
|
|
|
using Random = System.Random;
|
2020-11-06 21:30:54 +00:00
|
|
|
|
|
|
|
|
|
public class EnemyController : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public Transform target;
|
|
|
|
|
public float speed;
|
2020-11-06 23:51:23 +00:00
|
|
|
|
public float flyForce;
|
2020-11-07 01:21:50 +00:00
|
|
|
|
public short finalHealth = 3;
|
2020-11-06 21:30:54 +00:00
|
|
|
|
private Rigidbody2D _rb;
|
2020-11-07 01:21:50 +00:00
|
|
|
|
private short _hitsToRemove;
|
|
|
|
|
private Random _random = new Random();
|
2020-11-06 21:30:54 +00:00
|
|
|
|
|
|
|
|
|
// Start is called before the first frame update
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
_rb = GetComponent<Rigidbody2D>();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-07 01:21:50 +00:00
|
|
|
|
private void OnEnable()
|
|
|
|
|
{
|
|
|
|
|
_hitsToRemove = finalHealth;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-06 21:30:54 +00:00
|
|
|
|
// Update is called once per frame
|
|
|
|
|
void FixedUpdate()
|
|
|
|
|
{
|
2020-11-06 23:51:23 +00:00
|
|
|
|
var tr = transform;
|
|
|
|
|
var diff = target.position - tr.position;
|
|
|
|
|
if (_rb.mass < 0.01f)
|
|
|
|
|
{ //Már lelőttük
|
2020-11-07 20:50:40 +00:00
|
|
|
|
if (diff.magnitude > 10) //Ha már túl messze van
|
|
|
|
|
Remove();
|
2020-11-06 23:51:23 +00:00
|
|
|
|
|
|
|
|
|
_rb.AddForce(new Vector2(0f, flyForce * _rb.mass * _rb.gravityScale)); //Ne maradjon véletlenül útban
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-07 20:50:40 +00:00
|
|
|
|
if (diff.y > 5)
|
|
|
|
|
Remove();
|
|
|
|
|
|
2020-11-06 21:30:54 +00:00
|
|
|
|
diff.Normalize();
|
2020-11-07 01:21:50 +00:00
|
|
|
|
float sp = ((float) _random.NextDouble() / 2f + 1f) * speed; //1 és 1.5 közötti szorzó
|
|
|
|
|
_rb.AddForce(diff * (sp * _rb.mass * _rb.gravityScale));
|
2020-11-06 23:51:23 +00:00
|
|
|
|
if (diff.x * transform.localScale.x < 0) //Ha másfelé néz, mint amerre megy
|
|
|
|
|
{
|
|
|
|
|
var scale = tr.localScale;
|
|
|
|
|
scale.x *= -1;
|
|
|
|
|
tr.localScale = scale;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-07 01:21:50 +00:00
|
|
|
|
private void Die()
|
2020-11-06 23:51:23 +00:00
|
|
|
|
{
|
|
|
|
|
_rb.mass = 0.00001f;
|
|
|
|
|
_rb.gravityScale = 0.01f;
|
|
|
|
|
_rb.freezeRotation = false;
|
2020-11-06 21:30:54 +00:00
|
|
|
|
}
|
2020-11-06 23:51:23 +00:00
|
|
|
|
|
|
|
|
|
public bool IsAlive() => _rb.mass > 0.001f;
|
2020-11-07 01:21:50 +00:00
|
|
|
|
|
|
|
|
|
private void HitWhileFlying()
|
|
|
|
|
{
|
|
|
|
|
_hitsToRemove--;
|
|
|
|
|
if (_hitsToRemove == 0)
|
2020-11-07 20:50:40 +00:00
|
|
|
|
Remove();
|
2020-11-07 01:21:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Hit()
|
|
|
|
|
{
|
|
|
|
|
if (IsAlive())
|
|
|
|
|
Die();
|
|
|
|
|
else
|
|
|
|
|
HitWhileFlying();
|
|
|
|
|
}
|
2020-11-07 13:10:48 +00:00
|
|
|
|
|
|
|
|
|
private void OnCollisionEnter2D(Collision2D other)
|
|
|
|
|
{
|
|
|
|
|
var go = other.gameObject;
|
2020-11-07 20:50:40 +00:00
|
|
|
|
if (!go.CompareTag("Player"))
|
2020-11-07 13:10:48 +00:00
|
|
|
|
return;
|
|
|
|
|
if (IsAlive())
|
|
|
|
|
go.GetComponent<OwnCharacterController>().Hit();
|
|
|
|
|
}
|
2020-11-07 20:50:40 +00:00
|
|
|
|
|
|
|
|
|
private void Remove()
|
|
|
|
|
{
|
|
|
|
|
_rb.velocity = Vector2.zero;
|
|
|
|
|
gameObject.SetActive(false);
|
|
|
|
|
}
|
2020-11-06 21:30:54 +00:00
|
|
|
|
}
|