137 lines
4.3 KiB
C#
137 lines
4.3 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;
|
|
[SerializeField] float healthGainFromSuck = 30f;
|
|
[SerializeField] ParticleSystem bloodParticles;
|
|
[SerializeField] AudioSource suckingSource;
|
|
[SerializeField] AudioClip[] suckingSounds;
|
|
[SerializeField] AudioSource suckSource;
|
|
[SerializeField] AudioClip[] suckSounds;
|
|
|
|
Entity currentTarget;
|
|
VampireEntity vampireEntity;
|
|
bool isSucking;
|
|
float currentSuckTimer;
|
|
[HideInInspector] public SoundManager soundManager;
|
|
|
|
void Awake() {
|
|
vampireEntity = GetComponent<VampireEntity>();
|
|
bloodParticles.Stop(true, ParticleSystemStopBehavior.StopEmitting);
|
|
soundManager = FindObjectOfType<SoundManager>();
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void Update() {
|
|
if (currentTarget != null && currentTarget.bloodTokens <= 0) {
|
|
SetTarget(null);
|
|
}
|
|
}
|
|
|
|
public void ToggleSuck(InputAction.CallbackContext context) {
|
|
if (context.performed) {
|
|
if (currentTarget != null) {
|
|
SetIsSucking(true);
|
|
currentSuckTimer = suckDuration;
|
|
}
|
|
} else if (context.canceled) {
|
|
SetIsSucking(false);
|
|
}
|
|
}
|
|
|
|
void SearchSuckTarget() {
|
|
foreach (Collider2D coll in Physics2D.OverlapCircleAll(transform.position, suckRange)) {
|
|
Entity entity = coll.GetComponent<Entity>();
|
|
if (entity != null && entity.gameObject != gameObject) {
|
|
if (!entity.IsAlive() && entity.bloodTokens > 0) {
|
|
SetTarget(entity);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void SetTarget(Entity newTarget) {
|
|
if(newTarget != currentTarget) {
|
|
SetIsSucking(false);
|
|
}
|
|
if (currentTarget != null) {
|
|
UnHighlightTarget();
|
|
}
|
|
|
|
currentTarget = newTarget;
|
|
|
|
if (currentTarget != null) {
|
|
// print("new target : " + currentTarget.name);
|
|
HighlightTarget();
|
|
}
|
|
}
|
|
|
|
void PerformSuck(float deltaTime) {
|
|
if(currentTarget.bloodTokens <= 0){
|
|
SetIsSucking(false);
|
|
SetTarget(null);
|
|
}
|
|
bloodParticles.gameObject.transform.rotation = Quaternion.FromToRotation(transform.right, currentTarget.transform.position - transform.position);
|
|
bloodParticles.gameObject.transform.localScale = Vector3.one * (Vector3.Distance(currentTarget.transform.position, transform.position) + 1f) / 4f;
|
|
|
|
currentSuckTimer -= deltaTime;
|
|
if (currentSuckTimer < 0f) {
|
|
currentTarget.bloodTokens -= 1;
|
|
vampireEntity.HealDamage(healthGainFromSuck);
|
|
// print("One token sucked");
|
|
soundManager.PlaySound(suckSource, suckSounds, randomPitch:true, createTempSourceIfBusy:true);
|
|
if (currentTarget.bloodTokens == 0) {
|
|
SetIsSucking(false);
|
|
SetTarget(null);
|
|
// TODO : Dispawn target
|
|
} else {
|
|
currentSuckTimer = suckDuration;
|
|
}
|
|
}
|
|
}
|
|
|
|
void SetIsSucking(bool value) {
|
|
isSucking = value;
|
|
if(isSucking) {
|
|
bloodParticles.Play();
|
|
currentTarget.OnSuck(true);
|
|
soundManager.PlaySound(suckingSource, suckingSounds, randomPitch: true, createTempSourceIfBusy: false);
|
|
} else {
|
|
bloodParticles.Stop(true, ParticleSystemStopBehavior.StopEmitting);
|
|
if(!(currentTarget is null))currentTarget.OnSuck(false);
|
|
soundManager.StopSound(suckingSource);
|
|
}
|
|
}
|
|
|
|
void HighlightTarget() {
|
|
currentTarget.EnableHalo();
|
|
}
|
|
|
|
void UnHighlightTarget() {
|
|
currentTarget.DisableHalo();
|
|
}
|
|
|
|
}
|