alpacus [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=46821 Left side collision not working <p>Hello all! I'm having an issue with a game I'm developing, where the collision is working flawlessly with any side other than the left side. for some reason it just wont work with the warps in game and I'm not sure why. left side collisions are working everywhere else, namely with stopping the player walking thru the walls. but not for the warps in game, which are supposed to work kind of like Pok&eacute;mon's teleporting system. Frankly, I'm stumped and would appreciate if someone could take a look at this. Linked are not only the relevant code in the game, but a test cartridge as well to see what the problem is.</p> <p>the main files i believe the bug should be happening in is input.lua, collision.lua, and game_manager.lua</p> <h1>collision.lua</h1> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>--collsion for the map function collide_map(obj,aim,flag) --obj = table needs x,y,w,h --aim = left,right,up,down local x=obj.x local y=obj.y local w=obj.w local h=obj.h local x1=0 local y1=0 local x2=0 local y2=0 if aim==&quot;left&quot; then x1=x-1 y1=y x2=x y2=y+h-1 elseif aim==&quot;right&quot; then x1=x+w-1 y1=y x2=x+w y2=y+h-1 elseif aim==&quot;up&quot; then x1=x+2 y1=y-1 x2=x+w-3 y2=y elseif aim==&quot;down&quot; then x1=x+2 y1=y+h x2=x+w-3 y2=y+h end --pixels to tiles x1/=8 y1/=8 x2/=8 y2/=8 if fget(mget(x1,y1), flag) or fget(mget(x1,y2), flag) or fget(mget(x2,y1), flag) or fget(mget(x2,y2), flag) then return true else return false end end</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <h1>game_manager.lua</h1> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>--manages all overarching functions and mechanics --ex: turns --turn structure turns = { turn = 0, moves = 4, actions = 2, end_moves = false, end_actions = false } --moves player with given direction function move(dir) --if moving left and not colliding with walls if turns.moves &gt; 0 and dir == &quot;left&quot; and not collide_map(p, dir, 1) then p.x -= 8 turns.moves -= 1 printh(&quot;col: &quot;..tostring(collide_map(p, dir, 3)), &quot;log&quot;, false) --check if colliding with left side warp !NOT WORKING! if collide_map(p, dir, 3) then -- move map printh(&quot;moving map left&quot;, &quot;log&quot;, false) _map.last_x = _map.x _map.x -= 1 printh(&quot;x:&quot;.._map.x, &quot;log&quot;, false) p.x = 120 end --if moving left and not colliding with walls elseif turns.moves &gt; 0 and dir == &quot;right&quot; and not collide_map(p, dir, 1) then p.x += 8 turns.moves -= 1 --check if colliding with right side warp !WORKING! if collide_map(p, dir, 3) then -- move map printh(&quot;moving map right&quot;, &quot;log&quot;, false) _map.last_x = _map.x _map.x += 1 printh(&quot;x:&quot;.._map.x, &quot;log&quot;, false) p.x = 8 end elseif turns.moves &gt; 0 and dir == &quot;up&quot; and not collide_map(p, dir, 1) then p.y -= 8 turns.moves -= 1 elseif turns.moves &gt; 0 and dir == &quot;down&quot; and not collide_map(p, dir, 1) then p.y += 8 turns.moves -= 1 else if turns.actions &gt; 0 and turns.moves &lt;= 0 then turns.end_moves = true elseif turns.actions &lt;= 0 then turns.end_actions = true end end end function end_turn() turns.turn += 1 turns.moves = 4 turns.actions = 2 turns.end_moves = false turns.end_actions = false end function draw_turns() if turns.end_moves then print(&quot;out of moves&quot;, 32, 0) elseif turns.end_actions then print(&quot;out of actions&quot;, 32, 0) end print(&quot;turn: &quot;..turns.turn, 0, 0) print(&quot;moves: &quot;..turns.moves, 0, 8) print(&quot;actions: &quot;..turns.actions, 0, 16) end</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <h1>map.lua</h1> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>-- map functionality, using the x and y in _map to determine which room the player is in _map = { x = 0, y = 0, last_x = 0, last_y = 0 } --draws the map function draw_map() local x = 0 local y = 0 if _map.x ~= 0 then x = (_map.x - _map.last_x)*(_map.x+15) end if _map.y ~= 0 then y = (_map.y - _map.last_x)*(_map.x+15) end printh(&quot;draw_x:&quot;..x, &quot;log&quot;, false) map(x, y) end</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <h1>input.lua</h1> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>--input variables input_frozen = false --input function --gets input for all buttons in the game and provides logic behind them function input() if btnp(5) and not menu.menu_opened then open_menu() elseif btnp(5) and menu.menu_opened then close_menu() end if not input_frozen then if btnp(0) then move(&quot;left&quot;) end if btnp(1) then move(&quot;right&quot;) end if btnp(2) then move(&quot;up&quot;) end if btnp(3) then move(&quot;down&quot;) end if btnp(4) then end_turn() end end end</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <h1>player.lua</h1> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>--player class, contains all information relavent to the player character --structure containing all variables relevant to the player p = { sp = 1, x = 64, y = 64, w = 8, h = 8 } --draws the player to the screen function draw_player() spr(p.sp, p.x, p.y) end</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <h1>menu.lua</h1> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>--menu class containing all logic for the in game menu --structure containing all variabkes relevant to the menu menu = { menu_opened = false } --opens the menu function open_menu() input_frozen = true menu.menu_opened = true end --closes the menu function close_menu() input_frozen = false menu.menu_opened = false end -- draws the menu function draw_menu() cls(0) map(112, 48) print(&quot;menu&quot;, 59, 6) end</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <h1>main.lua</h1> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>--default functions draw, update, and init function _init() --setting player p.sp = 1 p.x = 64 p.y = 64 --resetting menu and input menu.menu_opened = false input_frozen = false end function _draw() if menu.menu_opened then draw_menu() elseif not menu.menu_opened then cls(3) draw_map() draw_player() draw_turns() end end function _update() input() end</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>here is a test cartridge to see the issue at play</p> <p>controls:<br /> left: move left<br /> right: move right<br /> up: move up<br /> down: move down</p> <p>x: open menu<br /> z: advance turn</p> <p>to recreate the bug, you can walk through the warp on the right of the screen from spawn (orange arrows)<br /> but when you make it to the other side, you cannot warp back even though the logic should let you.</p> <p> <table><tr><td> <a href="/bbs/?pid=117994#p"> <img src="/bbs/thumbs/pico8_fogijoyeso-0.png" style="height:256px"></a> </td><td width=10></td><td valign=top> <a href="/bbs/?pid=117994#p"> picorim-testbug</a><br><br> by <a href="/bbs/?uid=46821"> alpacus</a> <br><br><br> <a href="/bbs/?pid=117994#p"> [Click to Play]</a> </td></tr></table> </p> <p>thanks in advance for the help. I know the code is not as great as it could be, I'm relatively new to lua and pico-8 in general. thanks again!</p> https://www.lexaloffle.com/bbs/?tid=49495 https://www.lexaloffle.com/bbs/?tid=49495 Mon, 26 Sep 2022 18:44:45 UTC