MiniGames/Assets/Scripts/SkyWalker/Skywalker_PlayerController.cs
2025-09-01 00:01:33 +05:00

146 lines
3.4 KiB
C#

using DG.Tweening;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class Skywalker_PlayerController : MonoBehaviour
{
[Header("Movement Settings")]
public float moveSpeed = 5f;
public float jumpForce = 10f;
[Header("Ground Check")]
public Transform groundCheck;
public float groundCheckRadius = 0.3f;
public LayerMask groundLayer;
[Header("Combat")]
public GameObject Skywalker_BlueberryBlastPrefab;
[Header("Animation")]
public Animator animator;
[Header("Mobile UI")]
public Joystick joystick; // reference to joystick
private bool jumpPressed = false;
private bool shootPressed = false;
private Rigidbody rb;
private bool isGrounded;
private float move;
private bool facingRight = true;
public Transform shootPoint;
void Start()
{
rb = GetComponent<Rigidbody>();
if (animator == null)
animator = GetComponent<Animator>();
}
void Update()
{
Move();
Jump();
ShootBlast();
UpdateAnimations();
}
void Move()
{
move = joystick != null ? joystick.Horizontal : 0f; // get joystick x-axis
Vector3 velocity = rb.velocity;
velocity.x = move * moveSpeed;
rb.velocity = velocity;
if (move > 0 && !facingRight)
{
Flip();
}
else if (move < 0 && facingRight)
{
Flip();
}
}
void Jump()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundCheckRadius, groundLayer);
if (jumpPressed && isGrounded)
{
rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
animator.SetTrigger("Jump");
jumpPressed = false; // reset after using
}
}
public void ForceJumpForGameOver()
{
rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
if (animator != null)
animator.SetTrigger("Jump");
}
void ShootBlast()
{
if (shootPressed && Skywalker_BlueberryBlastPrefab != null && shootPoint != null)
{
if (Skywalker_BlueberryManager.Instance.UseBlueberry())
{
Instantiate(Skywalker_BlueberryBlastPrefab, shootPoint.position, shootPoint.rotation);
}
else
{
Debug.Log("No blueberries left!");
}
shootPressed = false; // reset after firing
}
}
void UpdateAnimations()
{
animator.SetFloat("Speed", Mathf.Abs(move));
animator.SetBool("IsGrounded", isGrounded);
}
void Flip()
{
facingRight = !facingRight;
float targetY = facingRight ? 90f : 270f;
transform.DORotate(new Vector3(0f, targetY, 0f), 0.1f, RotateMode.Fast);
}
void OnTriggerEnter(Collider col)
{
if (col.CompareTag("Blueberry"))
{
Destroy(col.gameObject);
}
if (col.CompareTag("Trap"))
{
Debug.Log("Hit Trap!");
}
}
void OnDrawGizmosSelected()
{
if (groundCheck != null)
{
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
}
}
// --- Called by UI Buttons ---
public void OnJumpButtonPressed()
{
jumpPressed = true;
}
public void OnShootButtonPressed()
{
shootPressed = true;
}
}