Are there any neat ways to wrap the different collision scenarios into maintanable and extendable code? In C++, for instance, I would probably add a virtual method to a class. How can I achieve this using HC?
Basically what I want to do is identify e.g. what enemy is colliding with e.g. the wall, the ground etc. and call e.g. the collision method for that corresponding enemy. I am not sure how to go about this.
local function _collision(dt, a, b, dx, dy)
if a.collision ~= nil then
a:collision(b, dx, dy)
end
if b.collision ~= nil then
b:collision(a, dx, dy)
end
end
local function _collision(dt, a, b, dx, dy)
if a.collision ~= nil then
(a.collision[b.kind] or a.collision.default)(a, b, dx, dy)
end
if b.collision ~= nil then
(b.collision[a.kind] or b.collision.default)(b, a, dx, dy)
end
end
entity.collision = {}
function entity.collision:default(other, dx, dy)
print("Collision!")
end
function entity.collision:bullet(other, dx, dy)
print("Collision with a bullet!")
end
Store a reference to the player inside the specific body. In addition to 'kind', you can also put in 'data' or something and then store the table containing the enemy there. As long as it's consistent, you should be able to access it.
(Also keep in mind that the example posted would call 2 functions for each collission; a touching b and b touching a, I'm not sure if that's what you'd want.)