141 lines
3.5 KiB
C#
141 lines
3.5 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(Rigidbody2D), typeof(Damageable))]
|
|
public class NPCMove : MonoBehaviour
|
|
{
|
|
public float walkSpeed = 3f;
|
|
public float walkStopRate = 0.6f;
|
|
public DetectionZone attackZone;
|
|
|
|
Rigidbody2D rb;
|
|
TouchingDirection touchingDirection;
|
|
Animator animator;
|
|
Damageable damageable;
|
|
|
|
public enum WalkableDirection { Right, Left }
|
|
|
|
private WalkableDirection _walkDirection;
|
|
|
|
public Vector2 walkableDirectionVector;
|
|
|
|
public WalkableDirection WalkDirection
|
|
{
|
|
get
|
|
{
|
|
return _walkDirection;
|
|
}
|
|
set
|
|
{
|
|
if (_walkDirection != value)
|
|
{
|
|
// flip direction
|
|
gameObject.transform.localScale = new Vector2(gameObject.transform.localScale.x * -1, gameObject.transform.localScale.y);
|
|
|
|
if (value == WalkableDirection.Right)
|
|
{
|
|
walkableDirectionVector = Vector2.right;
|
|
}
|
|
else if (value == WalkableDirection.Left)
|
|
{
|
|
walkableDirectionVector = Vector2.left;
|
|
}
|
|
}
|
|
_walkDirection = value;
|
|
}
|
|
}
|
|
|
|
public bool _hasTarget = false;
|
|
|
|
public bool HasTarget {
|
|
get
|
|
{
|
|
return _hasTarget;
|
|
}
|
|
private set
|
|
{
|
|
_hasTarget = value;
|
|
animator.SetBool(AnimationStrings.hasTarget, value);
|
|
}
|
|
}
|
|
|
|
public bool CanMove
|
|
{
|
|
get
|
|
{
|
|
return animator.GetBool(AnimationStrings.canMove);
|
|
}
|
|
}
|
|
|
|
public float AttackCooldown
|
|
{
|
|
get
|
|
{
|
|
return animator.GetFloat(AnimationStrings.attackCooldown);
|
|
}
|
|
private set
|
|
{
|
|
animator.SetFloat(AnimationStrings.attackCooldown, Mathf.Max(value, 0));
|
|
}
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
rb = GetComponent<Rigidbody2D>();
|
|
touchingDirection = GetComponent<TouchingDirection>();
|
|
animator = GetComponent<Animator>();
|
|
damageable = GetComponent<Damageable>();
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
HasTarget = attackZone.detectedCollider.Count > 0;
|
|
if (AttackCooldown > 0)
|
|
{
|
|
AttackCooldown -= Time.deltaTime;
|
|
}
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (touchingDirection.IsOnWall && touchingDirection.IsGrounded)
|
|
{
|
|
FlipDirection();
|
|
}
|
|
|
|
if (!damageable.LockVelocity)
|
|
{
|
|
if (CanMove)
|
|
{
|
|
rb.velocity = new Vector2(walkSpeed * walkableDirectionVector.x, rb.velocity.y);
|
|
}
|
|
else
|
|
{
|
|
rb.velocity = new Vector2(Mathf.Lerp(rb.velocity.x, 0, walkStopRate), rb.velocity.y);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void FlipDirection()
|
|
{
|
|
if (WalkDirection == WalkableDirection.Right)
|
|
{
|
|
WalkDirection = WalkableDirection.Left;
|
|
} else if (WalkDirection == WalkableDirection.Left)
|
|
{
|
|
WalkDirection = WalkableDirection.Right;
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Current walkable direction is not set to legal value lef of right");
|
|
}
|
|
}
|
|
public void OnHit(int damage, Vector2 knockback)
|
|
{
|
|
rb.velocity = new Vector2(knockback.x, rb.velocity.y + knockback.y);
|
|
}
|
|
}
|