pgimeno wrote:Sometimes it may be convenient to swap them. In particular, when you use an angle convention where north is 0° and angle increases clockwise
Sure you can do 90 degree rotation or mirror the vector by flipping the x/y values. Note that in this case we're talking about game logic/math - so I would advise with sticking with how atan2 is "supposed" to be used... the reason behind it is that we don't want the game logic dependent on Love2D's rendering projection.
childonline wrote:great feedback, thanks! i will definitely start using strict.lua more often. Could you please be more specific regarding what i'm doing wrong with the metatables/self-references (examples of best practices /bad practices would be most helpful)
Thanks, I think that you don't need metatables at all.
For example, vrld's Vec light gets the job done without metatables:
http://hump.readthedocs.io/en/latest/vector-light.html
Metatables are only really necessary if you want to have inheritance or you're doing something complicated.
I like to plug my own OO wrapper, as it shows verbosely how inheritance works using metatables:
Code: Select all
local oo = {}
local meta = {}
local rmeta = {}
-- Creates a new class
function oo.class()
local c = {}
local mt = { __index = c }
meta[c] = mt
rmeta[mt] = c
return c
end
-- Creates a subclass
function oo.subclass(p)
assert(meta[p], "undefined parent class")
local c = oo.class()
return setmetatable(c, meta[p])
end
-- Creates a new instance
function oo.instance(c)
assert(meta[c], "undefined class")
return setmetatable({}, meta[c])
end
-- Gets the class of an instance
function oo.getclass(i)
local mt = getmetatable(i)
return rmeta[mt]
end
Example:
Code: Select all
-- Inheritance:
local oo = require 'oo'
local Fruit = oo.class()
Fruit.sweetness_threshold = 5 -- sort of like "static"
function Fruit:initialize(sweetness)
self.sweetness = sweetness
end
function Fruit:isSweet()
return self.sweetness > Fruit.sweetness_threshold
end
local Lemon = oo.subclass(Fruit) -- subclassing
function Lemon:initialize()
Fruit.initialize(self, 1) -- manually invoking the superclass' initializer
end
local lemon = oo.instance(Lemon)
lemon:initialize()
print(lemon:isSweet()) -- false
Again, it's important to emphasize that there is no "standard" way of OO programming in Lua.
There are more sophisticated OO libraries out there with "mixin" support, yet 99% of the time you shouldn't need a library at all.
Shiffman strikes again!
At least he mentioned Reynolds in that chapter.