moved stuff to a different entity script
This commit is contained in:
parent
f4344c4700
commit
a0bfc600ef
14 changed files with 497 additions and 189 deletions
|
|
@ -1,11 +1,14 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class Ability : MonoBehaviour
|
||||
{
|
||||
[Header("Identification")]
|
||||
public string abilityName;
|
||||
public Entity thisEntity;
|
||||
[Header("Cooldown")]
|
||||
protected float currentCooldown;
|
||||
public float cooldown;
|
||||
[Header("Stats")]
|
||||
public float power;
|
||||
public Vector3 targetLocation;
|
||||
|
||||
|
|
@ -15,11 +18,6 @@ public class Ability : MonoBehaviour
|
|||
{
|
||||
currentCooldown -= Time.deltaTime;
|
||||
}
|
||||
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
TryAbility(); //testing, please remove
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryAbility()
|
||||
|
|
@ -28,8 +26,10 @@ public class Ability : MonoBehaviour
|
|||
{
|
||||
AbilityEffects();
|
||||
currentCooldown = cooldown;
|
||||
Debug.Log($"Ability {abilityName} SUCCESS");
|
||||
return true;
|
||||
}
|
||||
Debug.Log($"Ability {abilityName} ON COOLDOWN");
|
||||
return false;
|
||||
}
|
||||
protected virtual void AbilityEffects()
|
||||
|
|
|
|||
|
|
@ -1,30 +1,42 @@
|
|||
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()
|
||||
{
|
||||
Vector2 direction = new Vector2(0, rb.linearVelocityY);
|
||||
Vector2 direction = new Vector2(0, stats.rb.linearVelocityY);
|
||||
Entity target = player;
|
||||
if (!closestEntity && Vector3.Distance(player.transform.position, transform.position) <= playerMinDistance)
|
||||
{
|
||||
rb.linearVelocity = direction; //repeated code sucks but it works in this case i guess
|
||||
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 * speed;
|
||||
if ((target.transform.position.y - transform.position.y > maxHeightDifference || (DetectWalls() && Vector3.Distance(target.transform.position, transform.position) > minTargetJumpDistance)) && OnGround())
|
||||
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 = jumpPower;
|
||||
direction.y = stats.jumpPower;
|
||||
}
|
||||
rb.linearVelocity = direction;
|
||||
stats.rb.linearVelocity = direction;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,19 +5,19 @@ public class Enemy : AutoControlledEntity
|
|||
{
|
||||
private void FixedUpdate()
|
||||
{
|
||||
Vector2 direction = new Vector2(0, rb.linearVelocityY);
|
||||
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 * speed;
|
||||
if ((closestEntity.transform.position.y - transform.position.y > maxHeightDifference || (DetectWalls() && Vector3.Distance(closestEntity.transform.position, transform.position) > minTargetJumpDistance)) && OnGround())
|
||||
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 = jumpPower;
|
||||
direction.y = stats.jumpPower;
|
||||
}
|
||||
}
|
||||
rb.linearVelocity = direction;
|
||||
stats.rb.linearVelocity = direction;
|
||||
}
|
||||
|
||||
protected override void OnDeath()
|
||||
public override void OnDeath()
|
||||
{
|
||||
base.OnDeath();
|
||||
Destroy(gameObject);
|
||||
|
|
|
|||
|
|
@ -4,45 +4,14 @@ using UnityEngine;
|
|||
|
||||
public class Entity : MonoBehaviour
|
||||
{
|
||||
[Header("Health")]
|
||||
public float health;
|
||||
public float maxHealth;
|
||||
[Header("Stats")]
|
||||
public float speed;
|
||||
public float jumpPower;
|
||||
[Header("Abilities")]
|
||||
public List<Ability> abilities = new();
|
||||
public Transform attackOriginPoint;
|
||||
[SerializeField] protected Transform attackOriginCenter;
|
||||
[Header("Stats")]
|
||||
public EntityStats stats;
|
||||
[Header("Cache")]
|
||||
[SerializeField] protected Rigidbody2D rb;
|
||||
public List<Entity> entitiesInRange = new();
|
||||
public Entity closestEntity;
|
||||
[Header("Ground Detection")]
|
||||
[SerializeField] private Transform groundCheck;
|
||||
[SerializeField] private LayerMask groundLayer;
|
||||
|
||||
public void TakeDamage(float damage)
|
||||
{
|
||||
health -= damage;
|
||||
if (health <= 0)
|
||||
{
|
||||
OnDeath();
|
||||
}
|
||||
}
|
||||
|
||||
public void Heal(float healing)
|
||||
{
|
||||
health += healing;
|
||||
health = Math.Clamp(health, 0, maxHealth);
|
||||
}
|
||||
|
||||
protected virtual void OnDeath()
|
||||
public virtual void OnDeath()
|
||||
{
|
||||
|
||||
}
|
||||
protected bool OnGround()
|
||||
{
|
||||
return Physics2D.OverlapCircle(groundCheck.position, 0.05f, groundLayer);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
42
Assets/Scripts/EntityStats.cs
Normal file
42
Assets/Scripts/EntityStats.cs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
using System;
|
||||
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("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 void TakeDamage(float damage)
|
||||
{
|
||||
health -= damage;
|
||||
if (health <= 0)
|
||||
{
|
||||
thisEntity.OnDeath();
|
||||
}
|
||||
}
|
||||
|
||||
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/EntityStats.cs.meta
Normal file
2
Assets/Scripts/EntityStats.cs.meta
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 597d889207f92f03d81551cc8069aeee
|
||||
|
|
@ -7,16 +7,20 @@ public class Player : Entity
|
|||
[SerializeField] private Camera cam;
|
||||
private void Update()
|
||||
{
|
||||
attackOriginCenter.Lookat2D(cam.ScreenToWorldPoint(Input.mousePosition));
|
||||
stats.attackOriginCenter.Lookat2D(cam.ScreenToWorldPoint(Input.mousePosition));
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
stats.abilities[0].TryAbility();
|
||||
}
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
Vector2 movement = new Vector2(Input.GetAxis("Horizontal") * speed, rb.linearVelocityY);
|
||||
if (Input.GetKeyDown(KeyCode.Space) && OnGround())
|
||||
Vector2 movement = new Vector2(Input.GetAxis("Horizontal") * stats.speed, stats.rb.linearVelocityY);
|
||||
if (Input.GetKeyDown(KeyCode.Space) && stats.OnGround())
|
||||
{
|
||||
movement.y = jumpPower;
|
||||
movement.y = stats.jumpPower;
|
||||
}
|
||||
rb.linearVelocity = movement;
|
||||
stats.rb.linearVelocity = movement;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ public class Projectile : MonoBehaviour
|
|||
{
|
||||
if (!other.CompareTag(tag) && other.TryGetComponent(out Entity isEntity))
|
||||
{
|
||||
isEntity.TakeDamage(damage);
|
||||
isEntity.stats.TakeDamage(damage);
|
||||
currentPierced++;
|
||||
if (currentPierced > pierceAmount)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -17,6 +17,6 @@ public class ShootBullet : Ability
|
|||
newProjectile.damage = power;
|
||||
newProjectile.lifetime = projectileLifetime;
|
||||
newProjectile.pierceAmount = pierceAmount;
|
||||
newProjectile.transform.Lookat2D(thisEntity.attackOriginPoint.position); //targetLocation);
|
||||
newProjectile.transform.Lookat2D(thisEntity.stats.attackOriginPoint.position); //targetLocation);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue