follower.lua 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. -- title: follower
  2. -- author: pixelbath
  3. -- desc: reference for Gradius-like option followers
  4. -- script: lua
  5. local player = {
  6. x=50, y=50, spd=1,
  7. }
  8. local history_length = 100
  9. -- set up path history and fill with current player pos
  10. player_moving = false
  11. player_path = {}
  12. path_idx = 1
  13. for i=1,history_length do
  14. table.insert(player_path, { x=player.x, y=player.y })
  15. end
  16. -- provides wrapping values
  17. function get_previous_index(offset)
  18. local prev_idx = path_idx - offset
  19. if prev_idx <= 0 then prev_idx = history_length + prev_idx end
  20. return prev_idx
  21. end
  22. function get_next_index()
  23. local next_idx = path_idx + 1
  24. if next_idx > 100 then return 1 else return next_idx end
  25. end
  26. function TIC()
  27. -- only update when player is moving
  28. player_moving = false
  29. if btn(0) then
  30. player_moving = true
  31. player.y=player.y-player.spd
  32. end
  33. if btn(1) then
  34. player_moving = true
  35. player.y=player.y+player.spd
  36. end
  37. if btn(2) then
  38. player_moving = true
  39. player.x=player.x-player.spd
  40. end
  41. if btn(3) then
  42. player_moving = true
  43. player.x=player.x+player.spd
  44. end
  45. local fol_pos = player_path[get_previous_index(10)]
  46. local fol_pos2 = player_path[get_previous_index(20)]
  47. if player_moving then
  48. player_path[path_idx] = { x=player.x, y=player.y }
  49. path_idx = get_next_index()
  50. end
  51. cls(0)
  52. rect(player.x, player.y, 5, 5, 5)
  53. rect(fol_pos.x, fol_pos.y, 5, 5, 6)
  54. rect(fol_pos2.x, fol_pos2.y, 5, 5, 7)
  55. end
  56. -- <PALETTE>
  57. -- 000:1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2566c86333c57
  58. -- </PALETTE>