Skip to content

Commit

Permalink
some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AdronTech committed Jan 21, 2017
1 parent 9e4f135 commit 44ef051
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 38 deletions.
Binary file modified GGJ17/Assets/Prefab/King.prefab
Binary file not shown.
Binary file modified GGJ17/Assets/Prefab/Soldier.prefab
Binary file not shown.
Binary file modified GGJ17/Assets/Prefab/Target.prefab
Binary file not shown.
Binary file modified GGJ17/Assets/Scene/Marky.unity
Binary file not shown.
59 changes: 59 additions & 0 deletions GGJ17/Assets/Script/KingBehaviour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(MyPhysics))]
public class KingBehaviour : MonoBehaviour {

public GameObject soldierPre;
public int size;

private List<MyPhysics> army;

private MyPhysics my;

void Awake () {
my = GetComponent<MyPhysics>();
army = new List<MyPhysics>();

for (int i = 0; i < size; i++)
{
newSoldier();
}
}

private void newSoldier()
{
GameObject soldier = Instantiate(soldierPre);
soldier.transform.position = transform.position + Vector3.down * 0.5f;

MyPhysics solPhysics = soldier.GetComponent<MyPhysics>();
if (solPhysics != null)
army.Add(solPhysics);

SeekBehaviour seek = soldier.GetComponent<SeekBehaviour>();
seek.target = my;

SeperationBehaviour sb = soldier.GetComponent<SeperationBehaviour>();
if (sb != null)
sb.targets = army;

CohesionBehaviour cb = soldier.GetComponent<CohesionBehaviour>();
if (cb != null)
cb.targets = army;

// update other list
foreach(MyPhysics other in army)
{
SeperationBehaviour other_sb = other.GetComponent<SeperationBehaviour>();
if (other_sb != null)
other_sb.targets.Add(solPhysics);

CohesionBehaviour other_cb = other.GetComponent<CohesionBehaviour>();
if (other_cb != null)
other_cb.targets.Add(solPhysics);
}
}


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

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

19 changes: 6 additions & 13 deletions GGJ17/Assets/Script/Steering/CohesionBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public class CohesionBehaviour : Steering
{
private List<MyPhysics> targets;
public List<MyPhysics> targets;

public float maxAcc;
public float radius;
Expand All @@ -17,17 +17,6 @@ public CohesionBehaviour()

public override SteeringOutput getSteering()
{
targets.Clear();

Collider[] colls = Physics.OverlapSphere(my.pos, radius);

foreach (Collider col in colls)
{
CohesionBehaviour other = col.gameObject.GetComponent<CohesionBehaviour>();
if (other != null && other != this)
targets.Add(other.gameObject.GetComponent<MyPhysics>());
}

SteeringOutput steering = new SteeringOutput();

if (targets.Count == 0)
Expand All @@ -38,7 +27,11 @@ public override SteeringOutput getSteering()

foreach (MyPhysics target in targets)
{
center += target.pos;
if (target == my) continue;

Vector3 diff = my.pos - target.pos;
if(diff.magnitude < radius)
center += target.pos;
}

center /= targets.Count + 1;
Expand Down
14 changes: 7 additions & 7 deletions GGJ17/Assets/Script/Steering/MyPhysics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class MyPhysics : MonoBehaviour {

protected Rigidbody myRigit;

void Start()
void Awake()
{
myRigit = GetComponent<Rigidbody>();
}
Expand All @@ -27,7 +27,7 @@ void FixedUpdate () {
// collision
Vector3 dir = vel.normalized;
RaycastHit rh = new RaycastHit();
if (myRigit.SweepTest(dir, out rh, vel.magnitude*Time.deltaTime) && !rh.collider.isTrigger)
if (myRigit.SweepTest(dir, out rh, vel.magnitude * Time.deltaTime) && !rh.collider.isTrigger)
{
if (rh.collider.GetComponent<AbstractBuildingBlock>() != null)
{
Expand All @@ -40,22 +40,22 @@ void FixedUpdate () {
pos = transform.position;
pos += vel * Time.deltaTime;
vel += acc * Time.deltaTime;
acc *= 0;
acc = Vector3.zero;

//ang = transform.rotation.y;
ang += ang_vel * Time.deltaTime;
ang_vel += ang_acc * Time.deltaTime;
ang_acc *= 0;
ang_acc = 0;

// do not go too fast
Vector3.ClampMagnitude(vel, maxVel);
vel = Vector3.ClampMagnitude(vel, maxVel);
//vel.y = 0;
ang = ((((ang + 180) % 360) + 360) % 360) -180 ;
ang_vel = Mathf.Clamp(ang_vel, -maxAngVel, maxAngVel);

// drag
Vector3 d = -vel;
d *= drag;
applyForce(d);
applyForce(d * drag);

float ang_d = -ang_vel;
ang_d *= drag;
Expand Down
7 changes: 4 additions & 3 deletions GGJ17/Assets/Script/Steering/SeekBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class SeekBehaviour : Steering {
public MyPhysics target;

public float maxAcc;
public float maxSpeed;
public float radius;

public override SteeringOutput getSteering()
Expand All @@ -23,12 +24,12 @@ public override SteeringOutput getSteering()

if(d < radius)
{
des *= d * my.maxVel / radius * 0.5f;
des *= d * maxSpeed / radius;
}
else
{
des *= my.maxVel;
}
des *= maxSpeed;
}

steering.linear = des - my.vel;
steering.linear = Vector3.ClampMagnitude(steering.linear, maxAcc);
Expand Down
19 changes: 5 additions & 14 deletions GGJ17/Assets/Script/Steering/SeperationBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public class SeperationBehaviour : Steering
{
private List<MyPhysics> targets;
public List<MyPhysics> targets;

public float maxAcc;
public float radius;
Expand All @@ -17,25 +17,16 @@ public SeperationBehaviour()

public override SteeringOutput getSteering()
{
// Detect nearby creatures
targets.Clear();

Collider[] colls = Physics.OverlapSphere(my.pos, radius);

foreach (Collider col in colls)
{
SeperationBehaviour other = col.gameObject.GetComponent<SeperationBehaviour>();
if (other != null && other != this)
targets.Add(other.gameObject.GetComponent<MyPhysics>());
}

SteeringOutput steering = new SteeringOutput();

foreach (MyPhysics target in targets)
{
if (target == my) continue;

Vector3 diff = my.pos - target.pos;
float dist = diff.magnitude;
steering.linear += diff / (dist * dist);
if(dist < radius)
steering.linear += diff / (dist * dist);
}

if (targets.Count > 0)
Expand Down
2 changes: 1 addition & 1 deletion GGJ17/Assets/Script/Steering/Steering.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public abstract class Steering : MonoBehaviour {
public float weight;
protected MyPhysics my;

public void Start()
public void Awake()
{
my = GetComponent<MyPhysics>();
}
Expand Down

0 comments on commit 44ef051

Please sign in to comment.