mad1231999

Untitled

Feb 9th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.57 KB | None | 0 0
  1. local vec3 = {
  2.     x = 0,
  3.     y = 0,
  4.     z = 0,
  5.     w = "Hello, metatables!"
  6. }
  7. vec3.__index = vec3
  8. vec3.__call = function(self, x, y, z)
  9.     local t = { x = x, y = y,  z = z }
  10.     setmetatable(t, vec3)
  11.     return t
  12. end
  13.  
  14. vec3.__add = function(o1, o2)
  15.     local v = vec3(o1.x + o2.x, o1.y + o2.y, o1.z + o2.z)
  16.     return v
  17. end
  18.  
  19. function vec3:print()
  20.   print("(" .. self.x .. ", " .. self.y ..", " .. self.z .. ")")
  21. end
  22. setmetatable(vec3, vec3)
  23.  
  24. local v = vec3(12, 4.5, 3)
  25. local v2 = vec3(4, 1, 5)
  26.  
  27. v:print()
  28. v2:print()  
  29. local v3 = v + v2
  30. v3:print()
Advertisement
Add Comment
Please, Sign In to add comment