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
* description: AI code for the different AI in the game
* description: 'standard' npc ai behaviour
*/
using System.Collections;
@ -9,6 +9,9 @@
using UnityEngine.AI;
using Random = System.Random;
/// <summary>
/// TODO
/// </summary>
public class AI : MonoBehaviour
{
/// <summary>
@ -16,7 +19,6 @@ public class AI : MonoBehaviour
/// </summary>
private static readonly int AnimatorSpeed = Animator.StringToHash("Speed");
/// <summary>
/// set the layers the AI can be on
/// </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;
/// <summary>
/// TODO
/// </summary>
public class AICar : MonoBehaviour
{
/// <summary>
/// TODO
/// </summary>
[SerializeField] private Transform carPosition;
/// <summary>
/// TODO
/// </summary>
[SerializeField] private float stoppingDistance;
/// <summary>
/// TODO
/// </summary>
[SerializeField] private float slowingSpeed;
/// <summary>
/// TODO
/// </summary>
[SerializeField] private float slowingDistance;
/// <summary>
/// TODO
/// </summary>
[SerializeField] private float reverseDist;
private string _currentState;
private string _nextState;
/// <summary>
/// TODO
/// </summary>
private float _accelerationInput;
private CarController _car;
/// <summary>
/// TODO
/// </summary>
private float _angularDirection;
/// <summary>
/// TODO
/// </summary>
private CarController _car;
/// <summary>
/// TODO
/// </summary>
private string _currentState;
/// <summary>
/// TODO
/// </summary>
private Vector3 _dirToMove;
/// <summary>
/// TODO
/// </summary>
private float _distanceToTarget;
/// <summary>
/// TODO
/// </summary>
private Transform _driveTarget;
/// <summary>
/// TODO
/// </summary>
// FIXME: use an enum or something
private string _nextState;
/// <summary>
/// TODO
/// </summary>
private float _turnInput;
/// <summary>
/// TODO
/// </summary>
private float _verticalDirection;
/// <summary>
/// TODO
/// </summary>
private void Awake()
{
var hasChild = false;
@ -44,16 +116,25 @@ private void Awake()
ChangeState();
}
/// <summary>
/// TODO
/// </summary>
private void Update()
{
_currentState = _nextState;
}
/// <summary>
/// TODO
/// </summary>
private void ChangeState()
{
StartCoroutine(_currentState);
}
/// <summary>
/// TODO
/// </summary>
private void Steering()
{
_angularDirection = Vector3.SignedAngle(carPosition.transform.forward,
@ -64,30 +145,32 @@ private void Steering()
_turnInput = 1;
}
/// <summary>
/// TODO
/// </summary>
private IEnumerator Stopped()
{
while (_currentState == "Stopped")
{
_distanceToTarget = Vector3.Distance(carPosition.position, _driveTarget.position);
_car.braking = true;
_accelerationInput = 0f;
_turnInput = 0f;
if (_distanceToTarget>stoppingDistance)
{
_nextState = "Slowed";
}
if (_distanceToTarget > stoppingDistance) _nextState = "Slowed";
yield return new WaitForSeconds(1);
}
ChangeState();
}
/// <summary>
/// TODO
/// </summary>
private IEnumerator Slowed()
{
_car.braking = false;
while (_currentState == "Slowed")
{
_verticalDirection = Vector3.Dot(carPosition.transform.forward,
(_driveTarget.position - carPosition.transform.position).normalized);
_distanceToTarget = Vector3.Distance(carPosition.position, _driveTarget.position);
@ -111,9 +194,13 @@ private IEnumerator Slowed()
_car.SetInputs(_accelerationInput, _turnInput);
yield return new WaitForEndOfFrame();
}
ChangeState();
}
/// <summary>
/// TODO
/// </summary>
private IEnumerator Driving()
{
_car.braking = false;
@ -125,27 +212,22 @@ private IEnumerator Driving()
_accelerationInput = 1;
Steering();
if (_distanceToTarget < slowingDistance) _nextState = "Slowed";
_car.SetInputs(_accelerationInput, _turnInput);
yield return new WaitForEndOfFrame();
}
ChangeState();
}
/// <summary>
/// TODO
/// </summary>
private void SlowedCheck()
{
if (_distanceToTarget < stoppingDistance)
{
_nextState = "Stopped";
}
if (_distanceToTarget < stoppingDistance) _nextState = "Stopped";
if (_distanceToTarget > slowingDistance) _nextState = "Driving";
}
}

View file

@ -1,30 +1,69 @@
/*
* author: ryan lin
* date: TODO
* description: TODO
*/
using System.Collections;
using UnityEngine;
public class AiManager : MonoBehaviour
public class AIManager : MonoBehaviour
{
/// <summary>
/// TODO
/// </summary>
public Transform player;
/// <summary>
/// TODO
/// </summary>
public float cullingDistance;
/// <summary>
/// TODO
/// </summary>
public GameObject aiPrefab;
private GameObject[] _ais;
private float _distance;
/// <summary>
/// TODO
/// </summary>
public int maxAI;
/// <summary>
/// TODO
/// </summary>
private GameObject[] _ais;
/// <summary>
/// TODO
/// </summary>
private float _distance;
/// <summary>
/// TODO
/// </summary>
private void Start()
{
StartCoroutine(Manager());
}
/// <summary>
/// TODO
/// </summary>
private void OnDrawGizmos()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(player.position, cullingDistance);
}
// ReSharper disable Unity.PerformanceAnalysis
/// <summary>
/// TODO
/// </summary>
private IEnumerator Manager()
{
while (true)
{
// FIXME: feels weird
_ais = GameObject.FindGameObjectsWithTag("AIs");
if (_ais.Length < maxAI)
{
@ -39,5 +78,6 @@ private IEnumerator Manager()
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.Collections.Generic;
using UnityEngine;
/// <summary>
/// TODO
/// </summary>
public class CarController : MonoBehaviour
{
/// <summary>
/// TODO
/// </summary>
public enum Axel
{
Front,
Rear
}
/// <summary>
/// TODO
/// </summary>
public float acceleration;
/// <summary>
/// TODO
/// </summary>
public float turnAngle;
/// <summary>
/// TODO
/// </summary>
public List<Wheel> wheels;
/// <summary>
/// TODO
/// </summary>
public float brakeForce;
/// <summary>
/// TODO
/// </summary>
public bool braking;
/// <summary>
/// TODO
/// </summary>
private float _currentAcceleration;
/// <summary>
/// TODO
/// </summary>
private float _currentTurn;
/// <summary>
/// TODO
/// </summary>
private void FixedUpdate()
{
Move();
@ -27,18 +67,26 @@ private void FixedUpdate()
Brake();
}
/// <summary>
/// TODO
/// </summary>
public void SetInputs(float forwardAmount, float turnAmount)
{
_currentAcceleration = forwardAmount * acceleration;
_currentTurn = turnAmount * turnAngle;
}
/// <summary>
/// TODO
/// </summary>
public void Move()
{
foreach (var wheel in wheels) wheel.wheelCollider.motorTorque = _currentAcceleration;
}
/// <summary>
/// TODO
/// </summary>
public void Brake()
{
if (braking)
@ -49,6 +97,9 @@ public void Brake()
wheel.wheelCollider.brakeTorque = 0f;
}
/// <summary>
/// TODO
/// </summary>
public void Steering()
{
foreach (var wheel in wheels)
@ -56,6 +107,10 @@ public void Steering()
wheel.wheelCollider.steerAngle = _currentTurn;
}
/// <summary>
/// TODO
/// </summary>
// NOTE: once again, why is this at the bottom lol
[Serializable]
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.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Door : Interactable
/// <summary>
/// TODO
/// </summary>
public class Door : CommonInteractable
{
/// <summary>
/// TODO
/// </summary>
public enum DoorType
{
Sliding,
Rotating
}
/// <summary>
/// TODO
/// </summary>
public List<DoorInput> doors;
/// <summary>
/// TODO
/// </summary>
public float duration;
/// <summary>
/// TODO
/// </summary>
public AnimationCurve curve;
/// <summary>
/// TODO
/// </summary>
private bool _isOpen;
/// <summary>
/// TODO
/// </summary>
private bool _opening;
/// <summary>
/// TODO
/// </summary>
public override void Interact()
{
if (!_opening) StartCoroutine(OpenDoor());
}
/// <summary>
/// TODO
/// </summary>
private IEnumerator OpenDoor()
{
_opening = true;
@ -62,19 +97,19 @@ private IEnumerator OpenDoor()
currentDuration = 0;
_opening = false;
if (_isOpen)
{
_isOpen = false;
}
else
{
_isOpen = true;
}
}
yield return new WaitForEndOfFrame();
}
}
/// <summary>
/// TODO
/// </summary>
// NOTE: why is this at the bottom lol
[Serializable]
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;
/// <summary>
/// TODO
/// </summary>
public class Player : MonoBehaviour
{
public RaycastHit Hit;
/// <summary>
/// TODO
/// </summary>
[SerializeField] private Transform playerPosition;
/// <summary>
/// TODO
/// </summary>
[SerializeField] private float seeDistance;
private Interactable _currentInteractable;
private bool _active;
private bool _raycast;
/// <summary>
/// TODO
/// </summary>
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()
{
_raycast = Physics.Raycast(
@ -28,15 +65,14 @@ private void Update()
);
}
/// <summary>
/// TODO
/// </summary>
private void OnAction()
{
Debug.Log("test");
if (_raycast)
{
if (Hit.transform.CompareTag("Interactable"))
{
Hit.transform.GetComponent<Interactable>().Interact();
}
}
Hit.transform.GetComponent<CommonInteractable>().Interact();
}
}