64 lines
2.8 KiB
C#
64 lines
2.8 KiB
C#
namespace Marro.PacManUdon
|
|
{
|
|
using System;
|
|
using UnityEngine;
|
|
|
|
public static class GridMoverTools
|
|
{
|
|
public static Vector2 GetNextPosition(Vector2 currentPosition, Vector2 direction, float speed)
|
|
{
|
|
return currentPosition + direction * speed * Time.deltaTime;
|
|
}
|
|
|
|
public static Vector2 PositionToGrid(Vector2 position)
|
|
{
|
|
return new Vector2((float)Math.Round(position.x), (float)Math.Round(position.y));
|
|
}
|
|
|
|
public static bool CrossesTileCenter(Vector2 currentPosition, Vector2 nextPosition, bool horizontal, bool vertical)
|
|
{
|
|
bool result = false;
|
|
|
|
if (horizontal)
|
|
{
|
|
result = result || Math.Floor(currentPosition.x) != Math.Floor(nextPosition.x);
|
|
}
|
|
if (vertical)
|
|
{
|
|
result = result || Math.Floor(currentPosition.y) != Math.Floor(nextPosition.y);
|
|
}
|
|
|
|
// Debug.Log($"CrossesTileCenter at currentPosition {currentPosition} with nextPosition {nextPosition}, horizontal {horizontal} and vertical {vertical} gives {result}");
|
|
return result;
|
|
}
|
|
|
|
public static bool CrossesTileBorder(Vector2 currentPosition, Vector2 nextPosition, bool horizontal, bool vertical)
|
|
{
|
|
bool result = false;
|
|
|
|
if (horizontal)
|
|
{
|
|
result = result || nextPosition.x != currentPosition.x
|
|
&& ((Math.Floor(currentPosition.x) != Math.Floor(nextPosition.x) && nextPosition.x % 1 != 0)
|
|
|| currentPosition.x % 1 == 0);
|
|
}
|
|
if (vertical)
|
|
{
|
|
result = result || nextPosition.y != currentPosition.y
|
|
&& ((Math.Floor(currentPosition.y) != Math.Floor(nextPosition.y) && nextPosition.y % 1 != 0)
|
|
|| currentPosition.y % 1 == 0);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static bool CheckCollisionInDirection(Transform transform, Vector2 position, Vector2 direction)
|
|
{
|
|
// Debug.Log("Collision check");
|
|
bool result = Physics.Linecast(transform.parent.TransformPoint(position), transform.parent.TransformPoint(position + direction.normalized), 1 << 22, QueryTriggerInteraction.Ignore);
|
|
// Debug.DrawLine(transform.parent.TransformPoint(position), transform.parent.TransformPoint(position + direction.normalized), Color.red, 1);
|
|
// Debug.Log($"{gameObject} Collision; Position {position} became {transform.parent.TransformPoint(position)}, direction {direction.normalized} became {transform.parent.TransformDirection(direction.normalized)}");
|
|
// Debug.Log("collision check for " + position + " in direction " + direction + " returned " + result);
|
|
return result;
|
|
}
|
|
}
|
|
} |