91 lines
2.2 KiB
C#
91 lines
2.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
public class BloodSucker : MonoBehaviour {
|
|
|
|
[SerializeField] float suckDuration = 1f;
|
|
[SerializeField] float suckRange = 1f;
|
|
|
|
Entity currentTarget;
|
|
VampireEntity vampireEntity;
|
|
bool isSucking;
|
|
float currentSuckTimer;
|
|
|
|
void Awake() {
|
|
vampireEntity = GetComponent<VampireEntity>();
|
|
}
|
|
|
|
void FixedUpdate() {
|
|
if (currentTarget == null) {
|
|
SearchSuckTarget();
|
|
}
|
|
|
|
if (currentTarget != null) {
|
|
if (Vector3.Distance(currentTarget.transform.position, transform.position) > suckRange) {
|
|
SetTarget(null);
|
|
} else {
|
|
if (isSucking) {
|
|
PerformSuck(Time.fixedDeltaTime);
|
|
// } else {
|
|
// HighlightTarget();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ToggleSuck(InputAction.CallbackContext context) {
|
|
if (context.performed) {
|
|
if (currentTarget != null) {
|
|
isSucking = true;
|
|
currentSuckTimer = suckDuration;
|
|
}
|
|
} else if (context.canceled) {
|
|
isSucking = false;
|
|
}
|
|
}
|
|
|
|
void SearchSuckTarget() {
|
|
foreach (Collider2D coll in Physics2D.OverlapCircleAll(transform.position, suckRange)) {
|
|
Entity entity = coll.GetComponent<Entity>();
|
|
if (entity != null && entity.gameObject != gameObject) {
|
|
// TODO : check if target is dead
|
|
SetTarget(entity);
|
|
}
|
|
}
|
|
}
|
|
|
|
void SetTarget(Entity newTarget) {
|
|
if (currentTarget != null) {
|
|
UnHighlightTarget();
|
|
}
|
|
|
|
currentTarget = newTarget;
|
|
|
|
if (currentTarget != null) {
|
|
// print("new target : " + currentTarget.name);
|
|
HighlightTarget();
|
|
}
|
|
}
|
|
|
|
void PerformSuck(float deltaTime) {
|
|
currentSuckTimer -= deltaTime;
|
|
if (currentSuckTimer < 0f) {
|
|
// print("One token sucked");
|
|
// TODO check if no token left
|
|
SetTarget(null);
|
|
isSucking = false;
|
|
}
|
|
}
|
|
|
|
void HighlightTarget() {
|
|
|
|
}
|
|
|
|
void UnHighlightTarget() {
|
|
|
|
}
|
|
|
|
}
|