AI_Wanders.cs 1.52 KB
Newer Older
mthiele2's avatar
mthiele2 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
using UnityEngine;
using System.Collections;

public class AI_Wanders : WeightedAction
{

	AI_Core myAI;

	GameObject nearestPlayer;

	private float waitTime;

	public float startWaitTime;

	Vector3 patrolSpot;

	float minX, maxX, minY, maxY;

mthiele2's avatar
mthiele2 committed
19 20
	private float journeyLength;

mthiele2's avatar
mthiele2 committed
21 22
	public float patrolRange;

mthiele2's avatar
mthiele2 committed
23 24 25 26
	public float speed;

	private float startTime;

mthiele2's avatar
mthiele2 committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40

	// Use this for initialization
	void Start()
	{
		myAI = GetComponent<AI_Core>();

		waitTime = startWaitTime;

		minX = transform.position.x - patrolRange;
		maxX = transform.position.x + patrolRange;
		minY = transform.position.y - patrolRange;
		maxY = transform.position.y + patrolRange;

		patrolSpot = new Vector3(Random.Range(minX, maxX), Random.Range(minY, maxY), -1);
mthiele2's avatar
mthiele2 committed
41
		//startTime = Time.time;
mthiele2's avatar
mthiele2 committed
42 43 44 45 46 47 48 49 50 51
	}

	//AI wanders arround when its in detection range of the player
	void CheckConditions()
	{
		myAI.desiredAction.Add(this);
	}

	public override void performAction()
	{
mthiele2's avatar
mthiele2 committed
52
		transform.position = Vector2.MoveTowards(transform.position, patrolSpot, speed * Time.deltaTime);
mthiele2's avatar
mthiele2 committed
53 54 55 56 57

		if (Vector2.Distance(transform.position, patrolSpot) < 0.2f)
		{
			if (waitTime <= 0)
			{
mthiele2's avatar
mthiele2 committed
58 59
				//journeyLength = Vector3.Distance(transform.position, patrolSpot);
				//startTime = Time.time;
mthiele2's avatar
mthiele2 committed
60
				waitTime = startWaitTime;
mthiele2's avatar
mthiele2 committed
61 62

				patrolSpot = new Vector3(Random.Range(minX, maxX), Random.Range(minY, maxY), -1);
mthiele2's avatar
mthiele2 committed
63 64 65 66 67 68 69 70 71 72 73 74 75
			}
			else
			{
				waitTime -= Time.deltaTime;
			}
		}
	}

	private void OnCollisionStay2D(Collision2D collision)
	{
		patrolSpot = new Vector3(Random.Range(minX, maxX), Random.Range(minY, maxY), -1);
	}
}