diff --git a/GGJ17/Assets/Prefab/King.prefab b/GGJ17/Assets/Prefab/King.prefab index bfcd31e..799c6fe 100644 Binary files a/GGJ17/Assets/Prefab/King.prefab and b/GGJ17/Assets/Prefab/King.prefab differ diff --git a/GGJ17/Assets/Prefab/Soldier.prefab b/GGJ17/Assets/Prefab/Soldier.prefab index 43a8ed8..6e2c096 100644 Binary files a/GGJ17/Assets/Prefab/Soldier.prefab and b/GGJ17/Assets/Prefab/Soldier.prefab differ diff --git a/GGJ17/Assets/Prefab/Target.prefab b/GGJ17/Assets/Prefab/Target.prefab index da5d929..7a62c64 100644 Binary files a/GGJ17/Assets/Prefab/Target.prefab and b/GGJ17/Assets/Prefab/Target.prefab differ diff --git a/GGJ17/Assets/Scene/Marky.unity b/GGJ17/Assets/Scene/Marky.unity index 4a58ef3..f74cdab 100644 Binary files a/GGJ17/Assets/Scene/Marky.unity and b/GGJ17/Assets/Scene/Marky.unity differ diff --git a/GGJ17/Assets/Script/KingBehaviour.cs b/GGJ17/Assets/Script/KingBehaviour.cs new file mode 100644 index 0000000..e462766 --- /dev/null +++ b/GGJ17/Assets/Script/KingBehaviour.cs @@ -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 army; + + private MyPhysics my; + + void Awake () { + my = GetComponent(); + army = new List(); + + 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(); + if (solPhysics != null) + army.Add(solPhysics); + + SeekBehaviour seek = soldier.GetComponent(); + seek.target = my; + + SeperationBehaviour sb = soldier.GetComponent(); + if (sb != null) + sb.targets = army; + + CohesionBehaviour cb = soldier.GetComponent(); + if (cb != null) + cb.targets = army; + + // update other list + foreach(MyPhysics other in army) + { + SeperationBehaviour other_sb = other.GetComponent(); + if (other_sb != null) + other_sb.targets.Add(solPhysics); + + CohesionBehaviour other_cb = other.GetComponent(); + if (other_cb != null) + other_cb.targets.Add(solPhysics); + } + } + + +} diff --git a/GGJ17/Assets/Script/KingBehaviour.cs.meta b/GGJ17/Assets/Script/KingBehaviour.cs.meta new file mode 100644 index 0000000..137fcc8 --- /dev/null +++ b/GGJ17/Assets/Script/KingBehaviour.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 93b372856b6c7b14e898087396ba0607 +timeCreated: 1485027337 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/GGJ17/Assets/Script/Steering/CohesionBehaviour.cs b/GGJ17/Assets/Script/Steering/CohesionBehaviour.cs index 40eab9b..49748c4 100644 --- a/GGJ17/Assets/Script/Steering/CohesionBehaviour.cs +++ b/GGJ17/Assets/Script/Steering/CohesionBehaviour.cs @@ -5,7 +5,7 @@ public class CohesionBehaviour : Steering { - private List targets; + public List targets; public float maxAcc; public float radius; @@ -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(); - if (other != null && other != this) - targets.Add(other.gameObject.GetComponent()); - } - SteeringOutput steering = new SteeringOutput(); if (targets.Count == 0) @@ -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; diff --git a/GGJ17/Assets/Script/Steering/MyPhysics.cs b/GGJ17/Assets/Script/Steering/MyPhysics.cs index 875d607..f0806cd 100644 --- a/GGJ17/Assets/Script/Steering/MyPhysics.cs +++ b/GGJ17/Assets/Script/Steering/MyPhysics.cs @@ -17,7 +17,7 @@ public class MyPhysics : MonoBehaviour { protected Rigidbody myRigit; - void Start() + void Awake() { myRigit = GetComponent(); } @@ -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() != null) { @@ -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; diff --git a/GGJ17/Assets/Script/Steering/SeekBehaviour.cs b/GGJ17/Assets/Script/Steering/SeekBehaviour.cs index 363f046..8ceb876 100644 --- a/GGJ17/Assets/Script/Steering/SeekBehaviour.cs +++ b/GGJ17/Assets/Script/Steering/SeekBehaviour.cs @@ -8,6 +8,7 @@ public class SeekBehaviour : Steering { public MyPhysics target; public float maxAcc; + public float maxSpeed; public float radius; public override SteeringOutput getSteering() @@ -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); diff --git a/GGJ17/Assets/Script/Steering/SeperationBehaviour.cs b/GGJ17/Assets/Script/Steering/SeperationBehaviour.cs index 35621ea..d485f32 100644 --- a/GGJ17/Assets/Script/Steering/SeperationBehaviour.cs +++ b/GGJ17/Assets/Script/Steering/SeperationBehaviour.cs @@ -5,7 +5,7 @@ public class SeperationBehaviour : Steering { - private List targets; + public List targets; public float maxAcc; public float radius; @@ -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(); - if (other != null && other != this) - targets.Add(other.gameObject.GetComponent()); - } - 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) diff --git a/GGJ17/Assets/Script/Steering/Steering.cs b/GGJ17/Assets/Script/Steering/Steering.cs index 5070a29..3f4e784 100644 --- a/GGJ17/Assets/Script/Steering/Steering.cs +++ b/GGJ17/Assets/Script/Steering/Steering.cs @@ -7,7 +7,7 @@ public abstract class Steering : MonoBehaviour { public float weight; protected MyPhysics my; - public void Start() + public void Awake() { my = GetComponent(); }