Enemy Movement and BasicPatrollingEnemyAI has added

This commit is contained in:
Syntriax 2022-02-21 15:53:23 +03:00
parent 87f0652ce1
commit 4cfbf23960
6 changed files with 301 additions and 0 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,58 @@
using Movement;
using UnityEngine;
namespace AI
{
public class BasicPatrollingEnemyAI : MonoBehaviour
{
[SerializeField] protected bool isMovingRight = false;
[Space]
[Header("Left Detector")]
[SerializeField] protected Vector2 leftCliffDetectorOrigin = Vector2.left;
[SerializeField] protected Vector2 leftCliffDetectorSize = Vector2.one;
[Space]
[Header("Right Detector")]
[SerializeField] protected Vector2 rightCliffDetectorOrigin = Vector2.right;
[SerializeField] protected Vector2 rightCliffDetectorSize = Vector2.one;
[Space]
[Header("Right Detector")]
[SerializeField] protected LayerMask groundLayerMask = ~(1 << 6); // Everything except the "Player" layer
protected IMovement movement = null;
protected Collider2D[] nonAllocColliderArray = new Collider2D[10];
protected Vector2 leftCliffPosition => GetCliffPositionInWorld(leftCliffDetectorOrigin);
protected Vector2 rightCliffPosition => GetCliffPositionInWorld(rightCliffDetectorOrigin);
protected bool IsLeftDetectorCollided => Physics2D.OverlapBoxNonAlloc(leftCliffPosition, leftCliffDetectorSize, 0, nonAllocColliderArray, groundLayerMask) != 0;
protected bool IsRightDetectorCollided => Physics2D.OverlapBoxNonAlloc(rightCliffPosition, rightCliffDetectorSize, 0, nonAllocColliderArray, groundLayerMask) != 0;
protected virtual void Awake()
{
movement = gameObject.AddComponent<EnemyMovement>();
}
protected virtual void FixedUpdate()
{
if ((isMovingRight && !IsRightDetectorCollided) || (!isMovingRight && !IsLeftDetectorCollided))
isMovingRight = !isMovingRight;
movement.Move(isMovingRight ? 1f : -1f);
}
protected Vector2 GetCliffPositionInWorld(Vector2 cliffPosition)
=> cliffPosition + (Vector2)transform.position;
protected virtual void OnDrawGizmosSelected()
{
Gizmos.color = IsLeftDetectorCollided ? Color.green : Color.red;
Gizmos.DrawWireCube(leftCliffPosition, leftCliffDetectorSize);
Gizmos.color = IsRightDetectorCollided ? Color.green : Color.red;
Gizmos.DrawWireCube(rightCliffPosition, rightCliffDetectorSize);
}
}
}

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,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,167 @@
{
"templatePinStates": [],
"dependencyTypeInfos": [
{
"userAdded": false,
"type": "UnityEngine.AnimationClip",
"ignore": false,
"defaultInstantiationMode": 0,
"supportsModification": true
},
{
"userAdded": false,
"type": "UnityEditor.Animations.AnimatorController",
"ignore": false,
"defaultInstantiationMode": 0,
"supportsModification": true
},
{
"userAdded": false,
"type": "UnityEngine.AnimatorOverrideController",
"ignore": false,
"defaultInstantiationMode": 0,
"supportsModification": true
},
{
"userAdded": false,
"type": "UnityEditor.Audio.AudioMixerController",
"ignore": false,
"defaultInstantiationMode": 0,
"supportsModification": true
},
{
"userAdded": false,
"type": "UnityEngine.ComputeShader",
"ignore": true,
"defaultInstantiationMode": 1,
"supportsModification": true
},
{
"userAdded": false,
"type": "UnityEngine.Cubemap",
"ignore": false,
"defaultInstantiationMode": 0,
"supportsModification": true
},
{
"userAdded": false,
"type": "UnityEngine.GameObject",
"ignore": false,
"defaultInstantiationMode": 0,
"supportsModification": true
},
{
"userAdded": false,
"type": "UnityEditor.LightingDataAsset",
"ignore": false,
"defaultInstantiationMode": 0,
"supportsModification": false
},
{
"userAdded": false,
"type": "UnityEngine.LightingSettings",
"ignore": false,
"defaultInstantiationMode": 0,
"supportsModification": true
},
{
"userAdded": false,
"type": "UnityEngine.Material",
"ignore": false,
"defaultInstantiationMode": 0,
"supportsModification": true
},
{
"userAdded": false,
"type": "UnityEditor.MonoScript",
"ignore": true,
"defaultInstantiationMode": 1,
"supportsModification": true
},
{
"userAdded": false,
"type": "UnityEngine.PhysicMaterial",
"ignore": false,
"defaultInstantiationMode": 0,
"supportsModification": true
},
{
"userAdded": false,
"type": "UnityEngine.PhysicsMaterial2D",
"ignore": false,
"defaultInstantiationMode": 0,
"supportsModification": true
},
{
"userAdded": false,
"type": "UnityEngine.Rendering.PostProcessing.PostProcessProfile",
"ignore": false,
"defaultInstantiationMode": 0,
"supportsModification": true
},
{
"userAdded": false,
"type": "UnityEngine.Rendering.PostProcessing.PostProcessResources",
"ignore": false,
"defaultInstantiationMode": 0,
"supportsModification": true
},
{
"userAdded": false,
"type": "UnityEngine.Rendering.VolumeProfile",
"ignore": false,
"defaultInstantiationMode": 0,
"supportsModification": true
},
{
"userAdded": false,
"type": "UnityEditor.SceneAsset",
"ignore": false,
"defaultInstantiationMode": 0,
"supportsModification": false
},
{
"userAdded": false,
"type": "UnityEngine.Shader",
"ignore": true,
"defaultInstantiationMode": 1,
"supportsModification": true
},
{
"userAdded": false,
"type": "UnityEngine.ShaderVariantCollection",
"ignore": true,
"defaultInstantiationMode": 1,
"supportsModification": true
},
{
"userAdded": false,
"type": "UnityEngine.Texture",
"ignore": false,
"defaultInstantiationMode": 0,
"supportsModification": true
},
{
"userAdded": false,
"type": "UnityEngine.Texture2D",
"ignore": false,
"defaultInstantiationMode": 0,
"supportsModification": true
},
{
"userAdded": false,
"type": "UnityEngine.Timeline.TimelineAsset",
"ignore": false,
"defaultInstantiationMode": 0,
"supportsModification": true
}
],
"defaultDependencyTypeInfo": {
"userAdded": false,
"type": "<default_scene_template_dependencies>",
"ignore": false,
"defaultInstantiationMode": 1,
"supportsModification": true
},
"newSceneOverride": 0
}