ludumdare50/Assets/Scripts/MinionThrower.cs
2022-04-04 11:43:47 -04:00

115 lines
3.7 KiB
C#

#nullable enable
using NaughtyAttributes;
using UnityEngine;
using UnityEngine.InputSystem;
public class MinionThrower : MonoBehaviour {
[SerializeField] [Required]
Arena arena = null!;
[SerializeField] [Required]
GameFlowManager gameFlowManager = null!;
[SerializeField] [Required]
PlayerStats playerStats = null!;
public AIEntity[] minionPrefabs = null!;
public GameObject aimArrow = null!;
bool isInThrowMode;
Vector2 throwInput;
Vector2 lastThrowInput;
MinionBar minionBar = null!;
VampireEntity vampireEntity = null!;
PlayerMovement movement = null!;
float currentCooldownTimer;
void Awake() {
vampireEntity = GetComponent<VampireEntity>();
movement = GetComponent<PlayerMovement>();
minionBar = FindObjectOfType<MinionBar>();
aimArrow.SetActive(false);
lastThrowInput = Vector3.up;
}
void Start() {
foreach (AIEntity minion in minionPrefabs) {
minionBar.AddMinionType(minion);
}
minionBar.UpdateReload(0f);
}
void FixedUpdate() {
if (currentCooldownTimer > 0f) {
currentCooldownTimer -= Time.fixedDeltaTime;
minionBar.UpdateReload(currentCooldownTimer / playerStats.currentInitialCooldown);
}
}
public void ToggleThrowMode(InputAction.CallbackContext context) {
if (!gameFlowManager.CanDoAction)
return;
if (context.performed) {
isInThrowMode = true;
aimArrow.SetActive(true);
} else if (context.canceled) {
PerformThrow();
isInThrowMode = false;
aimArrow.SetActive(false);
}
if (movement.currentState is PlayerMovement.ImmobileMovementState immobileMovementState)
immobileMovementState.isThrowing = isInThrowMode;
}
public void AimThrow(InputAction.CallbackContext context) {
if (!gameFlowManager.CanDoAction)
return;
throwInput = context.ReadValue<Vector2>();
if (throwInput != Vector2.zero) {
if (throwInput.sqrMagnitude > 1f)
throwInput.Normalize();
lastThrowInput = throwInput;
}
if (vampireEntity.playerMovement.facingRight) {
aimArrow.transform.rotation = Quaternion.FromToRotation(Vector2.right, lastThrowInput);
} else {
aimArrow.transform.rotation = Quaternion.FromToRotation(Vector2.left, lastThrowInput);
}
}
void PerformThrow() {
if (!isInThrowMode || currentCooldownTimer > 0f) {
return;
}
float minionHealthCost = minionBar.GetCurrentMinion().cost;
if (minionHealthCost >= vampireEntity.Health) {
return;
}
// if (!movement.IsInSafeZone)
vampireEntity.TakeDamage(minionHealthCost, vampireEntity, intentional: true);
currentCooldownTimer = playerStats.currentInitialCooldown;
minionBar.UpdateReload(currentCooldownTimer / playerStats.currentInitialCooldown);
var newMinion = Instantiate(minionBar.GetCurrentMinion().gameObject, arena.minionParent)
.GetComponent<Monster>();
newMinion.arena = arena;
Vector2 normalizedDirection = lastThrowInput.normalized;
newMinion.transform.position = transform.position + (Vector3)normalizedDirection;
newMinion.direction = normalizedDirection;
newMinion.gameFlowManager = vampireEntity.gameFlowManager;
if (movement.currentState is PlayerMovement.ImmobileMovementState immobileMovementState) {
newMinion.thrownFromSafeZone = true;
newMinion.thrownTargetPosition = immobileMovementState.exitPosition;
}
}
}