I'm working on my 2nd official project, watching tutorials and self-teaching (not the best idea, I know.) I'm trying to do a very simple adventure style game for my wife to play, and I'd like any enemies that I place on the map to only move when the player is onscreen with them. I'm obviously not thinking this through thoroughly. Any help would be appreciated. I apologize for the code snippets I included, I'm still learning the formatting for the message board
function draw_map()
--map screen x,y in tiles
--follows player
mapx=flr(p.x_tile/16)16
mapy=flr(p.y_tile/16)16
--camera and hud x,y
cx=mapx8
cy=mapy8
--bound camera within game area
if (cx<=0) cx=0
if (cy<=0) cy=0
if (cx>=1024) cx=1024
if (cy>=512) cy=512
map()
camera(cx,cy)
draw_hud()
end
function move_enemies()
for e in all(enemies) do
--priorty one
--restrict offscreen enemy
--movement...debug needed
if mapx!=flr(e.x_tile/16)16 or mapy!=flr(e.y_tile/16)16 then
return
else
--x,y for movement
e.e_newx=e.x_tile
e.e_newy=e.y_tile
interact(e.e_newx,e.e_newy)
if e.etype=="beetle" then
--random move variable
local r=flr(rnd(4)+1)
--checks for bamboo and
--"eats" it
if check_tile(bamboo,e.x_tile-1,e.y_tile) then
move_left(e)
swap_tile(e.e_newx,e.e_newy)
elseif check_tile(bamboo,e.x_tile+1,e.y_tile) then
move_right(e)
swap_tile(e.e_newx,e.e_newy)
elseif check_tile(bamboo,e.x_tile,e.y_tile-1) then
move_up(e)
swap_tile(e.e_newx,e.e_newy)
elseif check_tile(bamboo,e.x_tile,e.y_tile+1) then
move_down(e)
swap_tile(e.e_newx,e.e_newy)
else
--if no bamboo then moves in
--a random direction
if r==1 then
move_left(e)
elseif r==2 then
move_right(e)
elseif r==3 then
move_up(e)
elseif r==4 then
move_down(e)
end
end
end
--bind enemies on map and set
--offset for wall collision
if can_move(e.e_newx,e.e_newy) then
e.x_tile=mid(mapx,e.e_newx,mapx+15)
e.y_tile=mid(mapy,e.e_newy,mapy+15)
else
e.off_x=e.off_x/4
e.off_y=e.off_y/4
sfx(0)
end
end
end
end
In your function move_ennemies, you loop over every ennemy. If the ennemy is in the screen, it works, but as soon as you encouter an ennemy in the list that is not on screen, you call return : that exits the function immediately, the enemies further down the list will not be checked and won't get a chance to move.
Just remove the return line.
by the way, the \ operator is integer division, so
flr(x/16) can be expressed as x\16
@RealShadowCaster thank you so much. I feel like a bit of an idiot haha. Too much human thinking in not remembering the return was going to kill the whole function. Also, thanks for the advice on the integer division operand. That could save me tons of tokens in the end.
@Andrew967 Thank you. My wife loves pandas, so the only direction I got was she wants a panda who rolls and eats bamboo. Dylan Bennett has a great series of short tutorials that I watched for the basic structure, and I'm currently watching through Lazy Devs Roguelike videos so I used his method of smoothing out the player movement. I love your version of his Basic Shmup. The different bullets fired in the spread shot was really cool. I'm scratching my head at how you did that.
@Tw1zTeD616 ,no problem, I'm guessing what you meant is continue, except that keyword doesn't exist in LUA and return was the nearest thing available.
I got stuck for a long time on a simple function when I started LUA : I was assigning 0 to a boolean out of habit from C/perl/javascript, when I meant false, and simply couldn't see it after rereading it a dozen time.
Took a couple second to a seasoned LUA dev to point it to me.
About tokens, if you are willing to sacrifice readability, you can take advantage of the fact that 16 is a nice round binary number.
flr(p.x_tile/16) * 16
becomes
p.x_tile&0xFFF0
[Please log in to post a comment]