
' Controllers
' alternative version, support for:
' terminal using inkey$

' this won't be as good a controller as a USB
' keyboard or a dedicated controller, but you can use it for testing

CONST Keys.Up = "o"
CONST Keys.Down = "k"
CONST Keys.Left = "z"
CONST Keys.Right = "x"

CONST Keys.ButtonA = " " ' Space
CONST Keys.ButtonB = "b" ' b, but Enter might be better

DIM Keys.Stale(256)
DIM Keys.Pressed(256)

SUB Keys.Read
  LOCAL k$
  do
    if k$ <> "" then
      Keys.Pressed(asc(k$))=1      
    endif
    k$=inkey$
  loop until k$=""
END SUB

SUB Keys.Flush
  LOCAL i
  for i=1 to 255
    Keys.Stale(i)=0
    Keys.Pressed(i)=0
  next
END SUB

FUNCTION Keys.RequestKey(k$)
  LOCAL i = asc(k$)
  if Keys.Stale(i) then Keys.Flush
  Keys.Stale(i)=1
  Keys.Read
  Keys.RequestKey=Keys.Pressed(i)
END FUNCTION

SUB Controllers.Init players
  ' default argument for players is interpreted as 1 player

  if players > 1 then print "multiple players not supported yet"

END SUB

FUNCTION Controllers.Up()
  Controllers.Up = Keys.RequestKey(Keys.Up)
END FUNCTION

FUNCTION Controllers.Down()
  Controllers.Down = Keys.RequestKey(Keys.Down)
END FUNCTION

FUNCTION Controllers.Left()
  Controllers.Left = Keys.RequestKey(Keys.Left)
END FUNCTION

FUNCTION Controllers.Right()
  Controllers.Right = Keys.RequestKey(Keys.Right)
END FUNCTION

FUNCTION Controllers.ButtonA()
  Controllers.ButtonA = Keys.RequestKey(Keys.ButtonA)
END FUNCTION

FUNCTION Controllers.ButtonB()
  Controllers.ButtonB = Keys.RequestKey(Keys.ButtonB)
END FUNCTION

