Browse Source

Merge branch 'master' of https://bitbucket.org/JoshuaReason/ggj2020

# Conflicts:
#	Assets/Scripts/Input/PlayerController.cs
#	Assets/WorldAssets/Prefabs/Horse.prefab
master
NickFowler 4 years ago
parent
commit
2d4e78ce9f
2 changed files with 52 additions and 15 deletions
  1. +26
    -14
      Assets/Scripts/Input/HerdController.cs
  2. +26
    -1
      Assets/Scripts/Input/PlayerController.cs

+ 26
- 14
Assets/Scripts/Input/HerdController.cs View File

@ -19,19 +19,27 @@ public class HerdController : MonoBehaviour
[SerializeField] [SerializeField]
private float WaitTime; private float WaitTime;
[SerializeField]
private Transform Centre;
private float lastTime;
private List<PlayerController> Herd; private List<PlayerController> Herd;
private Vector2 recievedInput;
void Start() void Start()
{ {
SpawnHerd(); SpawnHerd();
StartCoroutine(MoveRoutine());
} }
//Recieved movement input from player //Recieved movement input from player
private void OnMovement(InputValue value) private void OnMovement(InputValue value)
{ {
Vector2 input = value.Get<Vector2>(); Vector2 input = value.Get<Vector2>();
if (input.magnitude > 0)
input = input.normalized;
Herd.ForEach(p => p.SetMovement(input)); Herd.ForEach(p => p.SetMovement(input));
recievedInput = input;
} }
@ -50,7 +58,7 @@ public class HerdController : MonoBehaviour
for (int i = 0; i < HerdCount; i++) for (int i = 0; i < HerdCount; i++)
{ {
int SpawnAttempt = 0; int SpawnAttempt = 0;
@ -62,16 +70,16 @@ public class HerdController : MonoBehaviour
if (SpawnPoint != null) if (SpawnPoint != null)
position += SpawnPoint.position; position += SpawnPoint.position;
if (SpawnPositionValid(position,rotation, bound))
if (SpawnPositionValid(position, rotation, bound))
{ {
GameObject newObject = Instantiate(Prefab, position, rotation, transform); GameObject newObject = Instantiate(Prefab, position, rotation, transform);
Herd.Add(newObject.GetComponent<PlayerController>()); Herd.Add(newObject.GetComponent<PlayerController>());
break; break;
} }
SpawnAttempt++; SpawnAttempt++;
if (SpawnAttempt % 10 == 0) if (SpawnAttempt % 10 == 0)
@ -84,28 +92,32 @@ public class HerdController : MonoBehaviour
Debug.Log("Total Spawned: " + Herd.Count);
} }
Debug.Log("Total Spawned: " + Herd.Count);
} }
public IEnumerator MoveRoutine()
public void FixedUpdate()
{ {
foreach (PlayerController pc in Herd)
if (recievedInput.magnitude > 0 && lastTime + WaitTime < Time.time)
{ {
pc.MoveObject(WaitTime);
Herd.ForEach(p => p.MoveObject(WaitTime));
lastTime = Time.time;
} }
yield return new WaitForSeconds(WaitTime);
StartCoroutine(MoveRoutine());
if (Centre != null)
Centre.position = Herd.Aggregate(new Vector3(0, 0, 0), (s, v) => s + v.transform.position) / (float)Herd.Count;
} }
private bool SpawnPositionValid(Vector3 position,Quaternion rotation ,Bounds bound)
private bool SpawnPositionValid(Vector3 position, Quaternion rotation, Bounds bound)
{ {
Collider[] colliders = Physics.OverlapBox(position, bound.extents,rotation);
Collider[] colliders = Physics.OverlapBox(position, bound.extents, rotation);
Debug.DrawLine(position, position + Vector3.up); Debug.DrawLine(position, position + Vector3.up);
foreach(Collider col in colliders)
foreach (Collider col in colliders)
{ {
if (col.GetComponentInChildren<PlayerController>()) if (col.GetComponentInChildren<PlayerController>())
return false; return false;

+ 26
- 1
Assets/Scripts/Input/PlayerController.cs View File

@ -11,16 +11,21 @@ public class PlayerController : MonoBehaviour
public GameObject model; public GameObject model;
public CharacterController cController; public CharacterController cController;
private float speedMulitplier;
public float speedMulitplier;
private float randomizer;
private float directionRandmoizer;
private Vector2 receivedInput; private Vector2 receivedInput;
private Vector3 moveDirection = Vector3.zero; private Vector3 moveDirection = Vector3.zero;
private float moveDelta;
private float lastMoveTime;
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
speedMulitplier = UnityEngine.Random.Range(0.8f, 1.2f); speedMulitplier = UnityEngine.Random.Range(0.8f, 1.2f);
cController = GetComponent<CharacterController>(); cController = GetComponent<CharacterController>();
randomizer = UnityEngine.Random.Range(0,10);
} }
public void SetMovement(Vector2 input) public void SetMovement(Vector2 input)
@ -30,9 +35,24 @@ public class PlayerController : MonoBehaviour
public void UpdatePosition() public void UpdatePosition()
{ {
moveDelta = Time.time - lastMoveTime;
lastMoveTime = Time.time;
if (receivedInput.magnitude == 0)
return;
float HorseX, HorseZ; float HorseX, HorseZ;
HorseZ = receivedInput.y; HorseZ = receivedInput.y;
HorseX = receivedInput.x; HorseX = receivedInput.x;
if (Mathf.Abs(HorseZ) == Mathf.Min(Mathf.Abs(HorseZ), Mathf.Abs(HorseX)))
{
HorseZ += directionRandmoizer;
}
else
{
HorseX += directionRandmoizer;
}
if (cController.isGrounded) if (cController.isGrounded)
{ {
@ -40,6 +60,8 @@ public class PlayerController : MonoBehaviour
moveDirection *= walkSpeed * speedMulitplier; moveDirection *= walkSpeed * speedMulitplier;
} }
moveDirection.y -= gravity * Time.deltaTime; moveDirection.y -= gravity * Time.deltaTime;
cController.Move(moveDirection * Time.deltaTime); cController.Move(moveDirection * Time.deltaTime);
@ -104,6 +126,9 @@ public class PlayerController : MonoBehaviour
// Update is called once per frame // Update is called once per frame
void Update() void Update()
{ {
speedMulitplier = (Mathf.Sin(Time.time * UnityEngine.Random.Range(0.9f,1.1f) + randomizer) + 2);
directionRandmoizer = Mathf.Cos(Time.time + randomizer / 2) * 0.3f;
UpdatePosition(); UpdatePosition();
} }
} }

Loading…
Cancel
Save