Project Restructure (PLEASE UPDATE AFTER COMMIT)

This commit is contained in:
RochesterX
2025-01-15 15:05:04 -05:00
parent 63775c5103
commit dc61c3c1d0
168 changed files with 82 additions and 10 deletions

View File

@@ -0,0 +1,193 @@
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
public string player;
[Header("Ground Layers")]
public LayerMask ground;
[Header("Movement")]
public float walkSpeed;
public float walkSpeedFactor = 1f;
public float maxSpeed = 5f;
public float virtualAxisX;
public float virtualButtonJump;
public float virtualButtonJumpLastFrame;
public float turnaroundMultiplier = 2;
public float walkSmooth;
public float secondsToFullSpeed;
public float jumpSpeed;
public float coyoteTime;
public float jumpLenience;
public float timeUnableToBeDeclaredNotJumping = 0.1f;
public float groundCheckDistance;
private Rigidbody2D body;
private BoxCollider2D collide;
private PlayerInput input;
private bool jumpInputStillValid = false;
private float lastTimeJumpPressed;
private bool canBeDeclaredNotJumping = true;
private bool jumpPhysics;
private bool jumping;
private float lastTimeOnGround;
private Vector3 positionLastFrame;
void Start()
{
GetComponent<RespawnOnTriggerEnter>().spawnPoint = transform.position;
body = GetComponent<Rigidbody2D>();
collide = GetComponent<BoxCollider2D>();
input = GetComponent<PlayerInput>();
}
private void Update()
{
Jump();
UpdateVirtualAxis();
}
private void FixedUpdate()
{
JumpPhysics();
HorizontalMovement();
Land();
}
private void Land()
{
if (body.linearVelocity.y >= 0f) return;
if (IsPhysicallyGrounded())
{
if (canBeDeclaredNotJumping)
{
jumping = false;
}
}
}
private void Jump()
{
if (virtualButtonJumpLastFrame == 1f)
{
jumpInputStillValid = true;
lastTimeJumpPressed = Time.time;
}
bool isBasicallyGrounded = IsBasicallyGrounded();
if ((virtualButtonJumpLastFrame == 1f && isBasicallyGrounded && jumping == false) // Coyote Jump: Must have jump pressed this frame and be grounded in last time frame and not be actually jumping.
|| (jumpInputStillValid && Time.time - lastTimeJumpPressed <= jumpLenience && IsPhysicallyGrounded())) // Buffered Jump: Must have pressed jump in the last time frame and be jumping
{
jumpPhysics = true;
jumping = true;
jumpInputStillValid = false;
StartCoroutine(NotJumpingDelay());
}
}
private void JumpPhysics()
{
if (jumpPhysics)
{
if (body.linearVelocity.y < 0 || !IsPhysicallyGrounded()) body.linearVelocity = new Vector2(body.linearVelocity.x, 0);
body.AddForce(Vector2.up * jumpSpeed, ForceMode2D.Impulse);
jumpPhysics = false;
}
if (!IsPhysicallyGrounded() && !(virtualButtonJump == 1f))
body.AddForce(Vector2.down * jumpSpeed);
}
private IEnumerator NotJumpingDelay()
{
canBeDeclaredNotJumping = false;
yield return new WaitUntil(() => !IsBasicallyGrounded());
canBeDeclaredNotJumping = true;
}
private void HorizontalMovement()
{
//body.linearVelocity = new Vector2(virtualAxisX * walkSpeed, body.linearVelocity.y);
body.AddForce(new Vector2(virtualAxisX * walkSpeed * walkSpeedFactor, 0), ForceMode2D.Force);
if (Mathf.Abs(body.linearVelocityX) >= maxSpeed)
{
body.linearVelocity = new Vector2(Mathf.Sign(body.linearVelocityX) * maxSpeed, body.linearVelocity.y);
}
//if (!IsPhysicallyGrounded())
//{
body.linearVelocityX *= walkSmooth;
//}
if (transform.position == positionLastFrame && (input.actions.FindAction("Move").ReadValue<Vector2>().x == 0))
{
virtualAxisX = 0;
}
positionLastFrame = transform.position;
}
private void UpdateVirtualAxis()
{
virtualButtonJump = input.actions.FindAction("Action").ReadValue<float>();
virtualButtonJumpLastFrame = input.actions.FindAction("Action").WasPressedThisFrame() ? 1 : 0;
virtualAxisX = input.actions.FindAction("Move").ReadValue<Vector2>().x;
return;
}
public bool IsBasicallyGrounded()
{
if (IsPhysicallyGrounded())
{
lastTimeOnGround = Time.time;
}
if (Time.time - lastTimeOnGround < coyoteTime)
{
return true;
}
return false;
}
public bool IsPhysicallyGrounded()
{
RaycastHit2D leftCheck = Physics2D.Raycast(GetPointInBoxCollider(collide, -1, -1), Vector2.down, groundCheckDistance, ground);
RaycastHit2D rightCheck = Physics2D.Raycast(GetPointInBoxCollider(collide, 1, -1), Vector2.down, groundCheckDistance, ground);
RaycastHit2D midCheck = Physics2D.Raycast(GetPointInBoxCollider(collide, 0, -1), Vector2.down, groundCheckDistance, ground);
if (leftCheck || rightCheck || midCheck)
{
return true;
}
return false;
}
public Vector2 GetPointInBoxCollider(BoxCollider2D boxCollider2D, float horizontal, float vertical)
{
return new Vector2
(
boxCollider2D.bounds.center.x + (horizontal * boxCollider2D.bounds.extents.x),
boxCollider2D.bounds.center.y + (vertical * boxCollider2D.bounds.extents.y)
);
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 9b920f65db6650d4aa4858ad8a795d19

View File

@@ -0,0 +1,33 @@
using System.Collections.Generic;
using UnityEngine;
public class PlayerCameraMovement : MonoBehaviour
{
private Vector3 start;
private Vector3 target;
public float weight;
public float speed;
private void Start()
{
start = transform.position;
}
private void Update()
{
List<GameObject> players = PlayerManager.Instance.players;
if (players.Count == 0) return;
Vector3 playerAverage = Vector3.zero;
foreach (GameObject player in players)
{
playerAverage += player.transform.position;
}
playerAverage /= players.Count;
target = start * weight + playerAverage * (1 - weight);
transform.position = Vector3.Lerp(transform.position, target, speed * Time.deltaTime);
transform.position = new Vector3(transform.position.x, transform.position.y, -10);
}
}

View File

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

View File

@@ -0,0 +1,52 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerManager : MonoBehaviour
{
public static PlayerManager Instance;
public List<GameObject> players;
[SerializeField] private Vector2 spawnPosition;
private PlayerCameraMovement playerCamera;
private void Awake()
{
Init();
}
private void Start()
{
GetComponent<PlayerInputManager>().onPlayerJoined += OnPlayerJoined;
GetComponent<PlayerInputManager>().onPlayerLeft += OnPlayerLeft;
playerCamera = FindFirstObjectByType<PlayerCameraMovement>();
}
private void OnPlayerJoined(PlayerInput playerInput)
{
playerInput.transform.position = spawnPosition;
players.Add(playerInput.gameObject);
print("Player joined");
}
private void OnPlayerLeft(PlayerInput playerInput)
{
Destroy(playerInput.gameObject);
players.Remove(playerInput.gameObject);
print("Player left");
}
private void Init()
{
if (Instance == null)
{
Instance = this;
}
else
{
print("A PlayerManager already exists.");
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 33e0fad1e452e0140bc99e780d4dda4f

View File

@@ -0,0 +1,28 @@
using UnityEngine;
public class RespawnOnTriggerEnter : MonoBehaviour
{
public Vector2 spawnPoint;
public bool spawnPointIsInitialPosition = false;
public string respawnTag;
private void Start()
{
if (spawnPointIsInitialPosition)
{
spawnPoint = transform.position;
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag(respawnTag))
{
transform.position = spawnPoint;
if (TryGetComponent<Rigidbody2D>(out var rb))
{
rb.linearVelocity = Vector2.zero;
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 8ce1d588594ee416e9ab629d0b8c07dd