-
Notifications
You must be signed in to change notification settings - Fork 1
Rec.lua
Rec.lua is a rectangle class library which functions very similarly to Vec.lua, and in fact requires Vec.lua to work. Upon load, it will attempt to load Vec from multiple sources, or use it if there's already a global _VECTOR variable defined. Rec.lua is loaded the same as Vec.lua
The constructor is also similar:
local rec = Rec(0,0,10,10)This creates an axis-aligned rectangle object with its top left corner (bottom left if you're not using inverted y coords) at 0,0 and a width and height of 10 and 10.
These components can be accessed as rect.x, rect.y, rect.w, and rect.h respectively.
Rectangle objects support the following properties, which can be both read and written, having operations for both:
rec.typereturns "rectangle" (set)rec.lthe left side of the rectangle - returns x, sets xrec.rthe right side of the rectangle - returns x + w, sets x (with offset of w)rec.tthe top side of the rectangle (in inv y) - returns y, sets yrec.bthe bottom side of the rectangle (in inv y) - returns y + h, sets y (with offset of h)rec.mxthe middle x coordinate of the rectangle - returns x + w/2, sets x (with offset of w/2)rec.mythe middle y coordinate of the rectangle - returns y + h/2, sets y (with offset of h/2)rec.posthe top left vector of the rectangle (in inv y)rec.pos1the top left vector of the rectangle (in inv y)rec.pos2the top right vector of the rectangle (in inv y)rec.pos3the bottom right vector of the rectangle (in inv y)rec.pos4the bottom left vector of the rectangle (in inv y)rec.pos5the middle vector of the rectanglerec.dimsthe dimensions of the rectangle in vector form
Note: the 'pos' components should support slope directions, though it will result in some duplicates. For non duplicate positions, see sPos The following methods are, of course, read-only:
rec:intersect(rec2)AABB true/false intersection checkrec:relate(rec2)AABB distances - all negative for intersection, returns: left, right, up, downrec:fullIntersect(rec2)returns true/false intersection and results from relaterec:intersection(rec2)should return a rectangle representing the intersected area between the two rectangles.rec:expelDir(rec2)returns direction for AABB expulsion - 1,2,3 or 4 for left, right, up, and down respectively.rec:expel(rec2)pushes rec out of rec2 in basic AABB manner (nearest side expulsion)rec:fit(rec2, true)trims rec to fit inside rec2 - if second arg is true, will return a copy of rec rather than change the originalrec:copy(dx,dy,dw,dh,mod)copies rec. Will apply deltas if given, will copy unofficial values intomodif given a table, and will use themodtable rather than create or recycle a new one.rec:multiply(val)multiplies all values in rec by val, returns a new rectangle with the result.for v in rec:iter(rec2)iterates rec2 through rec in "stamp" fashion. Developed for use in grid/list-style menus. V is the rectangle representing the current space.rec:sPos(i)returns corner vectoriwith slope support - i being an integer from 1 to 3rec:sPosList()returns a list of the corner vectors with slope supportrec:aPos(i)returns the corner vectoriwith slope and basic supportrec:corner(i)returns theith corner of the rectangle without slope support.rec:corners()returns a list of the corners of the rectangle without slope support.rec:SATIntersect(other)returns whether or not the two rectangles are intersecting using the Separating Axis Theorem.rec:SATNearest(other,getDelta,getImpact)returns basic information for SAT physics between slopes: isIntersecting, nearestSideIndex, nearestPointIndex, nearestDistance, Delta/Impact, Impact/nil (the last two dependant on the given arguments)rec:SATExpel(other,getDelta)expels rec from other with slope and basic support. If getDelta is true, returns delta of SATNearest.rec:regressB(vec)regresses the given position - returns the integer index of the position on that row.rec:regress(rec2,vec)regresses the given position, using the rectangle to define cell size.rec:unpack()returns the top left x, top left y, width, and height of the box.
rec:del()recycles rec in similar fashion to the Vec library
The library is designed to natively support basic slopes for tiles, considering the component rec.dir - an enum with the four possible states of "tl", "tr", "br", and "bl" - to be generally reserved and reset after recycling. However, it cannot be set in the constructor without a properties table. An example of both options:
local A = rec(10,20,30,40,{dir = "tl"}) -- A top-left facing slope.
local B = rec(10,20,30,40) -- A basic rectangle...
B.dir = "tl" -- ...with a top-left facing slope.Both are roughly equivalent, though the first form will create a new table for every slope, while the second will attempt to make use of the recycling cache.
Rec.lua can also integrate Line.lua for some added features. It will automatically require/load Line if available; however, it is also possible to manually load Line.lua later:
rec.loadLine(_LINE)the library object or any returned rectangle contain this function.
This unlocks the following function:
rec.slopereturns a line representation of a slope, or nil if it is not a slope.