Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public abstract class BaseAILogic : MonoBehaviour
- {
- public ShipHealth ship_Hp;
- public ShipHealth enemy_Hp;
- public ShipHealth ally_Hp;
- public float moveSpeed;
- public float rotSpeed;
- public float hpFleeThreshold;
- public float stoppingDist;
- public float fleeDist;
- protected float currDistP;
- protected float currDistD;
- protected Vector2 destination;
- public GameObject shipAI;
- public GameObject Player;
- public GameObject Ally;
- public bool isFleeing;
- public bool isMoving;
- public int LogicID;
- public Vector3 startLoc;
- // Start is called before the first frame update
- public virtual void Init()
- {
- Debug.Log("Initializing AI values");
- startLoc = transform.position;
- ship_Hp = shipAI.GetComponent<ShipHealth>();
- Player = GameObject.FindGameObjectWithTag("Player_C");
- enemy_Hp = Player.GetComponent<ShipHealth>();
- if (Ally != null)
- {
- ally_Hp = Ally.GetComponent<ShipHealth>();
- }
- }
- public float getDistance(Vector2 targetPos)
- {
- float distToTarget = Vector2.Distance(this.gameObject.transform.position, targetPos);
- return distToTarget;
- }
- public virtual void MoveToDestination(float distToPos,Vector2 targetPos,float stoppingDist)
- {
- if (distToPos >= stoppingDist)
- {
- this.transform.Translate((moveSpeed * 0.1f) * Time.deltaTime, 0.0f,0.0f);
- LookAtTarget((Vector3)targetPos);
- }
- }
- public virtual void Flee(Vector2 location,float fleeDist)
- {
- Vector2 fleeVector = (location - (Vector2)this.transform.position) * fleeDist;
- destination = (Vector2)this.transform.position - fleeVector;
- MoveToDestination(getDistance(destination), destination, 0.0f);
- }
- void LookAtTarget(Vector3 targetPos)
- {
- Vector3 direction = targetPos - this.transform.position;
- float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
- Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
- transform.rotation = Quaternion.Slerp(transform.rotation, rotation, (rotSpeed * 0.01f) * Time.deltaTime);
- }
- Vector2 wanderTarget = Vector2.zero;
- public void Wander(float wanderRadius,float wanderDistance,float wanderJitter)
- {
- wanderTarget += new Vector2(Random.Range(-1.0f, 1.0f) * wanderJitter,
- Random.Range(-1.0f, 1.0f) * wanderJitter);
- wanderTarget.Normalize();
- wanderTarget *= wanderRadius;
- Vector2 targetLocal = wanderTarget + new Vector2(wanderDistance, 0);
- Vector2 targetWorld = this.gameObject.transform.InverseTransformVector(targetLocal);
- MoveToDestination(getDistance(targetWorld),targetWorld,0.0f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement