Skip to content

Commit

Permalink
AI: stop in front of building
Browse files Browse the repository at this point in the history
  • Loading branch information
AdronTech committed Jan 22, 2017
1 parent cd55775 commit a796b9c
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 17 deletions.
Binary file modified GGJ17/Assets/Scene/Marky.unity
Binary file not shown.
123 changes: 109 additions & 14 deletions GGJ17/Assets/Script/KingBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@ public class KingBehaviour : MonoBehaviour {
private MyPhysics my;
private SeekBehaviour mySeek;

public float attackRange;

public KingState state;

void Awake () {
my = GetComponent<MyPhysics>();
mySeek = GetComponent<SeekBehaviour>();

army = new List<MyPhysics>();
possibleTargets = new List<Transform>();

state = KingState.Seeking;

StartCoroutine(generateArmy());
StartCoroutine(stateMachineManager());
StartCoroutine(scanForTarget());
StartCoroutine(attackTarget());
}

private void newSoldier()
Expand All @@ -48,6 +56,12 @@ private void newSoldier()

}

public void Update()
{
if (mySeek.target)
Debug.DrawLine(my.pos, mySeek.target.position);
}

IEnumerator generateArmy()
{
for (int i = 0; i < size; i++)
Expand All @@ -60,33 +74,114 @@ IEnumerator generateArmy()

IEnumerator scanForTarget()
{
while(true)
while (true)
{
foreach(AbstractBaseBuilding a in FindObjectsOfType<AbstractBaseBuilding>())
{
Vector3 dist = a.transform.position - my.pos;
yield return new WaitUntil(() => state == KingState.Seeking);

if (!Physics.Raycast(my.pos, dist.normalized, dist.magnitude - 1))
foreach (AbstractBaseBuilding a in FindObjectsOfType<AbstractBaseBuilding>())
{
if (a.neighbors[AbstractBuildingBlock.down] && a.neighbors[AbstractBuildingBlock.down].tag == "Ground")
{
Debug.DrawLine(my.pos, a.transform.position, Color.red);
possibleTargets.Add(a.transform);
Vector3 dist = a.transform.position - my.pos;

if (!Physics.Raycast(my.pos, dist.normalized, dist.magnitude - 1))
{
//Debug.DrawLine(my.pos, a.transform.position, Color.red);
possibleTargets.Add(a.transform);
}
else
{
//Debug.DrawLine(my.pos, a.transform.position, Color.blue);
if (possibleTargets.Contains(a.transform)) possibleTargets.Remove(a.transform);
}
}
else
}

Transform bestTarget = null;

foreach (Transform possible in possibleTargets)
{
if (!bestTarget)
{
Debug.DrawLine(my.pos, a.transform.position, Color.blue);
if (possibleTargets.Contains(a.transform)) possibleTargets.Remove(a.transform);
bestTarget = possible;
continue;
}

if (getPriority(possible) > getPriority(bestTarget) ||
(getPriority(possible) == getPriority(bestTarget) && Vector3.Distance(my.pos, possible.position) <= Vector3.Distance(my.pos, bestTarget.position)))
bestTarget = possible;
}

if((mySeek.target != null && !possibleTargets.Contains(mySeek.target)) || mySeek.target == null)
Debug.Log(mySeek.target = bestTarget);

if (!mySeek.target ||
getPriority(bestTarget) > getPriority(mySeek.target) ||
(getPriority(bestTarget) == getPriority(mySeek.target) && Vector3.Distance(my.pos, bestTarget.position) <= Vector3.Distance(my.pos, mySeek.target.position)))
mySeek.target = bestTarget;

yield return new WaitForSeconds(0.5f);
}
}

IEnumerator attackTarget()
{
while (true)
{
yield return new WaitUntil(() => state == KingState.Attack);

Debug.Log("Attack " + mySeek.target);
}
}

IEnumerator stateMachineManager()
{
while(true)
{
switch(state)
{
if(possibleTargets.Count != 0)
mySeek.target = possibleTargets[0];
case KingState.Seeking:
if (!mySeek.target) break;

if (Mathf.Abs(my.pos.x - mySeek.target.position.x) <= attackRange || Mathf.Abs(my.pos.z - mySeek.target.position.z) <= attackRange)
{
state = KingState.Attack;
mySeek.stop();
}

break;
case KingState.Attack:
break;
}

yield return new WaitForSeconds(1f);
yield return null;
}
}

public int getPriority(Transform block)
{
if (!block) return 0;

AbstractBaseBuilding abb = block.GetComponent<AbstractBaseBuilding>();

if (abb)
{
if (abb is Building_Flag)
return 4;
if (abb is Building_Barracks)
return 3;
if (abb is Building_Extractor)
return 2;
if (abb is Building_Wall)
return 1;
}

return 0;
}

public enum KingState
{
Seeking,
Attack,
}

}
15 changes: 12 additions & 3 deletions GGJ17/Assets/Script/Steering/MyPhysics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ void FixedUpdate () {
// collect steering behaviours
foreach (Steering steering in GetComponents<Steering>())
{
SteeringOutput so = steering.getSteering();
applyForce(so.linear * steering.weight);
ang_acc += so.angular * steering.weight;
if (steering.Enabled)
{
SteeringOutput so = steering.getSteering();
applyForce(so.linear * steering.weight);
ang_acc += so.angular * steering.weight;
}
}

// move
Expand All @@ -79,4 +82,10 @@ public void applyForce(Vector3 f)
acc += f;
}

public void stop()
{
vel = Vector3.zero;
acc = Vector3.zero;
}

}
11 changes: 11 additions & 0 deletions GGJ17/Assets/Script/Steering/SeekBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,15 @@ public override SteeringOutput getSteering()
return steering;
}

public void start()
{
Enabled = true;
}

public void stop()
{
my.stop();
Enabled = false;
}

}
3 changes: 3 additions & 0 deletions GGJ17/Assets/Script/Steering/Steering.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ public abstract class Steering : MonoBehaviour {
public float weight;
protected MyPhysics my;

public bool Enabled;

public void Awake()
{
my = GetComponent<MyPhysics>();
Enabled = true;
}

public abstract SteeringOutput getSteering();
Expand Down

0 comments on commit a796b9c

Please sign in to comment.