Skip to content

Commit

Permalink
Project Source Files
Browse files Browse the repository at this point in the history
  • Loading branch information
SirLorrence authored Oct 3, 2022
1 parent 849e92c commit d1aa47a
Show file tree
Hide file tree
Showing 13 changed files with 1,192 additions and 0 deletions.
98 changes: 98 additions & 0 deletions SourceCode/FishEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// //////////////////////////////
// Authors: Laurence
// GitHub: @SirLorrence
// //////////////////////////////

using System;
using Managers;
using UnityEngine;

public class FishEntity : MonoBehaviour {
private const float _initSpeed = 5f;
private const float _speedIncrease = 1.2f;
private float _currentSpeed;
private float _futureSpeed;
private int _points;
private Animator _animator;
public event Action GameOver;
public event Action<int> ModifyScore;

private enum FishType {
Small,
Large,
Toxic
}

[SerializeField] private FishType fishType;

private void Awake() {
_animator = GetComponent<Animator>();
SetUpEntity();
SetInitSpeed();
}

private void Start() {
AssignEvents();
}

private void Update() {
_currentSpeed = Mathf.Lerp(_currentSpeed, _futureSpeed, Time.deltaTime);
transform.Translate(Vector3.forward * (Time.deltaTime * _currentSpeed));
if (transform.position.z < -5f) {
ModifyScore?.Invoke(-_points);
if (fishType != FishType.Toxic) GameManager.Instance().AudioManager.PlayAudio(AudioID.Miss);
gameObject.SetActive(false);
}
}

private void OnTriggerEnter(Collider other) {
if (other.CompareTag("Player")) {
OnCollected();
}

Debug.Log(other.name);
}

private void AssignEvents() {
// GameManager.Instance().IncreaseDifficulty += IncreaseSpeed;
GameManager.Instance().Restart += OnRest;
ModifyScore += GameManager.Instance().OnModifyScore;
GameOver += GameManager.Instance().OnGameOver;
}

private void SetUpEntity() {
_points = fishType switch {
FishType.Small => 1,
FishType.Large => 5,
_ => 0 // should be impossible to get....hopefully
};
}

private void OnCollected() {
if (fishType == FishType.Toxic) {
GameOver?.Invoke();
GameManager.Instance().AudioManager.PlayAudio(AudioID.Toxic);
}
else {
ModifyScore?.Invoke(_points);
GameManager.Instance().AudioManager.PlayAudio(AudioID.Collected);
}

gameObject.SetActive(false);
}

private void SetInitSpeed() {
_currentSpeed = _initSpeed;
_futureSpeed = _currentSpeed;
_animator.speed = 1;
}


private void OnRest() => SetInitSpeed();

public void IncreaseSpeed() {
var dLvl = GameManager.Instance().DifficultyLevel;
_futureSpeed = _initSpeed * Mathf.Pow(_speedIncrease, dLvl);
_animator.speed += 0.25f;
}
}
38 changes: 38 additions & 0 deletions SourceCode/GameTimer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// //////////////////////////////
// Authors: Laurence
// GitHub: @SirLorrence
// //////////////////////////////

using System;

public class GameTimer {
private float _timer;
private bool _eventSent;
private bool _sendEvent;
private static int _interval;
public event Action TimerEvent;

/// <param name="interval">How often to send out event</param>
public GameTimer(int interval) => _interval = interval;

public void Start() {
_timer = 0;
_sendEvent = false;
_eventSent = false;
}

public void Tick(float gameTick) {
_timer += gameTick;
if (_timer > 1) {
_sendEvent = (int)_timer % _interval == 0;

// to make sure the event is sent once
if (_sendEvent && !_eventSent) {
TimerEvent?.Invoke();
_eventSent = true;
}

if (!_sendEvent) _eventSent = false;
}
}
}
24 changes: 24 additions & 0 deletions SourceCode/GenericObjectPool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// //////////////////////////////
// Authors: Laurence
// GitHub: @SirLorrence
// //////////////////////////////

using UnityEngine;

public class GenericObjectPool {
private static int POOL_SIZE;
public GameObject[] pool;
private GameObject obj;
private string Name;

public delegate void CreateObject(GameObject gameObject, GameObject[] pool, string pName);

public GenericObjectPool(GameObject poolObject, int size, string poolName) {
POOL_SIZE = size;
pool = new GameObject[POOL_SIZE];
obj = poolObject;
Name = poolName;
}

public void CreatePool(CreateObject create) => create(obj, pool, Name);
}
47 changes: 47 additions & 0 deletions SourceCode/IKController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// //////////////////////////////
// Authors: Laurence
// GitHub: @SirLorrence
// //////////////////////////////

using UnityEngine;

public class IKController : MonoBehaviour {
private Animator _animator;
[SerializeField] private Transform _rightHandObj;
[SerializeField] private Transform _leftHandObj;
[SerializeField] private Transform _lookPos;

[Space] [SerializeField] [Range(0f, 1f)]
private float rightWeight = .25f, leftWeight = 1, bodyWeight = .1f;


private void Awake() {
_animator = GetComponent<Animator>();
}


private void OnAnimatorIK(int layerIndex) {
// Neck and Torso movement
_animator.SetLookAtWeight(1, bodyWeight);
_animator.SetLookAtPosition(_lookPos.position);

//Hand Placement
_animator.SetIKPositionWeight(AvatarIKGoal.RightHand, rightWeight);
_animator.SetIKRotationWeight(AvatarIKGoal.RightHand, rightWeight);
_animator.SetIKPosition(AvatarIKGoal.RightHand, _rightHandObj.position);
_animator.SetIKRotation(AvatarIKGoal.RightHand, _rightHandObj.rotation);

_animator.SetIKPositionWeight(AvatarIKGoal.LeftHand, leftWeight);
_animator.SetIKRotationWeight(AvatarIKGoal.LeftHand, leftWeight);
_animator.SetIKPosition(AvatarIKGoal.LeftHand, _leftHandObj.position);
_animator.SetIKRotation(AvatarIKGoal.LeftHand, _leftHandObj.rotation);
}


#if UNITY_EDITOR
private void OnDrawGizmos() {
// var tLocation = _lookPos.position + _body.position;
// Gizmos.DrawSphere(tLocation,.25f);
}
#endif
}
76 changes: 76 additions & 0 deletions SourceCode/Managers/AudioManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// //////////////////////////////
// Authors: Laurence
// GitHub: @SirLorrence
// //////////////////////////////

using System;
using System.Collections;
using UnityEngine;

namespace Managers {
public enum AudioID {
Collected,
Toxic,
Miss
}

public class AudioManager : MonoBehaviour {
private const string POOL_NAME = "Audio Sources";
private const Int16 SFX_SIZE = 10;
private GenericObjectPool _sourceAudioPool;

[SerializeField] private AudioClip _collectedClip;
[SerializeField] private AudioClip _toxicClip;
[SerializeField] private AudioClip _missedClip;

private void Awake() {
GameObject audioObj = new GameObject("SFX");
audioObj.AddComponent<AudioSource>();
_sourceAudioPool = new GenericObjectPool(audioObj, SFX_SIZE, POOL_NAME);
}

private void Start() {
_sourceAudioPool.CreatePool(Create);
}

private void Create(GameObject obj, GameObject[] pool, string poolName) {
var poolTransform = new GameObject(poolName) {
transform = { parent = transform }
};

for (int i = 0; i < pool.Length; i++) {
var gm = Instantiate(obj, poolTransform.transform);
gm.SetActive(false);
pool[i] = gm;
}
}

public void PlayAudio(AudioID id) {
AudioClip audioClip = id switch {
AudioID.Collected => _collectedClip,
AudioID.Toxic => _toxicClip,
AudioID.Miss => _missedClip,
};
StartCoroutine(RunAudio(audioClip));
}

IEnumerator RunAudio(AudioClip clip) {
var sourceGameObject = GetFromPool();
var source = sourceGameObject.GetComponent<AudioSource>();
source.clip = clip;
sourceGameObject.SetActive(true);
source.Play();
yield return new WaitWhile(() => source.isPlaying);
sourceGameObject.SetActive(false);
}

private GameObject GetFromPool() {
foreach (var source in _sourceAudioPool.pool) {
if (!source.activeInHierarchy)
return source;
}

return null;
}
}
}
Loading

0 comments on commit d1aa47a

Please sign in to comment.