From a644a57f63d8aaf07a383ba3a113e05b721e33f8 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Mon, 20 Oct 2014 14:16:25 +0300 Subject: [PATCH] client/api: buildat.safe.Vector2 --- client/api.lua | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/client/api.lua b/client/api.lua index 845c992..2f20ad1 100644 --- a/client/api.lua +++ b/client/api.lua @@ -252,4 +252,48 @@ function buildat.safe.Vector3(x, y, z) return self end +local Vector2_prototype = { + x = 0, + y = 0, + mul_components = function(a, b) + return buildat.safe.Vector2( + a.x * b.x, a.y * b.y) + end, + div_components = function(a, b) + return buildat.safe.Vector2( + a.x / b.x, a.y / b.y) + end, + floor = function(a) + return buildat.safe.Vector2( + math.floor(a.x), math.floor(a.y)) + end, + add = function(a, b) + return buildat.safe.Vector2( + a.x + b.x, a.y + b.y) + end, + sub = function(a, b) + return buildat.safe.Vector2( + a.x - b.x, a.y - b.y) + end, + length = function(a) + return math.sqrt(a.x*a.x + a.y*a.y) + end, +} +function buildat.safe.Vector2(x, y) + local self = {} + if x ~= nil and y == nil then + self.x = x.x + self.y = x.y + else + self.x = x + self.y = y + end + setmetatable(self, { + __index = Vector2_prototype, + __add = Vector2_prototype.add, + __sub = Vector2_prototype.sub, + }) + return self +end + -- vim: set noet ts=4 sw=4: