uh you can switch players now

This commit is contained in:
Sylvia 2026-06-11 02:35:57 -07:00
parent a0bfc600ef
commit cb4470f2d6
11 changed files with 582 additions and 235 deletions

View file

@ -21,22 +21,26 @@ public class AutomatedPlayer : AutoControlledEntity
private void FixedUpdate()
{
Vector2 direction = new Vector2(0, stats.rb.linearVelocityY);
Entity target = player;
if (!closestEntity && Vector3.Distance(player.transform.position, transform.position) <= playerMinDistance)
if (!stats.isStalled)
{
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
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;
}
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;
}
stats.rb.linearVelocity = direction;
}
}

View file

@ -5,16 +5,20 @@ public class Enemy : AutoControlledEntity
{
private void FixedUpdate()
{
Vector2 direction = new Vector2(0, stats.rb.linearVelocityY);
if (closestEntity && Vector3.Distance(closestEntity.transform.position, transform.position) < maxTargettingRange)
if (!stats.isStalled)
{
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())
Vector2 direction = new Vector2(0, stats.rb.linearVelocityY);
if (closestEntity && Vector3.Distance(closestEntity.transform.position, transform.position) < maxTargettingRange)
{
direction.y = stats.jumpPower;
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;
}
stats.rb.linearVelocity = direction;
}
public override void OnDeath()

View file

@ -14,4 +14,18 @@ public class Entity : MonoBehaviour
{
}
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;
}
}
}

View file

@ -11,6 +11,8 @@ public class EntityStats : MonoBehaviour
[Header("Stats")]
public float speed;
public float jumpPower;
[Header("State")]
public bool isStalled;
[Header("Ground Detection")]
[SerializeField] private Transform groundCheck;
[SerializeField] private LayerMask groundLayer;
@ -21,6 +23,8 @@ public class EntityStats : MonoBehaviour
public List<Ability> abilities = new();
[Header("Cache")]
public Rigidbody2D rb;
public SpriteRenderer sprite;
public bool isFacingRight;
public void TakeDamage(float damage)
{
health -= damage;

View file

@ -16,11 +16,15 @@ public class Player : Entity
private void FixedUpdate()
{
Vector2 movement = new Vector2(Input.GetAxis("Horizontal") * stats.speed, stats.rb.linearVelocityY);
if (Input.GetKeyDown(KeyCode.Space) && stats.OnGround())
if (!stats.isStalled)
{
movement.y = stats.jumpPower;
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;
}
stats.rb.linearVelocity = movement;
}
}

View file

@ -1,38 +1,49 @@
using System;
using Unity.Cinemachine;
using UnityEngine;
public class PlayerSwitcher : MonoBehaviour
{
[SerializeField] private CinemachineCamera cam;
[Header("Players")]
[SerializeField] private Player player1;
[SerializeField] private AutomatedPlayer player1AI;
[SerializeField] private Player player2;
[SerializeField] private AutomatedPlayer player2AI;
[Header("Switch Cooldowns")]
[SerializeField] private float switchCooldown;
private float currentSwitchCooldown;
private void Update()
{
if(Input.GetKeyDown(KeyCode.E))
if (Input.GetKeyDown(KeyCode.E) && currentSwitchCooldown <= 0f)
{
SwitchPlayers();
if (player1.isActiveAndEnabled)
{
SwitchPlayers(player1, player1AI, player2, player2AI);
}
else
{
SwitchPlayers(player2, player2AI, player1, player1AI);
}
currentSwitchCooldown = switchCooldown;
}
if (currentSwitchCooldown > 0f)
{
currentSwitchCooldown -= Time.deltaTime;
}
}
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
public void SwitchPlayers(Player playerA, AutomatedPlayer playerAAI, Player playerB, AutomatedPlayer playerBAI)
{
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);
}
cam.Follow = playerB.transform;
playerAAI.enabled = true;
playerA.enabled = false;
playerB.enabled = true;
playerBAI.enabled = false;
(playerB.transform.position, playerAAI.transform.position) = (playerAAI.transform.position, playerB.transform.position);
}
}