Skip to content

Commit

Permalink
AI attack
Browse files Browse the repository at this point in the history
  • Loading branch information
AdronTech committed Jan 22, 2017
1 parent bc9a0f9 commit 71d046e
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 8 deletions.
Binary file added GGJ17/Assets/Images/EnemyFire.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 76 additions & 0 deletions GGJ17/Assets/Images/EnemyFire.png.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added GGJ17/Assets/Material/EnemyFire.mat
Binary file not shown.
8 changes: 8 additions & 0 deletions GGJ17/Assets/Material/EnemyFire.mat.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified GGJ17/Assets/Prefab/Soldier.prefab
Binary file not shown.
Binary file modified GGJ17/Assets/Scene/Marky.unity
Binary file not shown.
88 changes: 88 additions & 0 deletions GGJ17/Assets/Script/AttackBehaviour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(LineRenderer))]
public class AttackBehaviour : MonoBehaviour {

public Transform target;
LineRenderer lr;

bool canSee;

public float delay;
public float damage;

void Start () {
lr = GetComponent<LineRenderer>();
lr.enabled = false;

StartCoroutine(inRange());
StartCoroutine(attack());
}

public void startAttack(Transform target)
{
this.target = target;
}

void Update()
{
lr.enabled = target != null;

if (lr.enabled)
{
Vector3[] positions = new Vector3[2];

positions[0] = transform.position;
positions[1] = target.position;

lr.SetPositions(positions);
}
}

public void stopAttack()
{
target = null;
}

IEnumerator inRange()
{
while(true)
{
if (target)
{
Vector3 dist = target.position - transform.position;

canSee = !Physics.Raycast(transform.position, dist.normalized, dist.magnitude - 1);
lr.enabled = canSee;
}
else
{
canSee = false;
}

yield return new WaitForSeconds(0.25f);
}
}

IEnumerator attack()
{
while(true)
{
if(target && canSee)
{
AbstractBaseBuilding t = target.GetComponent<AbstractBaseBuilding>();
if(t)
{
t.HP -= damage;
}

yield return new WaitForSeconds(delay);
}

yield return null;
}

}
}
12 changes: 12 additions & 0 deletions GGJ17/Assets/Script/AttackBehaviour.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions GGJ17/Assets/Script/Blocks/AbstractBaseBuilding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ public abstract class AbstractBaseBuilding : AbstractBuildingBlock {
private float hp = 10;
public float HP
{
get { return hp; }
set
{
hp = value;
if (hp < value)
if (hp < 0)
{
DestroyBuilding();
}
}
}

}

public void DestroyBuilding()
{

Expand Down
27 changes: 22 additions & 5 deletions GGJ17/Assets/Script/KingBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ IEnumerator scanForTarget()

if (!Physics.Raycast(my.pos, dist.normalized, dist.magnitude - 1))
{
//Debug.DrawLine(my.pos, a.transform.position, Color.red);
Debug.DrawLine(my.pos, a.transform.position, Color.red);
possibleTargets.Add(a.transform);
}
else
{
//Debug.DrawLine(my.pos, a.transform.position, Color.blue);
Debug.DrawLine(my.pos, a.transform.position, Color.blue);
if (possibleTargets.Contains(a.transform)) possibleTargets.Remove(a.transform);
}
}
Expand All @@ -112,9 +112,7 @@ IEnumerator scanForTarget()
bestTarget = possible;
}

Debug.Log(mySeek.target = bestTarget);

if (!mySeek.target ||
if (!mySeek.target || !possibleTargets.Contains(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;
Expand Down Expand Up @@ -146,10 +144,29 @@ IEnumerator stateMachineManager()
{
state = KingState.Attack;
mySeek.stop();

foreach(MyPhysics soldier in army)
{
AttackBehaviour ah = soldier.GetComponent<AttackBehaviour>();
if (ah) ah.startAttack(mySeek.target);
}

}

break;
case KingState.Attack:
if (!mySeek.target)
{
state = KingState.Seeking;
mySeek.start();

foreach (MyPhysics soldier in army)
{
AttackBehaviour ah = soldier.GetComponent<AttackBehaviour>();
if (ah) ah.startAttack(mySeek.target);
}
}

break;
}

Expand Down

0 comments on commit 71d046e

Please sign in to comment.