game(scripts): [big] standardise

just format and add placeholder xml comments where not placed
This commit is contained in:
Mark Joshwel 2024-08-10 02:25:46 +08:00
parent 6f91c582c3
commit 51148ef5e2
9 changed files with 323 additions and 64 deletions

View file

@ -1,7 +1,7 @@
/* /*
* author: lin hengrui ryan * author: ryan lin
* date: 30/7/2024 * date: 30/7/2024
* description: AI code for the different AI in the game * description: 'standard' npc ai behaviour
*/ */
using System.Collections; using System.Collections;
@ -9,6 +9,9 @@
using UnityEngine.AI; using UnityEngine.AI;
using Random = System.Random; using Random = System.Random;
/// <summary>
/// TODO
/// </summary>
public class AI : MonoBehaviour public class AI : MonoBehaviour
{ {
/// <summary> /// <summary>
@ -16,7 +19,6 @@ public class AI : MonoBehaviour
/// </summary> /// </summary>
private static readonly int AnimatorSpeed = Animator.StringToHash("Speed"); private static readonly int AnimatorSpeed = Animator.StringToHash("Speed");
/// <summary> /// <summary>
/// set the layers the AI can be on /// set the layers the AI can be on
/// </summary> /// </summary>

View file

@ -1,24 +1,96 @@
using System.Collections; /*
* author: ryan lin
* date: 30/7/2024
* description: vehicular ai behaviour
*/
using System.Collections;
using UnityEngine; using UnityEngine;
/// <summary>
/// TODO
/// </summary>
public class AICar : MonoBehaviour public class AICar : MonoBehaviour
{ {
/// <summary>
/// TODO
/// </summary>
[SerializeField] private Transform carPosition; [SerializeField] private Transform carPosition;
/// <summary>
/// TODO
/// </summary>
[SerializeField] private float stoppingDistance; [SerializeField] private float stoppingDistance;
/// <summary>
/// TODO
/// </summary>
[SerializeField] private float slowingSpeed; [SerializeField] private float slowingSpeed;
/// <summary>
/// TODO
/// </summary>
[SerializeField] private float slowingDistance; [SerializeField] private float slowingDistance;
/// <summary>
/// TODO
/// </summary>
[SerializeField] private float reverseDist; [SerializeField] private float reverseDist;
private string _currentState;
private string _nextState; /// <summary>
/// TODO
/// </summary>
private float _accelerationInput; private float _accelerationInput;
private CarController _car;
/// <summary>
/// TODO
/// </summary>
private float _angularDirection; private float _angularDirection;
/// <summary>
/// TODO
/// </summary>
private CarController _car;
/// <summary>
/// TODO
/// </summary>
private string _currentState;
/// <summary>
/// TODO
/// </summary>
private Vector3 _dirToMove; private Vector3 _dirToMove;
/// <summary>
/// TODO
/// </summary>
private float _distanceToTarget; private float _distanceToTarget;
/// <summary>
/// TODO
/// </summary>
private Transform _driveTarget; private Transform _driveTarget;
/// <summary>
/// TODO
/// </summary>
// FIXME: use an enum or something
private string _nextState;
/// <summary>
/// TODO
/// </summary>
private float _turnInput; private float _turnInput;
/// <summary>
/// TODO
/// </summary>
private float _verticalDirection; private float _verticalDirection;
/// <summary>
/// TODO
/// </summary>
private void Awake() private void Awake()
{ {
var hasChild = false; var hasChild = false;
@ -44,16 +116,25 @@ private void Awake()
ChangeState(); ChangeState();
} }
/// <summary>
/// TODO
/// </summary>
private void Update() private void Update()
{ {
_currentState = _nextState; _currentState = _nextState;
} }
/// <summary>
/// TODO
/// </summary>
private void ChangeState() private void ChangeState()
{ {
StartCoroutine(_currentState); StartCoroutine(_currentState);
} }
/// <summary>
/// TODO
/// </summary>
private void Steering() private void Steering()
{ {
_angularDirection = Vector3.SignedAngle(carPosition.transform.forward, _angularDirection = Vector3.SignedAngle(carPosition.transform.forward,
@ -64,34 +145,36 @@ private void Steering()
_turnInput = 1; _turnInput = 1;
} }
/// <summary>
/// TODO
/// </summary>
private IEnumerator Stopped() private IEnumerator Stopped()
{ {
while (_currentState == "Stopped") while (_currentState == "Stopped")
{ {
_distanceToTarget=Vector3.Distance(carPosition.position, _driveTarget.position); _distanceToTarget = Vector3.Distance(carPosition.position, _driveTarget.position);
_car.braking = true; _car.braking = true;
_accelerationInput = 0f; _accelerationInput = 0f;
_turnInput = 0f; _turnInput = 0f;
if (_distanceToTarget>stoppingDistance) if (_distanceToTarget > stoppingDistance) _nextState = "Slowed";
{
_nextState = "Slowed";
}
yield return new WaitForSeconds(1); yield return new WaitForSeconds(1);
} }
ChangeState(); ChangeState();
} }
/// <summary>
/// TODO
/// </summary>
private IEnumerator Slowed() private IEnumerator Slowed()
{ {
_car.braking = false; _car.braking = false;
while (_currentState == "Slowed") while (_currentState == "Slowed")
{ {
_verticalDirection = Vector3.Dot(carPosition.transform.forward, _verticalDirection = Vector3.Dot(carPosition.transform.forward,
(_driveTarget.position - carPosition.transform.position).normalized); (_driveTarget.position - carPosition.transform.position).normalized);
_distanceToTarget = Vector3.Distance(carPosition.position, _driveTarget.position); _distanceToTarget = Vector3.Distance(carPosition.position, _driveTarget.position);
if (_verticalDirection>0) if (_verticalDirection > 0)
{ {
if (carPosition.GetComponent<Rigidbody>().velocity.magnitude > slowingSpeed) if (carPosition.GetComponent<Rigidbody>().velocity.magnitude > slowingSpeed)
_accelerationInput = -1; _accelerationInput = -1;
@ -105,15 +188,19 @@ private IEnumerator Slowed()
else else
_accelerationInput = 1; _accelerationInput = 1;
} }
Steering(); Steering();
SlowedCheck(); SlowedCheck();
_car.SetInputs(_accelerationInput, _turnInput); _car.SetInputs(_accelerationInput, _turnInput);
yield return new WaitForEndOfFrame(); yield return new WaitForEndOfFrame();
} }
ChangeState(); ChangeState();
} }
/// <summary>
/// TODO
/// </summary>
private IEnumerator Driving() private IEnumerator Driving()
{ {
_car.braking = false; _car.braking = false;
@ -122,30 +209,25 @@ private IEnumerator Driving()
_verticalDirection = Vector3.Dot(carPosition.transform.forward, _verticalDirection = Vector3.Dot(carPosition.transform.forward,
(_driveTarget.position - carPosition.transform.position).normalized); (_driveTarget.position - carPosition.transform.position).normalized);
_distanceToTarget = Vector3.Distance(carPosition.position, _driveTarget.position); _distanceToTarget = Vector3.Distance(carPosition.position, _driveTarget.position);
_accelerationInput = 1; _accelerationInput = 1;
Steering(); Steering();
if (_distanceToTarget < slowingDistance) _nextState = "Slowed"; if (_distanceToTarget < slowingDistance) _nextState = "Slowed";
_car.SetInputs(_accelerationInput, _turnInput); _car.SetInputs(_accelerationInput, _turnInput);
yield return new WaitForEndOfFrame(); yield return new WaitForEndOfFrame();
} }
ChangeState(); ChangeState();
} }
/// <summary>
/// TODO
/// </summary>
private void SlowedCheck() private void SlowedCheck()
{ {
if (_distanceToTarget < stoppingDistance) if (_distanceToTarget < stoppingDistance) _nextState = "Stopped";
{
_nextState = "Stopped";
}
if (_distanceToTarget > slowingDistance) _nextState = "Driving"; if (_distanceToTarget > slowingDistance) _nextState = "Driving";
} }
} }

View file

@ -1,32 +1,71 @@
/*
* author: ryan lin
* date: TODO
* description: TODO
*/
using System.Collections; using System.Collections;
using UnityEngine; using UnityEngine;
public class AiManager : MonoBehaviour public class AIManager : MonoBehaviour
{ {
/// <summary>
/// TODO
/// </summary>
public Transform player; public Transform player;
/// <summary>
/// TODO
/// </summary>
public float cullingDistance; public float cullingDistance;
/// <summary>
/// TODO
/// </summary>
public GameObject aiPrefab; public GameObject aiPrefab;
private GameObject[] _ais;
private float _distance; /// <summary>
/// TODO
/// </summary>
public int maxAI; public int maxAI;
/// <summary>
/// TODO
/// </summary>
private GameObject[] _ais;
/// <summary>
/// TODO
/// </summary>
private float _distance;
/// <summary>
/// TODO
/// </summary>
private void Start() private void Start()
{ {
StartCoroutine(Manager()); StartCoroutine(Manager());
} }
/// <summary>
/// TODO
/// </summary>
private void OnDrawGizmos() private void OnDrawGizmos()
{ {
Gizmos.color = Color.yellow; Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(player.position, cullingDistance); Gizmos.DrawWireSphere(player.position, cullingDistance);
} }
// ReSharper disable Unity.PerformanceAnalysis /// <summary>
/// TODO
/// </summary>
private IEnumerator Manager() private IEnumerator Manager()
{ {
while (true) while (true)
{ {
// FIXME: feels weird
_ais = GameObject.FindGameObjectsWithTag("AIs"); _ais = GameObject.FindGameObjectsWithTag("AIs");
if (_ais.Length < maxAI ) if (_ais.Length < maxAI)
{ {
var instance = Instantiate(aiPrefab, gameObject.transform); var instance = Instantiate(aiPrefab, gameObject.transform);
} }
@ -38,6 +77,7 @@ private IEnumerator Manager()
} }
yield return new WaitForSeconds(1); yield return new WaitForSeconds(1);
} }
// TODO: iterator never returns? is this intended?
} }
} }

View file

@ -1,25 +1,65 @@
/*
* author: ryan lin
* date: TODO
* description: TODO
*/
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
/// <summary>
/// TODO
/// </summary>
public class CarController : MonoBehaviour public class CarController : MonoBehaviour
{ {
/// <summary>
/// TODO
/// </summary>
public enum Axel public enum Axel
{ {
Front, Front,
Rear Rear
} }
/// <summary>
/// TODO
/// </summary>
public float acceleration; public float acceleration;
/// <summary>
/// TODO
/// </summary>
public float turnAngle; public float turnAngle;
/// <summary>
/// TODO
/// </summary>
public List<Wheel> wheels; public List<Wheel> wheels;
/// <summary>
/// TODO
/// </summary>
public float brakeForce; public float brakeForce;
/// <summary>
/// TODO
/// </summary>
public bool braking; public bool braking;
/// <summary>
/// TODO
/// </summary>
private float _currentAcceleration; private float _currentAcceleration;
/// <summary>
/// TODO
/// </summary>
private float _currentTurn; private float _currentTurn;
/// <summary>
/// TODO
/// </summary>
private void FixedUpdate() private void FixedUpdate()
{ {
Move(); Move();
@ -27,18 +67,26 @@ private void FixedUpdate()
Brake(); Brake();
} }
/// <summary>
/// TODO
/// </summary>
public void SetInputs(float forwardAmount, float turnAmount) public void SetInputs(float forwardAmount, float turnAmount)
{ {
_currentAcceleration = forwardAmount * acceleration; _currentAcceleration = forwardAmount * acceleration;
_currentTurn = turnAmount * turnAngle; _currentTurn = turnAmount * turnAngle;
} }
/// <summary>
/// TODO
/// </summary>
public void Move() public void Move()
{ {
foreach (var wheel in wheels) wheel.wheelCollider.motorTorque = _currentAcceleration; foreach (var wheel in wheels) wheel.wheelCollider.motorTorque = _currentAcceleration;
} }
/// <summary>
/// TODO
/// </summary>
public void Brake() public void Brake()
{ {
if (braking) if (braking)
@ -49,6 +97,9 @@ public void Brake()
wheel.wheelCollider.brakeTorque = 0f; wheel.wheelCollider.brakeTorque = 0f;
} }
/// <summary>
/// TODO
/// </summary>
public void Steering() public void Steering()
{ {
foreach (var wheel in wheels) foreach (var wheel in wheels)
@ -56,6 +107,10 @@ public void Steering()
wheel.wheelCollider.steerAngle = _currentTurn; wheel.wheelCollider.steerAngle = _currentTurn;
} }
/// <summary>
/// TODO
/// </summary>
// NOTE: once again, why is this at the bottom lol
[Serializable] [Serializable]
public struct Wheel public struct Wheel
{ {

View file

@ -0,0 +1,20 @@
/*
* authors: ryan lin, mark joshwel
* date: 10/8/2024
* description: common interactable behaviour class
*/
using UnityEngine;
/// <summary>
/// TODO
/// </summary>
public class CommonInteractable : MonoBehaviour
{
/// <summary>
/// TODO
/// </summary>
public virtual void Interact()
{
}
}

View file

@ -1,29 +1,64 @@
/*
* author: ryan lin
* date: 8/8/2024
* description: door interaction and animation
*/
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class Door : Interactable /// <summary>
/// TODO
/// </summary>
public class Door : CommonInteractable
{ {
/// <summary>
/// TODO
/// </summary>
public enum DoorType public enum DoorType
{ {
Sliding, Sliding,
Rotating Rotating
} }
/// <summary>
/// TODO
/// </summary>
public List<DoorInput> doors; public List<DoorInput> doors;
/// <summary>
/// TODO
/// </summary>
public float duration; public float duration;
/// <summary>
/// TODO
/// </summary>
public AnimationCurve curve; public AnimationCurve curve;
/// <summary>
/// TODO
/// </summary>
private bool _isOpen; private bool _isOpen;
/// <summary>
/// TODO
/// </summary>
private bool _opening; private bool _opening;
/// <summary>
/// TODO
/// </summary>
public override void Interact() public override void Interact()
{ {
if (!_opening) StartCoroutine(OpenDoor()); if (!_opening) StartCoroutine(OpenDoor());
} }
/// <summary>
/// TODO
/// </summary>
private IEnumerator OpenDoor() private IEnumerator OpenDoor()
{ {
_opening = true; _opening = true;
@ -62,19 +97,19 @@ private IEnumerator OpenDoor()
currentDuration = 0; currentDuration = 0;
_opening = false; _opening = false;
if (_isOpen) if (_isOpen)
{
_isOpen = false; _isOpen = false;
}
else else
{
_isOpen = true; _isOpen = true;
}
} }
yield return new WaitForEndOfFrame(); yield return new WaitForEndOfFrame();
} }
} }
/// <summary>
/// TODO
/// </summary>
// NOTE: why is this at the bottom lol
[Serializable] [Serializable]
public struct DoorInput public struct DoorInput
{ {

View file

@ -1,11 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Interactable : MonoBehaviour
{
public virtual void Interact()
{
}
}

View file

@ -1,17 +1,54 @@
/*
* author: ryan lin
* date: 8/8/2024
* description: TODO
*/
using UnityEngine; using UnityEngine;
/// <summary>
/// TODO
/// </summary>
public class Player : MonoBehaviour public class Player : MonoBehaviour
{ {
public RaycastHit Hit; /// <summary>
/// TODO
/// </summary>
[SerializeField] private Transform playerPosition; [SerializeField] private Transform playerPosition;
/// <summary>
/// TODO
/// </summary>
[SerializeField] private float seeDistance; [SerializeField] private float seeDistance;
private Interactable _currentInteractable;
private bool _active; /// <summary>
private bool _raycast; /// TODO
/// </summary>
public LayerMask raycastLayers; public LayerMask raycastLayers;
/// <summary>
/// TODO
/// </summary>
private bool _active;
/// <summary>
/// TODO
/// </summary>
private CommonInteractable _currentInteractable;
/// <summary>
/// TODO
/// </summary>
private bool _raycast;
/// <summary>
/// TODO
/// </summary>
public RaycastHit Hit;
/// <summary>
/// TODO
/// </summary>
private void Update() private void Update()
{ {
_raycast = Physics.Raycast( _raycast = Physics.Raycast(
@ -28,15 +65,14 @@ private void Update()
); );
} }
/// <summary>
/// TODO
/// </summary>
private void OnAction() private void OnAction()
{ {
Debug.Log("test"); Debug.Log("test");
if (_raycast) if (_raycast)
{
if (Hit.transform.CompareTag("Interactable")) if (Hit.transform.CompareTag("Interactable"))
{ Hit.transform.GetComponent<CommonInteractable>().Interact();
Hit.transform.GetComponent<Interactable>().Interact();
}
}
} }
} }