riversong code showcase
This commit is contained in:
134
Source/Riversong/Game/CommonServices/TileMath/Directions.cs
Normal file
134
Source/Riversong/Game/CommonServices/TileMath/Directions.cs
Normal file
@@ -0,0 +1,134 @@
|
||||
using System;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public enum Directions
|
||||
{
|
||||
North,
|
||||
|
||||
NorthWest,
|
||||
|
||||
West,
|
||||
|
||||
SouthWest,
|
||||
|
||||
South,
|
||||
|
||||
SouthEast,
|
||||
|
||||
East,
|
||||
|
||||
NorthEast
|
||||
}
|
||||
|
||||
public enum DirectionsMask4
|
||||
{
|
||||
None = 0,
|
||||
|
||||
North = 1 << 0,
|
||||
|
||||
West = 1 << 1,
|
||||
|
||||
South = 1 << 2,
|
||||
|
||||
East = 1 << 3
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum DirectionsMask8
|
||||
{
|
||||
None = 0,
|
||||
|
||||
North = 1 << 0,
|
||||
|
||||
NorthWest = 1 << 1,
|
||||
|
||||
West = 1 << 2,
|
||||
|
||||
SouthWest = 1 << 3,
|
||||
|
||||
South = 1 << 4,
|
||||
|
||||
SouthEast = 1 << 5,
|
||||
|
||||
East = 1 << 6,
|
||||
|
||||
NorthEast = 1 << 7
|
||||
}
|
||||
|
||||
public static class DirectionsExtensions
|
||||
{
|
||||
public static int2 ToVector(this Directions direction)
|
||||
{
|
||||
switch (direction)
|
||||
{
|
||||
case Directions.North:
|
||||
return new int2(0, 1);
|
||||
|
||||
case Directions.NorthWest:
|
||||
return new int2(-1, 1);
|
||||
|
||||
case Directions.West:
|
||||
return new int2(-1, 0);
|
||||
|
||||
case Directions.SouthWest:
|
||||
return new int2(-1, -1);
|
||||
|
||||
case Directions.South:
|
||||
return new int2(0, -1);
|
||||
|
||||
case Directions.SouthEast:
|
||||
return new int2(1, -1);
|
||||
|
||||
case Directions.East:
|
||||
return new int2(1, 0);
|
||||
|
||||
case Directions.NorthEast:
|
||||
return new int2(1, 1);
|
||||
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
public static Quaternion ToQuaternion(this Directions direction)
|
||||
{
|
||||
switch (direction)
|
||||
{
|
||||
case Directions.North:
|
||||
return Quaternion.identity;
|
||||
|
||||
case Directions.NorthWest:
|
||||
return Quaternion.LookRotation(new Vector3(-1, 0, 1));
|
||||
|
||||
case Directions.West:
|
||||
return Quaternion.LookRotation(Vector3.left);
|
||||
|
||||
case Directions.SouthWest:
|
||||
return Quaternion.LookRotation(new Vector3(-1, 0, -1));
|
||||
|
||||
case Directions.South:
|
||||
return Quaternion.LookRotation(Vector3.back);
|
||||
|
||||
case Directions.SouthEast:
|
||||
return Quaternion.LookRotation(new Vector3(1, 0, -1));
|
||||
|
||||
case Directions.East:
|
||||
return Quaternion.LookRotation(Vector3.right);
|
||||
|
||||
case Directions.NorthEast:
|
||||
return Quaternion.LookRotation(new Vector3(1, 0, 1));
|
||||
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
public static int2 Rotate(this Directions direction, int2 v)
|
||||
{
|
||||
return TileMath.Rotate(v, direction);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user