#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 Entity[] minionPrefabs = null!; public GameObject aimArrow = null!; bool isInThrowMode; Vector2 throwDirection; MinionBar minionBar = null!; VampireEntity vampireEntity = null!; PlayerMovement movement = null!; float currentCooldownTimer; float currentInitialCooldown; void Awake() { vampireEntity = GetComponent(); movement = GetComponent(); minionBar = FindObjectOfType(); aimArrow.SetActive(false); } void Start() { foreach (Entity minion in minionPrefabs) { minionBar.AddMinionType(minion); } minionBar.UpdateReload(0f); } void FixedUpdate() { if (currentCooldownTimer > 0f) { currentCooldownTimer -= Time.fixedDeltaTime; minionBar.UpdateReload(currentCooldownTimer / currentInitialCooldown); } } public void ToggleThrowMode(InputAction.CallbackContext context) { if (!gameFlowManager.CanDoAction) return; if (context.performed) { isInThrowMode = true; aimArrow.SetActive(true); } else if (context.canceled && throwDirection.magnitude >= playerStats.MinJoystickValueForThrowing) { PerformThrow(); isInThrowMode = false; aimArrow.SetActive(false); } } public void AimThrow(InputAction.CallbackContext context) { if (!gameFlowManager.CanDoAction) return; throwDirection = context.ReadValue(); if (throwDirection.sqrMagnitude > 1f) throwDirection.Normalize(); if (vampireEntity.playerMovement.facingRight) { aimArrow.transform.rotation = Quaternion.FromToRotation(Vector2.right, throwDirection); } else { aimArrow.transform.rotation = Quaternion.FromToRotation(Vector2.left, throwDirection); } } void PerformThrow() { if (!isInThrowMode || currentCooldownTimer > 0f) { return; } float minionHealthCost = 10f; // TODO if (minionHealthCost >= vampireEntity.Health) { return; } vampireEntity.TakeDamage(minionHealthCost, vampireEntity); currentInitialCooldown = 2f; // TODO currentCooldownTimer = currentInitialCooldown; minionBar.UpdateReload(currentCooldownTimer / currentInitialCooldown); var newMinion = Instantiate(minionBar.GetCurrentMinion().gameObject, arena.minionParent) .GetComponent(); newMinion.arena = arena; if (throwDirection == Vector2.zero) Debug.LogWarning("Throwing with a null throwDirection."); Vector2 normalizedDirection = throwDirection.normalized; newMinion.transform.position = transform.position + (Vector3)normalizedDirection; newMinion.direction = normalizedDirection; newMinion.gameFlowManager = vampireEntity.gameFlowManager; if (movement.GetSafeZoneIfImmobile() is { } safeZone) { newMinion.thrownFromSafeZone = true; newMinion.thrownTargetPosition = safeZone.GetOutsidePosition(normalizedDirection); } } }