public override NPCState Update()
    {
		// Check if guards are in sight and if a transition to flee states is necessary
		// These checks are in the base state
		NPCState stateFromBase = base.Update();
		if (stateFromBase != this)
		{
			return stateFromBase;
		}

		soulSearchingTimer -= Time.deltaTime;
        movementDriver = this.stateMachine.NPC.MovementDriver;

		visibleSouls = CollectorStateHelper.FindVisibleSouls(this.stateMachine.NPC);
		if (visibleSouls.Count > 0) { 		// If there are visible souls
			GameObject closestSoul = NPCStateHelper.FindClosestGameObject(this.stateMachine.NPC.gameObject, visibleSouls);

			if (!NPCStateHelper.IsWithinCollisionRangeAtGroundLevel(this.stateMachine.NPC.Instance, closestSoul, CollectorStateMachine.SOUL_COLLISION_RANGE))
			{
				NPCStateHelper.MoveTo(this.stateMachine.NPC, closestSoul, 1f);
			}			
		}
		else {	// There are no visible souls
			if (soulSearchingTimer > 0) // If the time period for soul searching is not over
			{
				// If we're at the end of our path having found no souls, find a new random one
				CollectorStateHelper.GetNewPath(movementDriver, GameManager.AllNodes[UnityEngine.Random.Range(0, GameManager.AllNodes.Count - 1)]); 
			}
			else {	// If the time period for soul searching is over
				return new CollectorFindSingleTreeState(this.stateMachine);
			}
		}

        return this;
    }
	public override NPCState Update()
	{
        NPCState stateFromBase = base.Update();
        if (stateFromBase != this)
        {
            return stateFromBase;
        }

		movementDriver = this.stateMachine.NPC.MovementDriver;

		visibleSouls = CollectorStateHelper.FindVisibleSouls(this.stateMachine.NPC);
	
		if (visibleSouls.Count > 0) {
			GameObject closestSoul = NPCStateHelper.FindClosestGameObject(this.stateMachine.NPC.gameObject, visibleSouls);

			if (NPCStateHelper.IsWithinCollisionRangeAtGroundLevel(this.stateMachine.NPC.Instance, closestSoul, CollectorStateMachine.SOUL_COLLISION_RANGE))
			{
			    NPCActions.CmdConsumeSoul((CollectorDriver)this.stateMachine.NPC, closestSoul);
			}
			else 
			{
				NPCStateHelper.MoveTo(this.stateMachine.NPC, closestSoul, 1f);	
			}

		}
		else {	// All visible souls have been collected, return to previous state
			return this.GetPreviousStateInStack(); 
		}
		
		return this;
	}
    public override void Entry()
    {
		Debug.Log (this.stateMachine.NPC.name + ": Search Soul State Entry");
        movementDriver = this.stateMachine.NPC.MovementDriver;
        Node newEndNode = GameManager.AllNodes[UnityEngine.Random.Range(0, GameManager.AllNodes.Count - 1)];
        movementDriver.ChangePath(newEndNode);
    }
	public static void GetNewPath(NPCMovementDriver movementDriver, Node endNode) 
	{
		if (movementDriver.AttainedFinalNode)
		{
			movementDriver.ChangePath(endNode);
		}
	}
	public override void Entry()
	{
		Debug.Log (this.stateMachine.NPC.name + ": Call For Help State Entry");
		movementDriver = this.stateMachine.NPC.MovementDriver;
		GetButtonTriggeringInfoFromTargetTree();

		// Add more wait delay to a triple tree
		if (targetTree.TreeButtons.Count == 3) {
			waitForHelpTimer += 2f;
		}
	}
	public override NPCState Update()
	{
		// Check if guards are in sight and if a transition to flee states is necessary
		// These checks are in the base state
		NPCState stateFromBase = base.Update();
		if (stateFromBase != this)
		{
			return stateFromBase;
		}

		// In the case that this help call was initiated by a player, check if the player is still triggering the tree
		// If not, cancel the help calls
		if (!(this.stateMachine as CollectorStateMachine).CheckIfPlayerIsTriggeringTheTree(this.targetTree)) 
		{
			(this.stateMachine as CollectorStateMachine).CancelHelpCallsAfterPlayerCall();
		}

		if ((this.stateMachine as CollectorStateMachine).hasReceivedPlayerHelpCall && !hasNotifiedPlayerOfHelpAnswer)
		{
			// Notify the player that this collector is coming to help
			(this.stateMachine as CollectorStateMachine).NotifyPlayerOfHelp(this.stateMachine.NPC as CollectorDriver);
			hasNotifiedPlayerOfHelpAnswer = true;
		}

		movementDriver = this.stateMachine.NPC.MovementDriver;
		
		// Check if help call has been cancelled
		// If so, returned to soul search state
		if (!(this.stateMachine as CollectorStateMachine).HasReceivedHelpCall && !(this.stateMachine as CollectorStateMachine).hasReceivedPlayerHelpCall) {
			return this.GetPreviousStateInStack(); 
		}

		if (buttonTarget) {	
			// This collision range is set to handle the collision of the NPC and the tree button for the purpsoe of notifiying help calls
			if (NPCStateHelper.IsWithinCollisionRangeAtGroundLevel(stateMachine.NPC.Instance, buttonTarget, CollectorStateMachine.TREE_COLLISION_RANGE)) {
				if (!hasNotifiedCallerOfArrival) {
					this.callerStateMachine.NotifyCallerOfHelpArrival(this.stateMachine.NPC as CollectorDriver); 	// Inform the caller of your arrival
					hasNotifiedCallerOfArrival = true;
				}
			}
			// This collision range is set to move the NPC directly onto the tree button
			if (NPCStateHelper.IsWithinCollisionRangeAtGroundLevel(stateMachine.NPC.Instance, buttonTarget, CollectorStateMachine.TREE_MOVEMENT_COLLISION_RANGE)) {
				return this;
			}
			else {
				NPCStateHelper.MoveTo(this.stateMachine.NPC, buttonTarget, 5f);
			}
		}
		
		return this;
	}
	public override NPCState Update()
	{
		// Check if guards are in sight and if a transition to flee states is necessary
		// These checks are in the base state
		NPCState stateFromBase = base.Update();
		if (stateFromBase != this)
		{
			// Cancell help calls before transitioning out of this state
			if (!this.hasCancelledHelpCalls) 
				CancelHelpCalls();

			return stateFromBase;
		}

		// Timers
		waitForHelpTimer -= Time.deltaTime;
		if (everyoneIsOnTheTree)
			delayBeforeCancellingHelpCallsTimer -= Time.deltaTime;

		movementDriver = this.stateMachine.NPC.MovementDriver;

		// Make help calls for each button that needs a collector
		if (!this.hasAskedForHelp) {
			MakeHelpCalls();
			Debug.Log (this.stateMachine.NPC.name + " Caller: I've asked for help! collectorsAskedForHelp count: " + collectorsAskedForHelp.Count );
		}

		// If help callees have arrived, cancel the help calls
		if ((this.stateMachine as CollectorStateMachine).CheckIfHelpCalleesHaveArrived() && !this.hasCancelledHelpCalls) {
			everyoneIsOnTheTree = true;

			// Since it is likely souls will now be released from the tree and caller is transitioned into collect soul state
			// We reset the stack history so that after the collect soul state, the caller returns to search for souls and not call for help again
			this.ResetStackToDefaultState(new CollectorSearchSoulsState(this.stateMachine));
		}

		if (delayBeforeCancellingHelpCallsTimer <= 0 && !this.hasCancelledHelpCalls)	{// If set delay for remaining on the tree passes, cancel everyone's help calls
			Debug.Log (this.stateMachine.NPC.name + " Caller: Tree's been hit, I'm cancelling all help calls and cleaning up");
			CancelHelpCalls();
			(this.stateMachine as CollectorStateMachine).CleanUpCallerAfterCall();
			everyoneIsOnTheTree = false;
		}

		// Return to search soul state if help took too long to show up
		if (waitForHelpTimer <= 0 ) {
			CancelHelpCalls();
			return this.ResetStackToDefaultState(new CollectorSearchSoulsState(this.stateMachine));
		}

		return this;
	}
Beispiel #8
0
    public virtual void Setup(GameObject instance, GameObject cameraInstance, Transform spawnPoint, GameObject soulPrefab)
    {
        this.instance = instance;
        this.controlledByAI = true;
        this.spawnPoint = spawnPoint;
		this.soulPrefab = soulPrefab;

        this.visibleNPCs = new List<NPCDriver>();

        this.instance.AddComponent<NPCMovementDriver>();
        this.movementDriver = this.instance.GetComponent<NPCMovementDriver>();
        this.movementDriver.Setup(this.GetComponent<NPCMovement>());

		this.audioSource = this.instance.GetComponent<AudioSource>();
    }
	public override NPCState Update()
	{
		// Check if guards are in sight and if a transition to flee states is necessary
		// These checks are in the base state
		NPCState stateFromBase = base.Update();
		if (stateFromBase != this)
		{
			return stateFromBase;
		}

		singleTreeSearchingTimer -= Time.deltaTime;
		movementDriver = this.stateMachine.NPC.MovementDriver;

		buttonTargetForClosestSingleTree = CollectorStateHelper.FindClosestFullTreeButton(this.stateMachine.NPC, 1);

		// Add trees seen to strategic tree memory system
		this.stateMachine.AddVisibleTrees(NPCStateHelper.FindVisibleTrees(stateMachine.NPC));

		if (singleTreeSearchingTimer > 0) {
			if (buttonTargetForClosestSingleTree) 
			{
				if (NPCStateHelper.IsWithinCollisionRangeAtGroundLevel(stateMachine.NPC.Instance, buttonTargetForClosestSingleTree, CollectorStateMachine.TREE_MOVEMENT_COLLISION_RANGE)) {

					// Since it is likely souls will now be released from the tree and caller is transitioned into collect soul state
					// We reset the stack history so that after the collect soul state, the caller returns to search for souls and not search for single tree again
					this.ResetStackToDefaultState(new CollectorSearchSoulsState(this.stateMachine));

					return this;
				}
				else {
	            	NPCStateHelper.MoveTo(this.stateMachine.NPC, buttonTargetForClosestSingleTree, 2f);
				}
	           
			}
			else 
			{
				// If we're at the end of our path having found no trees, find a new path based on the trees we've remembered
				//CollectorStateHelper.GetNewPath(movementDriver, GameManager.AllNodes[UnityEngine.Random.Range(0, GameManager.AllNodes.Count - 1)]; 
				CollectorStateHelper.GetNewPath(movementDriver, CollectorStateHelper.FindNodeForRememberedTreePosition(this.stateMachine)); 
			}
		}
		else {
			return new CollectorFindMultipleTreeState(this.stateMachine);
		}

		return this;
	}
	public override void Entry()
	{
		Debug.Log (this.stateMachine.NPC.name + ": Find Single Tree State Entry");
		movementDriver = this.stateMachine.NPC.MovementDriver; 
	}
	public override void Entry()
	{	
		Debug.Log (this.stateMachine.NPC.name + ": Collect Soul State Entry");
		movementDriver = this.stateMachine.NPC.MovementDriver;
	}
	public override void Entry()
	{
		Debug.Log (this.stateMachine.NPC.name + ": Answer Help Call State Entry");
		movementDriver = this.stateMachine.NPC.MovementDriver;
		buttonTarget = GetUntriggeredButtonFromTree(targetTree);	// Get a button target for the targetTree
	}