2018-04-22 23:50:39 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using Assets.Scripts.Entities.Attack;
|
|
|
|
|
|
|
|
|
|
namespace Assets.Scripts.Entities {
|
2018-04-23 16:44:02 +02:00
|
|
|
|
public class Enemy : Mob {
|
|
|
|
|
public enum Enemys {
|
|
|
|
|
SCORPION,
|
|
|
|
|
BUG
|
|
|
|
|
}
|
2018-04-22 23:50:39 +02:00
|
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private float speed = 1;
|
|
|
|
|
[SerializeField]
|
|
|
|
|
private float rotationSpeed = 1;
|
2018-04-23 00:25:37 +02:00
|
|
|
|
[SerializeField]
|
2018-04-22 23:50:39 +02:00
|
|
|
|
protected GameObject victim;
|
2018-04-23 00:25:37 +02:00
|
|
|
|
[SerializeField]
|
2018-04-22 23:50:39 +02:00
|
|
|
|
private Rigidbody2D body;
|
2018-04-23 00:25:37 +02:00
|
|
|
|
private float nextAttackTime;
|
2018-04-22 23:50:39 +02:00
|
|
|
|
|
|
|
|
|
public Enemy(int mHP) : base(mHP) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Update() {
|
2018-04-23 01:25:50 +02:00
|
|
|
|
|
2018-04-22 23:50:39 +02:00
|
|
|
|
if ( victim == null || attack == null ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( Time.timeSinceLevelLoad >= nextAttackTime ) {
|
2018-04-23 01:25:50 +02:00
|
|
|
|
RaycastHit2D[] hits = Physics2D.RaycastAll(transform.position, transform.localRotation * Vector3.up, attack.GetRange());
|
|
|
|
|
List<RaycastHit2D> rh = new List<RaycastHit2D>(hits);
|
|
|
|
|
RaycastHit2D hit = rh.Find(x => x.fraction != 0);
|
|
|
|
|
if ( hit.collider != null && hit.collider.gameObject == victim ) {
|
|
|
|
|
Debug.Log("Attacking Player!!!");
|
2018-04-22 23:50:39 +02:00
|
|
|
|
attack.Attack();
|
|
|
|
|
nextAttackTime = Time.timeSinceLevelLoad + attack.GetCooldownTime();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector3 distanceToEnemy = victim.transform.position - gameObject.transform.position;
|
2018-04-23 00:35:32 +02:00
|
|
|
|
//rotation
|
|
|
|
|
Vector3 localRotation = gameObject.transform.localRotation * Vector3.up;
|
|
|
|
|
float angleToRotate = Mathf.Round(Vector3.SignedAngle(localRotation, distanceToEnemy.normalized, Vector3.forward));
|
|
|
|
|
gameObject.transform.Rotate(0, 0, angleToRotate * rotationSpeed);
|
2018-04-22 23:50:39 +02:00
|
|
|
|
|
2018-04-23 00:35:32 +02:00
|
|
|
|
if ( distanceToEnemy.magnitude < attack.GetRange() ) {
|
2018-04-22 23:50:39 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// movement
|
|
|
|
|
body.velocity = new Vector2(distanceToEnemy.normalized.x, distanceToEnemy.normalized.y) * speed;
|
|
|
|
|
|
2018-04-23 00:35:32 +02:00
|
|
|
|
|
2018-04-22 23:50:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetVictim(GameObject g) {
|
|
|
|
|
victim = g;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|