Merge remote-tracking branch 'origin/Syntriax' into Over

This commit is contained in:
OverflowNarhoym
2022-02-21 16:48:54 +01:00
22 changed files with 1375 additions and 7 deletions

8
Assets/Scripts/AI.meta Normal file
View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: d4e4f06743dc1be42806947cd62fb68e
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,45 @@
using Movement;
using UnityEngine;
namespace AI
{
public class BasicPatrollingEnemyAI : MonoBehaviour
{
[SerializeField] protected bool isMovingRight = false;
protected IMovement movement = null;
protected CollissionChecker leftWallChecker = null;
protected CollissionChecker rightWallChecker = null;
protected CollissionChecker leftGroundChecker = null;
protected CollissionChecker rightGroundChecker = null;
// If moving Right, and either there's no more ground to move into or there is a wall at the Right side of the enemy OR
// If moving Left, and either there's no more ground to move into or there is a wall at the Left side of the enemy
// We should change directions
protected bool ShouldChangeDirection
=> (isMovingRight && (rightWallChecker.IsCollided || !rightGroundChecker.IsCollided)) ||
(!isMovingRight && (leftWallChecker.IsCollided || !leftGroundChecker.IsCollided));
protected virtual void Awake()
{
movement = gameObject.AddComponent<EnemyMovement>();
leftWallChecker = GetCollissionCheckerOnChild("Collission Checkers/Left Wall");
rightWallChecker = GetCollissionCheckerOnChild("Collission Checkers/Right Wall");
leftGroundChecker = GetCollissionCheckerOnChild("Collission Checkers/Left Ground");
rightGroundChecker = GetCollissionCheckerOnChild("Collission Checkers/Right Ground");
}
protected virtual void FixedUpdate()
{
if (ShouldChangeDirection)
isMovingRight = !isMovingRight;
movement.Move(isMovingRight ? 1f : -1f);
}
protected CollissionChecker GetCollissionCheckerOnChild(string childName)
=> transform.Find(childName).GetComponent<CollissionChecker>();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 321c5495f0d597749bf29c3a2966aa4a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: bc70279699861044bb90fcb1700e5eaf
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,18 @@
using UnityEngine;
namespace Movement
{
public class CollissionChecker : MonoBehaviour
{
private Collider2D[] nonAllocColliderArray = new Collider2D[5];
public LayerMask LayerMask = ~((1 << 6) | (1 << 7)); // Everything except the "Player" and "Enemy" layer
public bool IsCollided => Physics2D.OverlapBoxNonAlloc(transform.position, transform.localScale, 0, nonAllocColliderArray, LayerMask) != 0;
public void OnDrawGizmosSelected()
{
Gizmos.color = IsCollided ? Color.green : Color.red;
Gizmos.DrawWireCube(transform.position, transform.localScale);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 02e036b6321fff34cbd154fd665a8b23
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,46 @@
using UnityEngine;
namespace Movement
{
[RequireComponent(typeof(Rigidbody2D))]
public class EnemyMovement : MonoBehaviour, IMovement
{
private Rigidbody2D _rigidbody2D = null;
private bool _isPaused = false;
private float moveValue = 0f;
public float BaseSpeed { get; set; } = 1f;
public bool IsPaused => _isPaused;
private void Awake()
=> _rigidbody2D = GetComponent<Rigidbody2D>();
private void FixedUpdate()
{
if (IsPaused)
return;
Vector2 velocity = _rigidbody2D.velocity;
velocity.x = moveValue;
_rigidbody2D.velocity = velocity;
}
public void Move(float value)
=> moveValue = value * BaseSpeed;
public void Pause()
{
_isPaused = true;
UpdateRigidbody();
}
public void Resume()
{
_isPaused = false;
UpdateRigidbody();
}
private void UpdateRigidbody()
=> _rigidbody2D.simulated = !_isPaused;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4ae10931055aaa44d8c518e9efa3d034
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,10 @@
using Pausable;
namespace Movement
{
public interface IMovement : IPausable
{
float BaseSpeed { get; set; }
void Move(float value);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7372c0f42c210d04f98b21e15803e940
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: a98c359db26a84941b48a708071cf618
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,9 @@
namespace Pausable
{
public interface IPausable
{
bool IsPaused { get; }
void Pause();
void Resume();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: beb0150e245e8cc4cad614670be50ac8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: