Page 2 of 2

Re: [SOLVED] adnzzzzZ windfield collision classes confusion

Posted: Fri May 08, 2020 9:06 pm
by kuzika
I now have a worldstate.lua that contains the world enter and exit functions, this is required in main love.load() under everything else. The player.lua simply checks the player.state itself. All the collision checks will be handled in worldstate.lua from now on and nowhere else as the world updates itself in love.update().

main.lua

Code: Select all

love.load()
--under everything else
require("worldstate")
end
worldstate.lua

Code: Select all

myWorld:setEnter(function(s1,s2,c,i)
    if s2:getClass() == "ObjectA" and
    (p.player.vx > 0 or p.player.vy > 0)
    then
        p.player.state = "pushing"
    end
end)

myWorld:setExit(function(s1,s2,c,i)
    if s2:getClass() == "ObjectA" and
    (p.player.vx > 0 or p.player.vy > 0)
    then
        p.player.state = "walking"
    elseif p.player.vx == 0 and p.player.vy == 0 then
        p.player.state = "standing"
    end
end)
player.lua

Code: Select all

  p.draw = function(self)
  local pose = sprites.stand_cycle
    if self.player.state == "pushing" then
      pose = sprites.player_pushing
    elseif self.player.state == "walking" then
      pose = sprites.player_walking
    elseif self.player.state == "standing" then
      pose = sprites.player_standing
    end
    --the rest of the code draws the pose
 end
pushstuff2.png
pushstuff2.png (211.7 KiB) Viewed 1684 times
It works just fine. I think now this is the best way to handle this.
pushstuff5.love
(357.89 KiB) Downloaded 96 times

Re: [SOLVED] adnzzzzZ windfield collision classes confusion

Posted: Fri May 08, 2020 10:11 pm
by 4vZEROv
If you use the world:setEnter callback there is no way for you to know when a contact occurs that "ObjectA" will be the shape2 and not the shape1.

Code: Select all

myWorld:setEnter(function(s1,s2,c,i)
	if (s1:getClass() == "ObjectA" and s2:getClass() == "Player") or 
	(s1:getClass() == "Player" and s2:getClass() == "ObjectA") then 
		-- code --
	end
end)
I alse see in your player.lua file that you still have

Code: Select all

    self.player.collider:setPostsolve(function(s1,s2)
        if s2:getClass() == "ObjectA" then
            self.player.state = "pushing"
        end
    end)
inside the update function. Don't !
The four callbacks (enter/exit/pre/post) only need to be set once, they will be automaticaly called every frame inside your 'myWorld:update(dt)' function.

Aside from that the code looks better.

If you have any bug/question/request concerning the library you can contact by pm, on github or twitter.