92 lines
2.8 KiB
C#
Raw Permalink Normal View History

2024-10-17 17:23:05 +03:00
using Mirror;
using Player.States;
using StateMachine;
using UnityEngine;
namespace Player
{
public class Player : MonoBehaviour
{
public float walkSpeed = 400f;
public float runSpeed = 1200f;
public float jumpForce = 160f;
public float groundCheckRadius = 0.14f;
public Transform groundCheck;
public LayerMask groundLayer;
public Animator animator;
public readonly StateMachine<PlayerState> StateMachine = new StateMachine<PlayerState>();
2024-10-18 23:36:46 +03:00
public GameObject laptopPrefab;
2024-10-17 17:23:05 +03:00
[HideInInspector] public Rigidbody playerRigidbody;
2024-10-17 23:09:51 +03:00
public bool isGrounded;
2024-10-17 17:23:05 +03:00
[HideInInspector] public Vector3 moveDirection;
private float _originalHeight;
void Start()
{
playerRigidbody = GetComponent<Rigidbody>();
StateMachine.Add(new Walk(this));
StateMachine.Add(new Run(this));
StateMachine.Add(new Jump(this));
2024-10-18 23:36:46 +03:00
StateMachine.Add(new MenuState(this));
2024-10-17 17:23:05 +03:00
StateMachine.SetCurrentState(PlayerState.Walk);
}
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundCheckRadius, groundLayer);
2024-10-17 23:09:51 +03:00
moveDirection = (transform.right * Input.GetAxis("Horizontal") + transform.forward * Input.GetAxis("Vertical")).normalized;
2024-10-17 17:23:05 +03:00
animator.SetFloat("x", Input.GetAxis("Horizontal"));
animator.SetFloat("y", Input.GetAxis("Vertical"));
if (isGrounded && Input.GetButtonDown("Jump"))
{
StateMachine.SetCurrentState(PlayerState.Jump);
}
2024-10-18 23:36:46 +03:00
if (Input.GetKeyDown(KeyCode.Escape) && StateMachine.GetCurrentState().ID != PlayerState.Menu)
{
StateMachine.SetCurrentState(PlayerState.Menu);
}
2024-10-17 17:23:05 +03:00
2024-10-17 23:09:51 +03:00
StateMachine.Update();
2024-10-17 17:23:05 +03:00
}
void FixedUpdate()
{
StateMachine.FixedUpdate();
}
void OnDrawGizmosSelected()
{
if (groundCheck != null)
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
}
}
2024-10-18 23:36:46 +03:00
private GameObject _laptopInstance;
public void OpenMenu()
{
2024-10-19 02:54:12 +03:00
Vector3 spawnPosition = transform.position + transform.up * 8f + transform.forward * 5f;
2024-10-18 23:36:46 +03:00
_laptopInstance = Instantiate(laptopPrefab, spawnPosition, Quaternion.identity);
_laptopInstance.transform.SetParent(transform);
Debug.Log("Open menu");
Debug.Log(_laptopInstance.transform.position);
}
public void CloseMenu()
{
Destroy(_laptopInstance);
Debug.Log("DESTROY");
}
2024-10-17 17:23:05 +03:00
}
}