z_mirror/flib

git clone https://git.raiguard.me/z_mirror/flib

Caleb HeuerRemove require guardsa9f2be7

trunk
1.4 KiB42 linesraw
1--- Functions for working with orientations.
2--- ```lua
3--- local flib_orientation = require("__flib__.orientation")
4--- ```
5--- @class flib_orientation
6local flib_orientation = {}
7
8flib_orientation.north = defines.direction.north / 16
9flib_orientation.east = defines.direction.east / 16
10flib_orientation.west = defines.direction.west / 16
11flib_orientation.south = defines.direction.south / 16
12flib_orientation.northeast = defines.direction.northeast / 16
13flib_orientation.northwest = defines.direction.northwest / 16
14flib_orientation.southeast = defines.direction.southeast / 16
15flib_orientation.southwest = defines.direction.southwest / 16
16
17--- Returns a 4way or 8way direction from an orientation.
18--- @param orientation number
19--- @param eight_way boolean
20--- @return defines.direction
21function flib_orientation.to_direction(orientation, eight_way)
22  local ways = eight_way and 8 or 4
23  local mod = eight_way and 1 or 2
24  return math.floor(orientation * ways + 0.5) % ways * mod --[[@as defines.direction]]
25end
26
27--- Returns the opposite orientation.
28--- @param orientation number
29--- @return number
30function flib_orientation.opposite(orientation)
31  return (orientation + 0.5) % 1
32end
33
34--- Add two orientations together.
35--- @param orientation1 number
36--- @param orientation2 number
37--- @return number the orientations added together
38function flib_orientation.add(orientation1, orientation2)
39  return (orientation1 + orientation2) % 1
40end
41
42return flib_orientation