movement, automated enemies, automated players, basic abilities

This commit is contained in:
Sylvia 2026-06-09 05:27:47 -07:00
parent 23576e137e
commit f4344c4700
31 changed files with 4343 additions and 70 deletions

39
Assets/Scripts/Ability.cs Normal file
View file

@ -0,0 +1,39 @@
using System;
using UnityEngine;
public class Ability : MonoBehaviour
{
public Entity thisEntity;
protected float currentCooldown;
public float cooldown;
public float power;
public Vector3 targetLocation;
protected virtual void Update()
{
if (currentCooldown > 0f)
{
currentCooldown -= Time.deltaTime;
}
if (Input.GetMouseButtonDown(0))
{
TryAbility(); //testing, please remove
}
}
public bool TryAbility()
{
if (currentCooldown <= 0f)
{
AbilityEffects();
currentCooldown = cooldown;
return true;
}
return false;
}
protected virtual void AbilityEffects()
{
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 977830b8cdc83e63a8cb158ab84c0d0d

View 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;
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: c03e169f911b6605cb6e9e94749f7223

View file

@ -1,16 +1,30 @@
using System;
using UnityEngine;
public class AutomatedPlayer : MonoBehaviour
public class AutomatedPlayer : AutoControlledEntity
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
public Player player;
public float playerMinDistance;
// Update is called once per frame
void Update()
{
}
private void FixedUpdate()
{
Vector2 direction = new Vector2(0, 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
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.y = jumpPower;
}
rb.linearVelocity = direction;
}
}

View 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;
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: c400f563d8deda32ebf80ab7262922f3

View file

@ -1,16 +1,25 @@
using System;
using UnityEngine;
public class Enemy : MonoBehaviour
public class Enemy : AutoControlledEntity
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
private void FixedUpdate()
{
Vector2 direction = new Vector2(0, 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.y = jumpPower;
}
}
rb.linearVelocity = direction;
}
// Update is called once per frame
void Update()
{
}
protected override void OnDeath()
{
base.OnDeath();
Destroy(gameObject);
}
}

View file

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using UnityEngine;
public class Entity : MonoBehaviour
@ -9,8 +10,17 @@ public class Entity : MonoBehaviour
[Header("Stats")]
public float speed;
public float jumpPower;
[Header("Abilities")]
public List<Ability> abilities = new();
public Transform attackOriginPoint;
[SerializeField] protected Transform attackOriginCenter;
[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)
{
@ -31,4 +41,8 @@ public class Entity : MonoBehaviour
{
}
protected bool OnGround()
{
return Physics2D.OverlapCircle(groundCheck.position, 0.05f, groundLayer);
}
}

View file

@ -1,11 +1,15 @@
using System;
using Core.Extensions;
using UnityEngine;
public class Player : Entity
{
[Header("Ground Detection")]
[SerializeField] private Transform groundCheck;
[SerializeField] private LayerMask groundLayer;
[SerializeField] private Camera cam;
private void Update()
{
attackOriginCenter.Lookat2D(cam.ScreenToWorldPoint(Input.mousePosition));
}
private void FixedUpdate()
{
Vector2 movement = new Vector2(Input.GetAxis("Horizontal") * speed, rb.linearVelocityY);
@ -15,9 +19,4 @@ public class Player : Entity
}
rb.linearVelocity = movement;
}
private bool OnGround()
{
return Physics2D.OverlapCircle(groundCheck.position, 0.05f, groundLayer);
}
}

View file

@ -1,16 +1,38 @@
using System;
using UnityEngine;
public class PlayerSwitcher : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
[SerializeField] private Player player1;
[SerializeField] private AutomatedPlayer player1AI;
[SerializeField] private Player player2;
[SerializeField] private AutomatedPlayer player2AI;
// Update is called once per frame
void Update()
{
}
private void Update()
{
if(Input.GetKeyDown(KeyCode.E))
{
SwitchPlayers();
}
}
public void SwitchPlayers() //i bet if i made it a parameter i wouldn't have to make an if statement. but it's literally 5 am and i'm lazy
{
if (player1.enabled)
{
player1AI.enabled = true;
player1.enabled = false;
player2.enabled = true;
player2AI.enabled = false;
(player2.transform.position, player1AI.transform.position) = (player1AI.transform.position, player2.transform.position);
}
else
{
player2AI.enabled = true;
player2.enabled = false;
player1.enabled = true;
player1AI.enabled = false;
(player1.transform.position, player2AI.transform.position) = (player2AI.transform.position, player1.transform.position);
}
}
}

View file

@ -0,0 +1,40 @@
using System;
using UnityEngine;
public class Projectile : MonoBehaviour
{
public Entity owner;
public float damage;
public float lifetime;
private int currentPierced;
public int pierceAmount;
public float speed;
[SerializeField] private Rigidbody2D rb;
private void Start()
{
Destroy(gameObject, lifetime);
}
private void FixedUpdate()
{
rb.linearVelocity = transform.right * speed;
}
private void OnTriggerEnter2D(Collider2D other)
{
if (!other.CompareTag(tag) && other.TryGetComponent(out Entity isEntity))
{
isEntity.TakeDamage(damage);
currentPierced++;
if (currentPierced > pierceAmount)
{
Destroy(gameObject);
}
}
else if (other.gameObject.layer == 3) //hard coded layer but who tf cares i'm not inputting the variable every time i make a projectile lol
{
Destroy(gameObject);
}
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 3aed04444af8b4d13b5fe9031150044a

View file

@ -0,0 +1,22 @@
using Core.Extensions;
using UnityEngine;
public class ShootBullet : Ability
{
[SerializeField] private Projectile projectile;
[SerializeField] private float projectileSpeed;
[SerializeField] private float projectileLifetime;
[SerializeField] private int pierceAmount;
protected override void AbilityEffects()
{
base.AbilityEffects();
Projectile newProjectile = Instantiate(projectile, thisEntity.transform.position, projectile.transform.rotation);
newProjectile.owner = thisEntity;
newProjectile.tag = thisEntity.tag;
newProjectile.speed = projectileSpeed;
newProjectile.damage = power;
newProjectile.lifetime = projectileLifetime;
newProjectile.pierceAmount = pierceAmount;
newProjectile.transform.Lookat2D(thisEntity.attackOriginPoint.position); //targetLocation);
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: c2e841223273eee80837e20d27430787