summaryrefslogtreecommitdiff
path: root/examples/resources/lib/vector2.lua
blob: afb5fda0126ba66e4032c4dcbc7b6782c02bb434 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
-- For luaJit compatibility.
if table.unpack == nil then
	table.unpack = unpack
end

Vector2 = {}
Vector2.meta = {
	__index = Vector2,
	__tostring = function( v )
		return "{"..tostring( v.x )..", "..tostring( v.y ).."}"
	end,
	__add = function( v1, v2 )
		return Vector2:new( v1.x + v2.x, v1.y + v2.y )
	end,
	__sub = function( v1, v2 )
		return Vector2:new( v1.x - v2.x, v1.y - v2.y )
	end,
	__mul = function( v1, v2 )
		return Vector2:new( v1.x * v2.x, v1.y * v2.y )
	end,
	__div = function( v1, v2 )
		return Vector2:new( v1.x / v2.x, v1.y / v2.y )
	end,
	__mod = function( v, value )
		return Vector2:new( math.fmod( v.x, value ), math.fmod( v.y, value ) )
	end,
	__pow = function( v, value )
		return Vector2:new( v.x ^ value, v.y ^ value )
	end,
	__unm = function( v )
		return Vector2:new( -v.x, -v.y )
	end,
	__len = function()
		return 2
	end,
	__eq = function( v1, v2 )
		return RL.Vector2Equals( v1, v2 ) == 1
	end,
}

function Vector2:new( x, y )
	if type( x ) == "table" then
		x, y = table.unpack( x )
	elseif type( x ) == "nil" then
		x, y = 0, 0
	end

	local object = setmetatable( {}, Vector2.meta )

	object.x = x
	object.y = y

	return object
end

function Vector2:set( x, y )
	if type( x ) == "table" then
		x, y = table.unpack( x )
	end

	self.x = x
	self.y = y
end

function Vector2:arr()
	return { self.x, self.y }
end

function Vector2:unpack()
	return self.x, self.y
end

function Vector2:clone()
	return Vector2:new( self.x, self.y )
end

function Vector2:abs()
	return Vector2:new( math.abs( self.x ), math.abs( self.y ) )
end

function Vector2:min( v2 )
	return Vector2:new( math.min( self.x, v2.x ), math.min( self.y, v2.y ) )
end

function Vector2:max( v2 )
	return Vector2:new( math.max( self.x, v2.x ), math.max( self.y, v2.y ) )
end

function Vector2:floor()
	return Vector2:new( math.floor( self.x ), math.floor( self.y ) )
end

function Vector2:ceil()
	return Vector2:new( math.ceil( self.x ), math.ceil( self.y ) )
end

function Vector2:addValue( value )
	return Vector2:new( RL.Vector2AddValue( self, value ) )
end

function Vector2:subValue( value )
	return Vector2:new( RL.Vector2SubtractValue( self, value ) )
end

function Vector2:length()
	return RL.Vector2Length( self )
end

function Vector2:lengthSqr()
	return RL.Vector2LengthSqr( self )
end

function Vector2:dot( v2 )
	return RL.Vector2DotProduct( self, v2 )
end

function Vector2:distance( v2 )
	return RL.Vector2Distance( self, v2 )
end

function Vector2:distanceSqr( v2 )
	return RL.Vector2DistanceSqr( self, v2 )
end

function Vector2:angle( v2 )
	return RL.Vector2Angle( self, v2 )
end

function Vector2:lineAngle( v2 )
	return RL.Vector2LineAngle( self, v2 )
end

function Vector2:atan2()
	return math.atan( self.y, self.x )
end

function Vector2:scale( scale )
	return Vector2:new( RL.Vector2Scale( self, scale ) )
end

function Vector2:normalize()
	return Vector2:new( RL.Vector2Normalize( self ) )
end

function Vector2:transform( mat )
	return Vector2:new( RL.Vector2Transform( self, mat ) )
end

function Vector2:lerp( v2, value )
	return Vector2:new( RL.Vector2Lerp( self, v2, value ) )
end

function Vector2:reflect( normal )
	return Vector2:new( RL.Vector2Reflect( self, normal ) )
end

function Vector2:rotate( angle )
	return Vector2:new( RL.Vector2Rotate( self, angle ) )
end

function Vector2:moveTowards( target, maxDistance )
	return Vector2:new( RL.Vector2MoveTowards( self, target, maxDistance ) )
end

function Vector2:invert()
	return Vector2:new( RL.Vector2Invert( self ) )
end

function Vector2:clamp( min, max )
	return Vector2:new( RL.Vector2Clamp( self, min, max ) )
end

function Vector2:clampValue( min, max )
	return Vector2:new( RL.Vector2ClampValue( self, min, max ) )
end

function Vector2:equals( v2 )
	return RL.Vector2Equals( self, v2 )
end

return Vector2