more abilities and fixes
This commit is contained in:
parent
cb4470f2d6
commit
fc2329a873
31 changed files with 268 additions and 52 deletions
23
Assets/Scripts/Entities/AutoControlledEntity.cs
Normal file
23
Assets/Scripts/Entities/AutoControlledEntity.cs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
using UnityEngine;
|
||||
|
||||
public class AutoControlledEntity : Entity
|
||||
{
|
||||
[Header("Movement AI")]
|
||||
[SerializeField] protected float maxTargettingRange;
|
||||
[SerializeField] protected float maxHeightDifference;
|
||||
[SerializeField] protected float minTargetJumpDistance;
|
||||
[SerializeField] protected Transform[] wallDetectors;
|
||||
[SerializeField] protected LayerMask wallLayer;
|
||||
|
||||
protected bool DetectWalls()
|
||||
{
|
||||
foreach (Transform wallDetector in wallDetectors)
|
||||
{
|
||||
if (Physics2D.OverlapCircle(wallDetector.position, 0.05f, wallLayer))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Entities/AutoControlledEntity.cs.meta
Normal file
2
Assets/Scripts/Entities/AutoControlledEntity.cs.meta
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c03e169f911b6605cb6e9e94749f7223
|
||||
46
Assets/Scripts/Entities/AutomatedPlayer.cs
Normal file
46
Assets/Scripts/Entities/AutomatedPlayer.cs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
using System;
|
||||
using Core.Extensions;
|
||||
using UnityEngine;
|
||||
|
||||
public class AutomatedPlayer : AutoControlledEntity
|
||||
{
|
||||
public Player player;
|
||||
public float playerMinDistance;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (closestEntity)
|
||||
{
|
||||
stats.attackOriginCenter.Lookat2D(closestEntity.transform.position);
|
||||
foreach (Ability ability in stats.abilities)
|
||||
{
|
||||
ability.TryAbility();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (!stats.isStalled)
|
||||
{
|
||||
Vector2 direction = new Vector2(0, stats.rb.linearVelocityY);
|
||||
Entity target = player;
|
||||
if (!closestEntity && Vector3.Distance(player.transform.position, transform.position) <= playerMinDistance)
|
||||
{
|
||||
stats.rb.linearVelocity = direction; //repeated code sucks but it works in this case i guess
|
||||
return; //do not bother running any more code lol you're already next to the player and no enemies nearby
|
||||
}
|
||||
if (closestEntity && Vector3.Distance(closestEntity.transform.position, transform.position) < maxTargettingRange)
|
||||
{
|
||||
target = closestEntity;
|
||||
}
|
||||
direction.x = (target.transform.position - transform.position).normalized.x * stats.speed;
|
||||
if (/*(target.transform.position.y - transform.position.y > maxHeightDifference ||*/ (DetectWalls() && Vector3.Distance(target.transform.position, transform.position) > minTargetJumpDistance)/*)*/ && stats.OnGround())
|
||||
{
|
||||
direction.y = stats.jumpPower;
|
||||
}
|
||||
FlipSprite(direction);
|
||||
stats.rb.linearVelocity = direction;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Entities/AutomatedPlayer.cs.meta
Normal file
2
Assets/Scripts/Entities/AutomatedPlayer.cs.meta
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 88cc3202b26534d78b932ebaf49077f4
|
||||
42
Assets/Scripts/Entities/DetectEntities.cs
Normal file
42
Assets/Scripts/Entities/DetectEntities.cs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class DetectEntities : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Entity thisEntity;
|
||||
private void OnTriggerEnter2D(Collider2D other)
|
||||
{
|
||||
if (!other.CompareTag(thisEntity.tag) && other.TryGetComponent(out Entity isEntity))
|
||||
{
|
||||
thisEntity.entitiesInRange.Add(isEntity);
|
||||
thisEntity.closestEntity = FindClosestEntity();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerExit2D(Collider2D other)
|
||||
{
|
||||
if (other.TryGetComponent(out Entity isEntity) && thisEntity.entitiesInRange.Contains(isEntity))
|
||||
{
|
||||
thisEntity.entitiesInRange.Remove(isEntity);
|
||||
if (thisEntity.closestEntity == isEntity)
|
||||
{
|
||||
thisEntity.closestEntity = FindClosestEntity();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Entity FindClosestEntity()
|
||||
{
|
||||
Entity currentClosestEntity = null;
|
||||
foreach (Entity entityFound in thisEntity.entitiesInRange)
|
||||
{
|
||||
if (!currentClosestEntity ||
|
||||
Vector3.Distance(entityFound.transform.position, thisEntity.transform.position) <
|
||||
Vector3.Distance(currentClosestEntity.transform.position, thisEntity.transform.position))
|
||||
{
|
||||
currentClosestEntity = entityFound;
|
||||
}
|
||||
}
|
||||
return currentClosestEntity;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Entities/DetectEntities.cs.meta
Normal file
2
Assets/Scripts/Entities/DetectEntities.cs.meta
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c400f563d8deda32ebf80ab7262922f3
|
||||
29
Assets/Scripts/Entities/Enemy.cs
Normal file
29
Assets/Scripts/Entities/Enemy.cs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class Enemy : AutoControlledEntity
|
||||
{
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (!stats.isStalled)
|
||||
{
|
||||
Vector2 direction = new Vector2(0, stats.rb.linearVelocityY);
|
||||
if (closestEntity && Vector3.Distance(closestEntity.transform.position, transform.position) < maxTargettingRange)
|
||||
{
|
||||
direction.x = (closestEntity.transform.position - transform.position).normalized.x * stats.speed;
|
||||
if ((closestEntity.transform.position.y - transform.position.y > maxHeightDifference || (DetectWalls() && Vector3.Distance(closestEntity.transform.position, transform.position) > minTargetJumpDistance)) && stats.OnGround())
|
||||
{
|
||||
direction.y = stats.jumpPower;
|
||||
}
|
||||
}
|
||||
FlipSprite(direction);
|
||||
stats.rb.linearVelocity = direction;
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnDeath()
|
||||
{
|
||||
base.OnDeath();
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Entities/Enemy.cs.meta
Normal file
2
Assets/Scripts/Entities/Enemy.cs.meta
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 29eabb6316cb9023da130abba3ba64fc
|
||||
31
Assets/Scripts/Entities/Entity.cs
Normal file
31
Assets/Scripts/Entities/Entity.cs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Entity : MonoBehaviour
|
||||
{
|
||||
[Header("Stats")]
|
||||
public EntityStats stats;
|
||||
[Header("Cache")]
|
||||
public List<Entity> entitiesInRange = new();
|
||||
public Entity closestEntity;
|
||||
|
||||
public virtual void OnDeath()
|
||||
{
|
||||
|
||||
}
|
||||
protected void FlipSprite(Vector2 lookDirection)
|
||||
{
|
||||
if (lookDirection.x > 0f && stats.isFacingRight)
|
||||
{
|
||||
stats.sprite.flipX = true;
|
||||
stats.isFacingRight = !stats.isFacingRight;
|
||||
}
|
||||
else if (lookDirection.x < 0f && !stats.isFacingRight)
|
||||
{
|
||||
stats.sprite.flipX = false;
|
||||
stats.isFacingRight = !stats.isFacingRight;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
2
Assets/Scripts/Entities/Entity.cs.meta
Normal file
2
Assets/Scripts/Entities/Entity.cs.meta
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a436974b96680ea6287afa659d6b0b4a
|
||||
67
Assets/Scripts/Entities/EntityStats.cs
Normal file
67
Assets/Scripts/Entities/EntityStats.cs
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class EntityStats : MonoBehaviour
|
||||
{
|
||||
public Entity thisEntity;
|
||||
[Header("Health")]
|
||||
public float health;
|
||||
public float maxHealth;
|
||||
[Header("Stats")]
|
||||
public float speed;
|
||||
public float jumpPower;
|
||||
[Header("State")]
|
||||
public bool isStalled;
|
||||
[Header("Ground Detection")]
|
||||
[SerializeField] private Transform groundCheck;
|
||||
[SerializeField] private LayerMask groundLayer;
|
||||
[Header("Attack Origin")]
|
||||
public Transform attackOriginPoint;
|
||||
public Transform attackOriginCenter;
|
||||
[Header("Abilities")]
|
||||
public List<Ability> abilities = new();
|
||||
[Header("Cache")]
|
||||
public Rigidbody2D rb;
|
||||
public SpriteRenderer sprite;
|
||||
public bool isFacingRight;
|
||||
public Color originalColor; //remove later
|
||||
public float damageColorChangeSpeed;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
originalColor = sprite.color;
|
||||
}
|
||||
|
||||
public void TakeDamage(float damage)
|
||||
{
|
||||
health -= damage;
|
||||
StartCoroutine(DamageVisual());
|
||||
if (health <= 0)
|
||||
{
|
||||
thisEntity.OnDeath();
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator DamageVisual()
|
||||
{
|
||||
float currentState = 0;
|
||||
while (currentState < 1)
|
||||
{
|
||||
currentState += Time.deltaTime * damageColorChangeSpeed;
|
||||
sprite.color = Color.Lerp(Color.gray, originalColor, currentState);
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void Heal(float healing)
|
||||
{
|
||||
health += healing;
|
||||
health = Math.Clamp(health, 0, maxHealth);
|
||||
}
|
||||
public bool OnGround()
|
||||
{
|
||||
return Physics2D.OverlapCircle(groundCheck.position, 0.05f, groundLayer);
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Entities/EntityStats.cs.meta
Normal file
2
Assets/Scripts/Entities/EntityStats.cs.meta
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 597d889207f92f03d81551cc8069aeee
|
||||
30
Assets/Scripts/Entities/Player.cs
Normal file
30
Assets/Scripts/Entities/Player.cs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
using System;
|
||||
using Core.Extensions;
|
||||
using UnityEngine;
|
||||
|
||||
public class Player : Entity
|
||||
{
|
||||
[SerializeField] private Camera cam;
|
||||
private void Update()
|
||||
{
|
||||
stats.attackOriginCenter.Lookat2D(cam.ScreenToWorldPoint(Input.mousePosition));
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
stats.abilities[0].TryAbility();
|
||||
}
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (!stats.isStalled)
|
||||
{
|
||||
Vector2 movement = new Vector2(Input.GetAxis("Horizontal") * stats.speed, stats.rb.linearVelocityY);
|
||||
if (Input.GetKeyDown(KeyCode.Space) && stats.OnGround())
|
||||
{
|
||||
movement.y = stats.jumpPower;
|
||||
}
|
||||
FlipSprite(movement);
|
||||
stats.rb.linearVelocity = movement;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Entities/Player.cs.meta
Normal file
2
Assets/Scripts/Entities/Player.cs.meta
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: d4ea4a5fe3d2523039e302c010b04daf
|
||||
Loading…
Add table
Add a link
Reference in a new issue