jimmy tremblay-Bernier c1bf5a4ca1 initial commit
2022-03-12 22:04:30 -04:00

236 lines
6.2 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyMovement : MonoBehaviour
{
public float speed = 10f;
public int swordDamage = 1;
private Transform target;
private Transform[] waypoints;
private int wavepointIndex = 0;
private bool isClimbing;
private bool isPushing;
private bool isEnemyMoevementSocketed;
private float triggerRadius = 1f;
private float climbingRotation;
private Animator enemyAnimator = null;
private Socket socket;
public NavMeshAgent playerAgent;
private Vector3 playerPos;
private PlayerManagement pm;
private void Awake()
{
pm = GameObject.Find("/OmniCharacterController").GetComponent<PlayerManagement>();
}
public void SetupWaypoints(GameObject path)
{
Transform t = path.transform;
waypoints = new Transform[t.childCount];
for (int i = 0; i < waypoints.Length; i++)
{
waypoints[i] = t.GetChild(i);
}
target = waypoints[0];
}
private void RandomTargetModifier()
{
float xRandom = UnityEngine.Random.Range(0f,3f);
float zRandom = UnityEngine.Random.Range(0f,3f);
xRandom = target.position.x + xRandom;
zRandom = target.position.z + zRandom;
Vector3 waypointTemp = new Vector3(xRandom,target.position.y,zRandom);
waypoints[wavepointIndex].position = waypointTemp;
target = waypoints[wavepointIndex];
}
void Update()
{
if (waypoints == null || waypoints.Length == 0)
{
return;
}
else if (wavepointIndex >= waypoints.Length)
{
if (isClimbing)
{
setIsClimbing(false);
}
NavMeshSetup();
}
else if (isEnemyMoevementSocketed)
{
if(Vector3.Distance(transform.position, socket.transform.position) <= 0.4f)
{
transform.position = socket.transform.position;
transform.rotation = socket.transform.rotation;
enemyAnimator.runtimeAnimatorController = Resources.Load("Animations/Push/PushController") as RuntimeAnimatorController;
isPushing = true;
}
else
{
Vector3 dir = socket.transform.position - transform.position;
moveEnemy(dir);
}
//transform.position = socket.transform.position;
}
else
{
Vector3 dir = target.position - transform.position;
moveEnemy(dir);
}
}
void moveEnemy(Vector3 dir)
{
transform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);
if (isClimbing)
{
transform.rotation = Quaternion.Euler(40, climbingRotation, 0);
}
else
{
Quaternion toRotation = Quaternion.LookRotation(dir,Vector3.up);
transform.rotation = Quaternion.Lerp(transform.rotation,toRotation, speed * Time.deltaTime);
}
if(Vector3.Distance(transform.position, target.position) <= 0.3f)
{
GetNextWaypoint();
}
}
private void NavMeshSetup()
{
if (!playerAgent.enabled)
{
playerAgent.enabled = true;
}
NavmeshAgentMovement();
}
void GetNextWaypoint()
{
if(wavepointIndex >= waypoints.Length)
{
return;
}
if (wavepointIndex == 1 && isClimbing)
{
enemyAnimator.runtimeAnimatorController = Resources.Load("Animations/FinishClimbingLadder/FinishClimbingLadderController") as RuntimeAnimatorController;
}
wavepointIndex++;
target = waypoints[wavepointIndex];
//RandomTargetModifier();
}
void NavmeshAgentMovement()
{
if (NearEnemy())
{
enemyAnimator.runtimeAnimatorController = Resources.Load("Animations/SwordSlash/SwordSlashController") as RuntimeAnimatorController;
pm.takeDamage(swordDamage);
}
else
{
enemyAnimator.runtimeAnimatorController = Resources.Load("Animations/Walk/WalkController") as RuntimeAnimatorController;
playerPos = GameObject.FindGameObjectWithTag("Player").transform.position;
playerAgent.SetDestination(playerPos);
}
}
public void SetEnemySocket(Socket socket)
{
isEnemyMoevementSocketed = true;
this.socket = socket;
}
public void removeEnemySocket()
{
if (isEnemyMoevementSocketed)
{
isEnemyMoevementSocketed = false;
isPushing = false;
this.socket.removeEnemyGameobject();
this.socket = null;
}
}
public bool isEnemyMovementSocket(){
return isEnemyMoevementSocketed;
}
public void SetWaypointsTransform(Transform[] waypointTarget)
{
this.waypoints = waypointTarget;
wavepointIndex = 0;
target = waypointTarget[0];
}
public void setIsClimbing(bool value)
{
this.isClimbing = value;
if(isClimbing)
{
enemyAnimator.runtimeAnimatorController = Resources.Load("Animations/ClimbingLadder/ClimbingController") as RuntimeAnimatorController;
}
else
{
enemyAnimator.runtimeAnimatorController = Resources.Load("Animations/Walk/WalkController") as RuntimeAnimatorController;
}
}
public bool getIsClimbin()
{
return this.isClimbing;
}
public void setClimbingRotation(float value)
{
this.climbingRotation = value;
}
public void setEnemyAnimatior(Animator animator)
{
this.enemyAnimator = animator;
}
bool NearEnemy()
{
Collider[] hitColliders = Physics.OverlapSphere(this.transform.position, triggerRadius);
foreach (var hitCollider in hitColliders)
{
if (hitCollider.transform.root.tag == "Player")
{
return true;
}
}
return false;
}
public bool getIsPushing()
{
return this.isPushing;
}
}