Files

242 lines
10 KiB
C#
Raw Permalink Normal View History

using UnityEngine;
public class DemonicsPhysics : MonoBehaviour
{
2022-11-15 21:39:40 +02:00
[SerializeField] private bool _ignoreWalls = default;
2023-07-02 23:17:37 +03:00
public DemonVector2 Velocity { get; set; }
public DemonVector2 Position { get; set; }
2022-11-06 13:31:42 +02:00
public bool OnGround { get { return Position.y <= GROUND_POINT ? true : false; } private set { } }
2022-11-10 20:50:13 +02:00
public bool OnWall { get { return Position.x >= WALL_RIGHT_POINT || Position.x <= WALL_LEFT_POINT ? true : false; } private set { } }
2023-07-02 23:17:37 +03:00
private DemonVector2 _freezePosition;
2022-11-05 16:08:03 +02:00
private bool _freeze;
2023-10-15 17:05:57 +03:00
public static DemonFloat FOOT_POINT = (DemonFloat)(-80);
2023-07-02 23:17:37 +03:00
public static DemonFloat GROUND_POINT = (DemonFloat)(-72);
2023-10-15 17:05:57 +03:00
public static DemonFloat CELLING_POINT = (DemonFloat)120;
2023-07-02 23:17:37 +03:00
public static DemonFloat WALL_RIGHT_POINT;
public static DemonFloat WALL_LEFT_POINT;
public static DemonFloat GRAVITY = (DemonFloat)0.235f;
2023-07-02 23:17:37 +03:00
public static DemonFloat JUGGLE_GRAVITY = (DemonFloat)0.208f;
2022-11-10 19:08:34 +02:00
public DemonicsPhysics OtherPhysics { get; set; }
2022-11-16 19:07:10 +02:00
public bool IgnoreWalls { get { return _ignoreWalls; } set { _ignoreWalls = value; } }
public void OnCollision(DemonicsPhysics otherPhysics)
{
2022-11-10 19:08:34 +02:00
this.OtherPhysics = otherPhysics;
}
2022-11-05 16:08:03 +02:00
public void SetFreeze(bool state)
{
_freeze = state;
if (_freeze)
{
_freezePosition = Position;
}
}
2022-11-09 23:33:00 +02:00
void FixedUpdate()
{
2022-11-05 16:08:03 +02:00
if (_freeze)
{
Position = _freezePosition;
return;
}
2022-11-09 23:33:00 +02:00
}
2022-12-22 01:39:39 +02:00
public static bool Collision(PlayerNetwork player, PlayerNetwork otherPlayer)
{
2023-01-10 18:39:07 +02:00
if (!player.pushbox.active || !otherPlayer.pushbox.active)
{
return false;
}
2022-12-22 01:39:39 +02:00
if (Colliding(player, otherPlayer))
{
if (player.position.y > otherPlayer.position.y)
{
if (player.velocity.y < otherPlayer.velocity.y)
{
2023-07-02 23:17:37 +03:00
DemonFloat difference = DemonFloat.Abs(player.position.x - otherPlayer.position.x);
DemonFloat pushDistance = (player.pushbox.size.x - difference) / (2);
2022-12-31 20:46:52 +02:00
if (player.position.x <= DemonicsPhysics.WALL_LEFT_POINT)
2022-12-22 01:39:39 +02:00
{
2023-07-02 23:17:37 +03:00
player.position = new DemonVector2(player.position.x + pushDistance, player.position.y);
2022-12-22 01:39:39 +02:00
}
2022-12-31 20:46:52 +02:00
else if (player.position.x >= DemonicsPhysics.WALL_RIGHT_POINT)
2022-12-22 01:39:39 +02:00
{
2023-07-02 23:17:37 +03:00
player.position = new DemonVector2(player.position.x - pushDistance, player.position.y);
2022-12-22 01:39:39 +02:00
}
}
}
2023-07-02 23:17:37 +03:00
DemonVector2 main = player.velocity;
DemonVector2 second = otherPlayer.velocity;
if (otherPlayer.position.x >= DemonicsPhysics.WALL_RIGHT_POINT && player.velocity.x >= (DemonFloat)0 || otherPlayer.position.x <= DemonicsPhysics.WALL_LEFT_POINT && player.velocity.x <= (DemonFloat)0)
2022-12-22 01:39:39 +02:00
{
2023-07-02 23:17:37 +03:00
main = new DemonVector2((DemonFloat)0, player.velocity.y);
second = new DemonVector2((DemonFloat)0, otherPlayer.velocity.y);
otherPlayer.position = (new DemonVector2(otherPlayer.position.x + second.x, otherPlayer.position.y + second.y));
player.position = (new DemonVector2(player.position.x + main.x, player.position.y + main.y));
2022-12-22 01:39:39 +02:00
Intersects(player, otherPlayer);
return true;
}
2023-07-02 23:17:37 +03:00
if (DemonFloat.Abs(player.velocity.x) > DemonFloat.Abs(otherPlayer.velocity.x))
2022-12-22 01:39:39 +02:00
{
2023-07-02 23:17:37 +03:00
DemonFloat totalVelocity;
2022-12-22 01:39:39 +02:00
if (player.velocity.x > 0 && otherPlayer.velocity.x < 0)
{
2023-07-02 23:17:37 +03:00
totalVelocity = DemonFloat.Abs(player.velocity.x) - DemonFloat.Abs(otherPlayer.velocity.x);
2022-12-22 01:39:39 +02:00
}
else
{
2023-07-02 23:17:37 +03:00
totalVelocity = DemonFloat.Abs(player.velocity.x);
2022-12-22 01:39:39 +02:00
}
if (player.position.x < otherPlayer.position.x && player.velocity.x > 0)
{
2023-07-02 23:17:37 +03:00
main = new DemonVector2(totalVelocity, player.velocity.y);
second = new DemonVector2(totalVelocity, otherPlayer.velocity.y);
otherPlayer.position = (new DemonVector2(otherPlayer.position.x + second.x, otherPlayer.position.y + second.y));
player.position = (new DemonVector2(player.position.x + main.x, player.position.y + main.y));
2022-12-22 01:39:39 +02:00
Intersects(player, otherPlayer);
return true;
}
else if (player.position.x > otherPlayer.position.x && player.velocity.x < 0)
{
2023-07-02 23:17:37 +03:00
main = new DemonVector2(-totalVelocity, player.velocity.y);
second = new DemonVector2(-totalVelocity, otherPlayer.velocity.y);
otherPlayer.position = (new DemonVector2(otherPlayer.position.x + second.x, otherPlayer.position.y + second.y));
player.position = (new DemonVector2(player.position.x + main.x, player.position.y + main.y));
2022-12-22 01:39:39 +02:00
Intersects(player, otherPlayer);
return true;
}
2023-07-02 23:17:37 +03:00
if (player.velocity.x == (DemonFloat)0 || otherPlayer.velocity.x == (DemonFloat)0)
2022-12-22 01:39:39 +02:00
{
Intersects(player, otherPlayer);
}
return false;
}
2023-07-02 23:17:37 +03:00
else if (DemonFloat.Abs(player.velocity.x) == DemonFloat.Abs(otherPlayer.velocity.x))
2022-12-22 01:39:39 +02:00
{
if (player.position.x < otherPlayer.position.x && player.velocity.x > 0 || player.position.x > otherPlayer.position.x && player.velocity.x < 0)
{
2023-07-02 23:17:37 +03:00
main = new DemonVector2((DemonFloat)0, player.velocity.y);
second = new DemonVector2((DemonFloat)0, otherPlayer.velocity.y);
player.position = (new DemonVector2(player.position.x + main.x, player.position.y + main.y));
2022-12-22 01:39:39 +02:00
Intersects(player, otherPlayer);
return true;
}
Intersects(player, otherPlayer);
return false;
}
return true;
}
return false;
}
private static void Intersects(PlayerNetwork player, PlayerNetwork otherPlayer)
{
2022-12-31 20:46:52 +02:00
if (player.position.x <= DemonicsPhysics.WALL_LEFT_POINT || player.position.x >= DemonicsPhysics.WALL_RIGHT_POINT)
2022-12-22 01:39:39 +02:00
{
return;
}
if (player.position.x < otherPlayer.position.x)
{
2022-12-29 14:40:06 +02:00
if (player.position.x + player.pushbox.size.x >= otherPlayer.position.x)
2022-12-22 01:39:39 +02:00
{
2023-07-02 23:17:37 +03:00
DemonFloat difference = DemonFloat.Abs(player.position.x - otherPlayer.position.x);
2022-12-29 14:40:06 +02:00
int pushDistance = (int)(player.pushbox.size.x - difference) / (2);
2023-07-02 23:17:37 +03:00
player.position = new DemonVector2((player.position.x - pushDistance), player.position.y);
2022-12-22 01:39:39 +02:00
}
}
else
{
2022-12-29 14:40:06 +02:00
if (player.position.x <= otherPlayer.position.x + player.pushbox.size.x)
2022-12-22 01:39:39 +02:00
{
2023-07-02 23:17:37 +03:00
DemonFloat difference = DemonFloat.Abs(player.position.x - otherPlayer.position.x);
2022-12-29 14:40:06 +02:00
int pushDistance = (int)(player.pushbox.size.x - difference) / (2);
2023-07-02 23:17:37 +03:00
player.position = new DemonVector2((player.position.x + pushDistance), player.position.y);
2022-12-22 01:39:39 +02:00
}
}
}
2023-07-02 23:17:37 +03:00
private static bool valueInRange(DemonFloat value, DemonFloat min, DemonFloat max)
2022-12-22 01:39:39 +02:00
{ return (value >= min) && (value <= max); }
private static bool Colliding(PlayerNetwork a, PlayerNetwork b)
{
2022-12-29 14:40:06 +02:00
bool xOverlap = valueInRange(a.position.x - (a.pushbox.size.x / 2), b.position.x - (b.pushbox.size.x / 2), b.position.x + (b.pushbox.size.x / 2)) ||
valueInRange(b.position.x - (b.pushbox.size.x / 2), a.position.x - (a.pushbox.size.x / 2), a.position.x + (a.pushbox.size.x / 2));
bool yOverlap = valueInRange(a.position.y - (a.pushbox.size.y / 2), b.position.y - (b.pushbox.size.y / 2), b.position.y + (b.pushbox.size.y / 2)) ||
valueInRange(b.position.y - (b.pushbox.size.y / 2), a.position.y - (a.pushbox.size.y / 2), a.position.y + (a.pushbox.size.y / 2));
2022-12-22 01:39:39 +02:00
return xOverlap && yOverlap;
}
2023-01-01 23:08:39 +02:00
public static void CameraHorizontalBounds(PlayerNetwork player, PlayerNetwork otherPlayer)
2022-11-09 23:33:00 +02:00
{
2023-07-02 23:17:37 +03:00
DemonFloat distance = DemonFloat.Abs((DemonFloat)player.position.x - (DemonFloat)otherPlayer.position.x);
if (distance >= (DemonFloat)220)
2023-01-01 23:08:39 +02:00
{
if (player.position.x > otherPlayer.position.x)
{
2023-07-02 23:17:37 +03:00
WALL_LEFT_POINT = (DemonFloat)(otherPlayer.position.x);
WALL_RIGHT_POINT = (DemonFloat)(player.position.x);
2023-01-01 23:08:39 +02:00
}
else
{
2023-07-02 23:17:37 +03:00
WALL_LEFT_POINT = (DemonFloat)(player.position.x);
WALL_RIGHT_POINT = (DemonFloat)(otherPlayer.position.x);
2023-01-01 23:08:39 +02:00
}
}
else
{
2023-07-02 23:17:37 +03:00
WALL_LEFT_POINT = (DemonFloat)(-169);
WALL_RIGHT_POINT = (DemonFloat)(169);
2023-01-01 23:08:39 +02:00
}
}
2023-07-02 23:17:37 +03:00
public void SetPositionWithRender(DemonVector2 position)
2022-11-05 16:08:03 +02:00
{
Position = position;
transform.position = new Vector2((float)Position.x, (float)Position.y);
}
2023-07-02 23:17:37 +03:00
public static DemonVector2 Bounds(PlayerNetwork player)
2022-12-23 03:17:40 +02:00
{
2023-07-02 23:17:37 +03:00
DemonVector2 position = player.position;
2022-12-31 20:46:52 +02:00
if (player.position.y >= CELLING_POINT && player.velocity.y > 0)
2022-12-23 03:17:40 +02:00
{
2023-01-15 13:05:14 +02:00
//CHECK FOR CELLING
2022-12-23 03:17:40 +02:00
}
2023-07-02 23:17:37 +03:00
if (player.position.x >= WALL_RIGHT_POINT && player.velocity.x > (DemonFloat)0)
2022-12-23 03:17:40 +02:00
{
2023-01-01 23:08:39 +02:00
if (player.position.y <= GROUND_POINT)
{
2023-07-02 23:17:37 +03:00
position = new DemonVector2(WALL_RIGHT_POINT, GROUND_POINT);
2023-01-01 23:08:39 +02:00
}
else
{
2023-07-02 23:17:37 +03:00
position = new DemonVector2(WALL_RIGHT_POINT, player.position.y);
2023-01-01 23:08:39 +02:00
}
2022-12-23 03:17:40 +02:00
}
2023-07-02 23:17:37 +03:00
else if (player.position.x <= WALL_LEFT_POINT && player.velocity.x < (DemonFloat)0)
2022-12-23 03:17:40 +02:00
{
2023-01-01 23:08:39 +02:00
if (player.position.y <= GROUND_POINT)
{
2023-07-02 23:17:37 +03:00
position = new DemonVector2(WALL_LEFT_POINT, GROUND_POINT);
2023-01-01 23:08:39 +02:00
}
else
{
2023-07-02 23:17:37 +03:00
position = new DemonVector2(WALL_LEFT_POINT, player.position.y);
2023-01-01 23:08:39 +02:00
}
}
else
{
if (player.position.y <= GROUND_POINT)
{
2023-07-02 23:17:37 +03:00
position = new DemonVector2(player.position.x, GROUND_POINT);
2023-01-01 23:08:39 +02:00
}
2022-12-23 03:17:40 +02:00
}
2023-01-01 04:39:49 +02:00
return position;
2022-12-23 03:17:40 +02:00
}
2022-12-26 21:22:49 +02:00
public static bool IsInCorner(PlayerNetwork player)
{
2022-12-31 20:46:52 +02:00
if (player.position.x <= DemonicsPhysics.WALL_LEFT_POINT || player.position.x >= DemonicsPhysics.WALL_RIGHT_POINT)
2022-12-26 21:22:49 +02:00
{
return true;
}
return false;
}
}