133 lines
4.1 KiB
C#
133 lines
4.1 KiB
C#
|
|
using System;
|
||
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.InputSystem;
|
||
|
|
|
||
|
|
public class CameraController : MonoBehaviour
|
||
|
|
{
|
||
|
|
[SerializeField] private float moveSpeed;
|
||
|
|
[SerializeField] private float rotationSpeed;
|
||
|
|
[SerializeField] private float zoomSpeed;
|
||
|
|
[SerializeField] private float minZoom;
|
||
|
|
[SerializeField] private float maxZoom;
|
||
|
|
[SerializeField] private Texture2D defaultCursor, buildCursor, destroyCursor, attackCursor, interactCursor, constructionCursor;
|
||
|
|
private Vector2 moveInput;
|
||
|
|
private float rotateInput;
|
||
|
|
private float zoomInput;
|
||
|
|
|
||
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||
|
|
void Start()
|
||
|
|
{
|
||
|
|
Cursor.lockState = CursorLockMode.Confined;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Update is called once per frame
|
||
|
|
void Update()
|
||
|
|
{
|
||
|
|
ApplyCursorTexture();
|
||
|
|
ApplyMovement();
|
||
|
|
ApplyRotation();
|
||
|
|
ApplyZoom();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void ApplyCursorTexture()
|
||
|
|
{
|
||
|
|
var camera = Camera.main;
|
||
|
|
if (camera == null)
|
||
|
|
return;
|
||
|
|
|
||
|
|
Vector2 mousePosition = Mouse.current.position.ReadValue();
|
||
|
|
Ray ray = camera.ScreenPointToRay(mousePosition);
|
||
|
|
|
||
|
|
if (Physics.Raycast(ray, out RaycastHit hitInfo, Mathf.Infinity))
|
||
|
|
{
|
||
|
|
if (hitInfo.collider.TryGetComponent(out Building building))
|
||
|
|
{
|
||
|
|
Building.BuildingState buildingState = building.GetBuildingState();
|
||
|
|
switch (buildingState)
|
||
|
|
{
|
||
|
|
case Building.BuildingState.Unbuilt:
|
||
|
|
Cursor.SetCursor(buildCursor, Vector2.zero, CursorMode.Auto);
|
||
|
|
break;
|
||
|
|
case Building.BuildingState.Constructing:
|
||
|
|
Cursor.SetCursor(constructionCursor, Vector2.zero, CursorMode.Auto);
|
||
|
|
break;
|
||
|
|
case Building.BuildingState.Active:
|
||
|
|
Cursor.SetCursor(interactCursor, Vector2.zero, CursorMode.Auto);
|
||
|
|
break;
|
||
|
|
case Building.BuildingState.Destroyed:
|
||
|
|
Cursor.SetCursor(destroyCursor, Vector2.zero, CursorMode.Auto);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
Cursor.SetCursor(defaultCursor, Vector2.zero, CursorMode.Auto);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// called when we press down the left mouse interact input
|
||
|
|
public void OnInteractInput(InputAction.CallbackContext context)
|
||
|
|
{
|
||
|
|
if (!context.performed)
|
||
|
|
return;
|
||
|
|
|
||
|
|
var camera = Camera.main;
|
||
|
|
if (camera == null)
|
||
|
|
return;
|
||
|
|
|
||
|
|
Vector2 mousePosition = Mouse.current.position.ReadValue();
|
||
|
|
Ray ray = camera.ScreenPointToRay(mousePosition);
|
||
|
|
|
||
|
|
if (Physics.Raycast(ray, out RaycastHit hitInfo, Mathf.Infinity))
|
||
|
|
{
|
||
|
|
if (hitInfo.collider.TryGetComponent(out Building building))
|
||
|
|
{
|
||
|
|
//building.Interact();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void OnMoveInput(InputAction.CallbackContext context)
|
||
|
|
{
|
||
|
|
moveInput = context.ReadValue<Vector2>();
|
||
|
|
}
|
||
|
|
|
||
|
|
public void OnRotateInput(InputAction.CallbackContext context)
|
||
|
|
{
|
||
|
|
rotateInput = context.ReadValue<float>();
|
||
|
|
}
|
||
|
|
|
||
|
|
public void OnZoomInput(InputAction.CallbackContext context)
|
||
|
|
{
|
||
|
|
zoomInput = context.ReadValue<float>();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void ApplyMovement()
|
||
|
|
{
|
||
|
|
if (moveInput == Vector2.zero)
|
||
|
|
return;
|
||
|
|
|
||
|
|
Vector3 moveDirection = new Vector3(moveInput.x, 0f, moveInput.y);
|
||
|
|
transform.Translate(moveDirection * moveSpeed * Time.deltaTime, Space.World);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void ApplyRotation()
|
||
|
|
{
|
||
|
|
if (Mathf.Approximately(rotateInput, 0f))
|
||
|
|
return;
|
||
|
|
|
||
|
|
transform.Rotate(Vector3.up, rotateInput * rotationSpeed * Time.deltaTime);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void ApplyZoom()
|
||
|
|
{
|
||
|
|
if (Mathf.Approximately(zoomInput, 0f))
|
||
|
|
return;
|
||
|
|
|
||
|
|
float newY = Mathf.Clamp(transform.position.y + zoomInput * zoomSpeed * Time.deltaTime, minZoom, maxZoom);
|
||
|
|
transform.position = new Vector3(transform.position.x, newY, transform.position.z);
|
||
|
|
}
|
||
|
|
}
|