106 lines
2.0 KiB
GDScript
106 lines
2.0 KiB
GDScript
class_name Direction
|
|
extends Object
|
|
|
|
# Direction enum, used to refer to the cell edges
|
|
enum { NORTH = 0, SOUTH = 1, EAST = 2, WEST = 3 }
|
|
|
|
# Get the opposite direction
|
|
static func opposite(d):
|
|
match d:
|
|
NORTH:
|
|
return SOUTH
|
|
SOUTH:
|
|
return NORTH
|
|
EAST:
|
|
return WEST
|
|
WEST:
|
|
return EAST
|
|
|
|
# Get the offset (X)
|
|
static func offset_x(d):
|
|
match d:
|
|
NORTH:
|
|
return 0
|
|
SOUTH:
|
|
return 0
|
|
EAST:
|
|
return 1
|
|
WEST:
|
|
return -1
|
|
|
|
# Get the offset (Y 2D, Z 3D)
|
|
static func offset_y(d):
|
|
match d:
|
|
NORTH:
|
|
return -1
|
|
SOUTH:
|
|
return 1
|
|
EAST:
|
|
return 0
|
|
WEST:
|
|
return 0
|
|
|
|
# Get the offset as a vector
|
|
static func offset(d):
|
|
return Vector2(offset_x(d), offset_y(d))
|
|
|
|
|
|
# Edge offset (from), as unit vectors
|
|
static func edge_from(d):
|
|
match d:
|
|
NORTH:
|
|
return Vector2(0, 0)
|
|
SOUTH:
|
|
return Vector2(0, 1)
|
|
EAST:
|
|
return Vector2(1, 0)
|
|
WEST:
|
|
return Vector2(0, 0)
|
|
|
|
static func edge_to(d):
|
|
match d:
|
|
NORTH:
|
|
return Vector2(1, 0)
|
|
SOUTH:
|
|
return Vector2(1, 1)
|
|
EAST:
|
|
return Vector2(1, 1)
|
|
WEST:
|
|
return Vector2(0, 1)
|
|
|
|
# Rotate left
|
|
static func rotate_left(d):
|
|
match d:
|
|
NORTH:
|
|
return WEST
|
|
SOUTH:
|
|
return EAST
|
|
EAST:
|
|
return NORTH
|
|
WEST:
|
|
return SOUTH
|
|
|
|
# Rotate right
|
|
static func rotate_right(d):
|
|
match d:
|
|
NORTH:
|
|
return EAST
|
|
SOUTH:
|
|
return WEST
|
|
EAST:
|
|
return SOUTH
|
|
WEST:
|
|
return NORTH
|
|
|
|
# To string
|
|
static func string_of(d):
|
|
match d:
|
|
NORTH:
|
|
return "NORTH"
|
|
SOUTH:
|
|
return "SOUTH"
|
|
EAST:
|
|
return "EAST"
|
|
WEST:
|
|
return "WEST"
|