Skip to content

Instantly share code, notes, and snippets.

@dphfox
Created April 30, 2022 20:50
Show Gist options
  • Save dphfox/99f5ff494395f59e811d255a36166f01 to your computer and use it in GitHub Desktop.
Save dphfox/99f5ff494395f59e811d255a36166f01 to your computer and use it in GitHub Desktop.
A better enum for working with directions and faces in Roblox.
--!strict
--[[
Facing
A better enum for working with directions and faces.
-----
© Elttob 2022 - Available under MIT license
]]
type Facing = {
normalID: Enum.NormalId,
facingID: number,
direction: Vector3,
axis: Vector3,
opposite: Facing
}
local fromNormalID: {[Enum.NormalId]: Facing} = {}
local fromFacingID: {[number]: Facing} = {}
local allFacings: {Facing} = {}
local function newFacing(normalID: Enum.NormalId, facingID: number): Facing
local self = {}
self.normalID = normalID
self.facingID = facingID
local direction = Vector3.FromNormalId(normalID)
self.direction = direction
self.axis = Vector3.new(math.abs(direction.X), math.abs(direction.Y), math.abs(direction.Z))
self.opposite = self -- we'll remap this later
fromNormalID[normalID] = self
fromFacingID[facingID] = self
table.insert(allFacings, self)
return self
end
local Facing = {}
Facing.fromNormalID = fromNormalID
Facing.fromFacingID = fromFacingID
Facing.allFacings = allFacings
Facing.TOP = newFacing(Enum.NormalId.Top, 1)
Facing.BOTTOM = newFacing(Enum.NormalId.Bottom, 2)
Facing.FRONT = newFacing(Enum.NormalId.Front, 4)
Facing.BACK = newFacing(Enum.NormalId.Back, 8)
Facing.LEFT = newFacing(Enum.NormalId.Left, 16)
Facing.RIGHT = newFacing(Enum.NormalId.Right, 32)
-- aliases
Facing.UP = Facing.TOP
Facing.DOWN = Facing.BOTTOM
Facing.X_POSITIVE = Facing.RIGHT
Facing.X_NEGATIVE = Facing.LEFT
Facing.Y_POSITIVE = Facing.UP
Facing.Y_NEGATIVE = Facing.DOWN
Facing.Z_POSITIVE = Facing.BACK
Facing.Z_NEGATIVE = Facing.FRONT
Facing.X_AXIS = Facing.X_POSITIVE
Facing.Y_AXIS = Facing.Y_POSITIVE
Facing.Z_AXIS = Facing.Z_POSITIVE
-- map opposites
Facing.TOP.opposite = Facing.BOTTOM
Facing.BOTTOM.opposite = Facing.TOP
Facing.FRONT.opposite = Facing.BACK
Facing.BACK.opposite = Facing.FRONT
Facing.LEFT.opposite = Facing.RIGHT
Facing.RIGHT.opposite = Facing.LEFT
return Facing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment