-- title:  follower
-- author: pixelbath
-- desc:   reference for Gradius-like option followers
-- script: lua

local player = {
    x=50, y=50, spd=1,
}

local history_length = 100

-- set up path history and fill with current player pos
player_moving = false
player_path = {}
path_idx = 1
for i=1,history_length do
    table.insert(player_path, { x=player.x, y=player.y })
end

-- provides wrapping values
function get_previous_index(offset)
    local prev_idx = path_idx - offset
    if prev_idx <= 0 then prev_idx = history_length + prev_idx end
    return prev_idx
end
function get_next_index()
    local next_idx = path_idx + 1
    if next_idx > 100 then return 1 else return next_idx end
end

function TIC()
    -- only update when player is moving
    player_moving = false
    if btn(0) then
        player_moving = true
		player.y=player.y-player.spd
	end
	if btn(1) then
        player_moving = true
		player.y=player.y+player.spd
	end
	if btn(2) then
        player_moving = true
		player.x=player.x-player.spd
	end
	if btn(3) then
        player_moving = true
		player.x=player.x+player.spd
	end

    local fol_pos = player_path[get_previous_index(10)]
    local fol_pos2 = player_path[get_previous_index(20)]
    if player_moving then
        player_path[path_idx] = { x=player.x, y=player.y }
        path_idx = get_next_index()
    end

	cls(0)
	rect(player.x, player.y, 5, 5, 5)
    rect(fol_pos.x, fol_pos.y, 5, 5, 6)
    rect(fol_pos2.x, fol_pos2.y, 5, 5, 7)
end

-- <PALETTE>
-- 000:1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2566c86333c57
-- </PALETTE>
