🤑 Indie game store🙌 Free games😂 Fun games😨 Horror games
👷 Game development🎨 Assets📚 Comics
🎉 Sales🎁 Bundles

Another program for gamepads test =)

A topic by alrado created Feb 12, 2017 Views: 190
Viewing posts 1 to 1
-- title:  Controller test
-- author: Al Rado
-- script: lua
-- input:  gamepad

TITLE="Controller test"

UP="Up" DOWN="Down" 
LEFT="Left" RIGHT="Right"
BTN_A="Btn A" BTN_B="Btn B"

STATE_ON={name="ON", color=11}
STATE_OFF={name="OFF", color=6}

GAMEPAD_ONE={
  {code=0, name=UP},
  {code=1, name=DOWN},
  {code=2, name=LEFT},
  {code=3, name=RIGHT},
  {code=4, name=BTN_A},
  {code=5, name=BTN_B}
}

GAMEPAD_TWO={
  {code=8,  name=UP},
  {code=9,  name=DOWN},
  {code=10, name=LEFT},
  {code=11, name=RIGHT},
  {code=12, name=BTN_A},
  {code=13, name=BTN_B}
}

PLAYER_ONE_NAME={value="PLAYER ONE", shiftX=6, shiftY=-3, color=7}
PLAYER_TWO_NAME={value="PLAYER TWO", shiftX=6, shiftY=-3, color=7}
GAMEPADS_VIEWS={
  {name=PLAYER_ONE_NAME,actions=GAMEPAD_ONE, 
  x=40, y=40, stateShiftX=50, sinCoeff=1.57, timeShift=0},

  {name=PLAYER_TWO_NAME, actions=GAMEPAD_TWO, 
  x=140, y=40, stateShiftX=50, sinCoeff=0.76, timeShift=0.5}
}
ACTION_STEP_Y=10

SPEED=1/1000
ROTATION_LIMIT=10
SLEEP_DURATION = 3000

wakeUpTime = 0
deltaX = 0 deltaY = 0

function TIC()
  cls()
  print(TITLE,80,10)

  slowedTime = time()*SPEED
  for viewIx=1, #GAMEPADS_VIEWS do
    wiev = GAMEPADS_VIEWS[viewIx]
    if(time() > wakeUpTime) then
      deltaX=math.cos(slowedTime+wiev.timeShift)*ROTATION_LIMIT
      deltaY=math.sin(slowedTime*wiev.sinCoeff+wiev.timeShift)*ROTATION_LIMIT
    end

    print(wiev.name.value, 
      wiev.x+wiev.name.shiftX+deltaX, 
      wiev.y+wiev.name.shiftY+deltaY,
      wiev.name.color)

    for actIx=1, #wiev.actions do
      act=wiev.actions[actIx]
      print(act.name..": ", wiev.x+deltaX, wiev.y+actIx*ACTION_STEP_Y+deltaY)

      state=btn(act.code) and STATE_ON or STATE_OFF
      print(state.name, wiev.x+wiev.stateShiftX+deltaX, 
        wiev.y+actIx*ACTION_STEP_Y+deltaY, state.color)

      if(state == STATE_ON) then
        wakeUpTime = time()+SLEEP_DURATION
        deltaX = 0 deltaY = 0
      end
    end
  end
end