2022-11-23 21:02:33 +08:00
|
|
|
|
/*********************************************************************
|
|
|
|
|
* \file LeafAttack.cs
|
|
|
|
|
* \author Ryan Wang Nian Jing
|
|
|
|
|
* \brief Leaf node implementation for AI attacking the player
|
|
|
|
|
* when the AI is close enough after chasing
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* \copyright Copyright (c) 2022 DigiPen Institute of Technology. Reproduction
|
|
|
|
|
or disclosure of this file or its contents without the prior written
|
|
|
|
|
consent of DigiPen Institute of Technology is prohibited.
|
|
|
|
|
*********************************************************************/
|
|
|
|
|
|
|
|
|
|
using SHADE;
|
|
|
|
|
using SHADE_Scripting.AIBehaviour.BehaviourTree;
|
2023-03-04 04:57:59 +08:00
|
|
|
|
using SHADE_Scripting.Audio;
|
2022-11-23 21:02:33 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
//VARIABLES
|
|
|
|
|
public partial class LeafAttack : BehaviourTreeNode
|
|
|
|
|
{
|
2022-11-25 20:16:31 +08:00
|
|
|
|
//Holds the player game object that is to be targeted
|
2022-11-23 21:02:33 +08:00
|
|
|
|
private GameObject player;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//FUNCTIONS
|
|
|
|
|
public partial class LeafAttack : BehaviourTreeNode
|
|
|
|
|
{
|
2022-11-25 16:12:58 +08:00
|
|
|
|
public LeafAttack(string name) : base (name)
|
2022-11-23 21:02:33 +08:00
|
|
|
|
{
|
2022-11-25 20:16:31 +08:00
|
|
|
|
//Debug.Log("LeafAttack ctor");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Helper, find the nearest unobstructed waypoint to return to when chase is over
|
|
|
|
|
public void reevaluateWaypoint()
|
|
|
|
|
{
|
|
|
|
|
List<GameObject> waypoints = (List<GameObject>)GetNodeData("waypoints");
|
|
|
|
|
Transform transform = (Transform)GetNodeData("transform");
|
|
|
|
|
|
|
|
|
|
if (waypoints == null || transform == null)
|
|
|
|
|
{
|
|
|
|
|
SetNodeData("currentWaypointIndex", 0);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int nearestWaypointIndex = 0;
|
|
|
|
|
for (int i = 0; i < waypoints.Count; ++i)
|
|
|
|
|
{
|
|
|
|
|
if ((transform.GlobalPosition - waypoints[i].GetComponent<Transform>().GlobalPosition).GetSqrMagnitude() <
|
|
|
|
|
(transform.GlobalPosition - waypoints[nearestWaypointIndex].GetComponent<Transform>().GlobalPosition).GetSqrMagnitude())
|
|
|
|
|
{
|
|
|
|
|
nearestWaypointIndex = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
SetNodeData("currentWaypointIndex", nearestWaypointIndex);
|
2022-11-23 21:02:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override BehaviourTreeNodeStatus Evaluate()
|
|
|
|
|
{
|
2022-11-25 20:16:31 +08:00
|
|
|
|
//Debug.LogWarning("LeafAttack");
|
2022-11-23 21:02:33 +08:00
|
|
|
|
//Fail if no target in blackboard?
|
|
|
|
|
|
|
|
|
|
onEnter(BehaviourTreeNodeStatus.RUNNING);
|
|
|
|
|
|
2022-11-25 16:12:58 +08:00
|
|
|
|
//Get Data
|
|
|
|
|
float? captureTime;
|
|
|
|
|
if (GetNodeData("captureTimeLeft") == null)
|
2022-11-25 11:44:58 +08:00
|
|
|
|
{
|
|
|
|
|
status = BehaviourTreeNodeStatus.FAILURE;
|
|
|
|
|
onExit(BehaviourTreeNodeStatus.FAILURE);
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2022-11-25 16:12:58 +08:00
|
|
|
|
else
|
2022-11-25 20:16:31 +08:00
|
|
|
|
captureTime = (float)GetNodeData("captureTimeLeft");
|
2022-11-25 16:12:58 +08:00
|
|
|
|
|
|
|
|
|
if (GameObject.Find("Player") == null)
|
|
|
|
|
{
|
|
|
|
|
status = BehaviourTreeNodeStatus.FAILURE;
|
|
|
|
|
onExit(BehaviourTreeNodeStatus.FAILURE);
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
player = GameObject.Find("Player").GetValueOrDefault();
|
|
|
|
|
|
|
|
|
|
//Succeed when stand in hurt box for long enough
|
|
|
|
|
//Debug.Log("Attempting to get blackboard data");
|
|
|
|
|
//Debug.Log("Got blackboard data");
|
2022-11-23 21:02:33 +08:00
|
|
|
|
captureTime -= Time.DeltaTimeF;
|
|
|
|
|
SetNodeData("captureTimeLeft", captureTime);
|
|
|
|
|
//Debug.Log(captureTime.ToString());
|
|
|
|
|
if (captureTime <= 0.0f)
|
|
|
|
|
{
|
|
|
|
|
//Catch player when in range for long enough
|
2022-11-25 20:16:31 +08:00
|
|
|
|
//Debug.Log("Success: Caught");
|
2022-11-23 21:02:33 +08:00
|
|
|
|
player.GetScript<PlayerController>().currentState = PlayerController.RaccoonStates.CAUGHT;
|
2023-01-25 18:20:26 +08:00
|
|
|
|
if (player.GetScript<PlayerController>().stateMachine && !player.GetScript<PlayerController>().stateMachine.IsState(typeof(PlayerCaughtState)))
|
|
|
|
|
player.GetScript<PlayerController>().stateMachine.SetState(typeof(PlayerCaughtState));
|
|
|
|
|
|
2023-03-04 04:57:59 +08:00
|
|
|
|
//Teleport AI back to home waypoint
|
|
|
|
|
int homeIndex = 0;
|
|
|
|
|
GameObject? startWaypoint = null;
|
|
|
|
|
List<GameObject>? waypoints = (List<GameObject>)GetNodeData("waypoints");
|
|
|
|
|
if (GetNodeData("startWaypoint") != null)
|
|
|
|
|
startWaypoint = (GameObject)GetNodeData("startWaypoint");
|
|
|
|
|
if (startWaypoint != null)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < waypoints.Count; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (startWaypoint == waypoints[i])
|
|
|
|
|
{
|
|
|
|
|
homeIndex = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
SetNodeData("currentWaypointIndex", homeIndex);
|
|
|
|
|
SetNodeData("playerLastSightedWaypointIndex", homeIndex);
|
|
|
|
|
SetNodeData("isAlert", false);
|
|
|
|
|
Audio.SetParameterWithLabel("PlayerDetection", "Undetected");
|
|
|
|
|
AudioHandler.audioClipHandlers["SFXHumming"].Play();
|
|
|
|
|
|
|
|
|
|
Transform? transform = (Transform)GetNodeData("transform");
|
|
|
|
|
if (waypoints != null && transform != null)
|
|
|
|
|
{
|
|
|
|
|
transform.GlobalPosition = waypoints[homeIndex].GetComponent<Transform>().GlobalPosition;
|
|
|
|
|
}
|
|
|
|
|
ClearNodeData("target");
|
|
|
|
|
|
2022-11-23 21:02:33 +08:00
|
|
|
|
status = BehaviourTreeNodeStatus.SUCCESS;
|
|
|
|
|
onExit(BehaviourTreeNodeStatus.SUCCESS);
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Return running if not success
|
2022-11-25 20:16:31 +08:00
|
|
|
|
//Debug.Log("Running: About to capture in " + captureTimeLeft);
|
2022-11-23 21:02:33 +08:00
|
|
|
|
status = BehaviourTreeNodeStatus.RUNNING;
|
|
|
|
|
onExit(BehaviourTreeNodeStatus.RUNNING);
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
}
|