This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
project-undercover/Project Undercover/Assets/Scripts/CharacterStateMachine/ActionScripts/IdleClickAction.cs

91 lines
3.3 KiB
C#
Raw Normal View History

2017-10-03 04:15:13 -05:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.EventSystems;
2017-10-03 04:15:13 -05:00
[CreateAssetMenu(menuName = "CharacterStateMachine/Actions/IdleClick")]
public class IdleClickAction : Action
{
public GameObject wayPointPrefab;
public override void StartAct(StateController controller)
{
ProgressPanelController.ActivePanel.Hide();
controller.SelectedInteraction = null;
2017-10-08 18:37:19 -05:00
controller.Interactor = null;
controller.SelectedObject = null;
2017-10-09 18:55:07 -05:00
controller.characterAnimator.SetBool(CharacterAnimator.Params.Interacting, false);
}
2017-10-03 04:15:13 -05:00
public override void Act(StateController controller)
{
if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
2017-10-03 04:15:13 -05:00
{
if (controller.Interactor)
2017-10-03 04:15:13 -05:00
{
controller.Interactor = null;
InteractionPanelController.ActivePanel.Hide();
2017-10-03 04:15:13 -05:00
}
else
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// Check first if the player clicked on a selectable object
SelectableObject selectableObject;
if (RaycastForSelectableObject(controller, ray, out selectableObject))
{
controller.SelectedObject = selectableObject;
return;
}
2017-10-03 04:15:13 -05:00
// At this point, the player didn't click on a selectable object,
// so the player is probably issuing a move command.
controller.SelectedObject = null;
RaycastToMoveController(controller, ray);
}
2017-10-03 04:15:13 -05:00
}
// If the player has selected an object, move the player toward that object
2017-10-08 18:37:19 -05:00
if (controller.SelectedObject && !controller.IsInteracting)
controller.Destination = controller.SelectedObject.transform.position;
2017-10-03 04:15:13 -05:00
}
// Check if StateController clicked on a Selectable Object
2017-10-03 04:15:13 -05:00
bool RaycastForSelectableObject(StateController controller, Ray ray, out SelectableObject selectableObject)
{
int layerMask = LayerMask.NameToLayer("SelectableObject");
int mask = 1 << layerMask;
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100.0f, mask))
{
SelectableObject selectable = hit.collider.gameObject.GetComponentInParent<SelectableObject>();
2017-10-08 18:37:19 -05:00
if (selectable != null && selectable != controller && selectable.HasInteractions())
2017-10-03 04:15:13 -05:00
{
selectableObject = selectable;
return true;
}
}
selectableObject = null;
return false;
}
// Check if StateController clicked on the floor to issue a move command
2017-10-03 04:15:13 -05:00
bool RaycastToMoveController(StateController controller, Ray ray)
{
int layerMask = LayerMask.NameToLayer("Floor");
int mask = 1 << layerMask;
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100.0f, mask))
{
NavMeshHit navhit;
if (NavMesh.SamplePosition(hit.point, out navhit, 1.0f, NavMesh.AllAreas))
{
controller.Destination = navhit.position;
Instantiate(wayPointPrefab, navhit.position, Quaternion.identity);
2017-10-03 04:15:13 -05:00
return true;
}
}
return false;
}
}