Advertisement
Ltven0mI

vector.lua

Mar 30th, 2023 (edited)
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.81 KB | None | 0 0
  1. -- Vector
  2.  
  3. local _prototype = {x=0, y=0, z=0}
  4.  
  5. function _prototype:tostring()
  6.   local pattern = "Vector: (x=%.2f, y=%.2f, z=%.2f)"
  7.   return string.format(pattern, self.x, self.y, self.z)
  8. end
  9.  
  10. function _prototype:equal(other)
  11.   return (self.x == other.x and
  12.     self.y == other.y and
  13.     self.z == other.z)
  14. end
  15.  
  16. function _prototype:add(other)
  17.   return new(
  18.     self.x + other.x,
  19.     self.y + other.y,
  20.     self.z + other.z)
  21. end
  22.  
  23. function _prototype:sub(other)
  24.   return new(
  25.     self.x - other.x,
  26.     self.y - other.y,
  27.     self.z - other.z)
  28. end
  29.  
  30. local _mt = {
  31.   __index = _prototype,
  32.   __tostring = _prototype.tostring,
  33.   __eq = _prototype.equal,
  34.   __add = _prototype.add,
  35.   __sub = _prototype.sub
  36. }
  37.  
  38. function new(x, y, z)
  39.   local o = {x=x, y=y, z=z}
  40.   setmetatable(o, _mt)
  41.   return o
  42. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement