Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

TIC-80

Fantasy computer for making, playing and sharing tiny games. · By Nesbox

You have not yet tested the gamepad? ) Now on moonScript

A topic by alrado created Feb 16, 2017 Views: 407
Viewing posts 1 to 1
(+1)
-- title:  TIC-80 controller test
-- author: Al Rado 16.02.2017
-- desc:   controller test, only moonScript
-- script: moon
-- input:  gamepad
-- pal:    DB16


TITLE="TIC-80 controller test"
SCREEN_W=240


export class Gamepad
  new:(name,startCode=0,mainColor=6,sound=36)=> 
    @name=name 
    @buttons={
      {name:"Up",     code:0+startCode, x:7,  y:4},
      {name:"Down",   code:1+startCode, x:7,  y:12},
      {name:"Left",   code:2+startCode, x:3,  y:8},
      {name:"Right",  code:3+startCode, x:11, y:8},
      {name:"Btn A",  code:4+startCode, x:23, y:10},
      {name:"Btn B",  code:5+startCode, x:30, y:6}
    }
    @pressedKeys={}
    @mainColor=mainColor
    @bodyColor=7
    @shadowColor=1
    @pressedBtnColor=5
    @sound=sound


  Update:(x,y,msgY)=>
    @DrawBack(x,y)  
    @pressedKeys={}
    for button in *@buttons
      if (btn button.code) then 
        table.insert @pressedKeys, button.name
        circ x+button.x,y+button.y,2,@pressedBtnColor
    @PrintKeys msgY


  PrintKeys:(y)=>
    if (#@pressedKeys > 0) then 
      keysMsg=""
      for i=1, #@pressedKeys
        keysMsg..=@pressedKeys[i]
        if (i<#@pressedKeys) then keysMsg..=", "
      title=@name.." pressed: "
      len=print title..keysMsg,-100,-100 --fake print  
      posX=(SCREEN_W-len)/2
      shiftX=print title,posX,y 
      print keysMsg,posX+shiftX,y,@mainColor  
  
  IsActive:=>#@pressedKeys>0


  AddViewTo:(views)=> 
    if (@IsActive()) then table.insert views, ViewParam(@mainColor,@sound)
 
  DrawBack:(x,y)=>
    doubleDraw=(drawFunc,colorOne,colorTwo)-> 
      drawFunc 1, colorOne 
      drawFunc 0, colorTwo 


    drawBody=(shiftX,shiftY,colorOne,colorTwo)-> 
      drawLayer=(shift,color)-> 
        rect x+shiftX+shift,y+shiftY+shift,36,18,color  
      doubleDraw(drawLayer,colorOne,colorTwo)


    drawDpad=(shiftX,shiftY,colorOne,colorTwo)-> 
      drawLayer=(shift,color)-> 
        rect x+shiftX+shift,y+shiftY+shift,9,3,color 
        rect x+shiftX+shift+3,y+shiftY+shift-3,3,9,color  
      doubleDraw(drawLayer,colorOne,colorTwo)


    drawButton=(shiftX,shiftY,colorOne,colorTwo)-> 
      drawLayer=(shift,color)-> 
        circ x+shiftX+shift,y+shiftY+shift,2,color 
      doubleDraw(drawLayer,colorOne,colorTwo)


    drawBody(0,0,@shadowColor,@bodyColor)
    drawDpad(3,7,@shadowColor,@mainColor)
    drawButton(23,10,@shadowColor,@mainColor)
    drawButton(30,6,@shadowColor,@mainColor)


export class ViewParam
  new:(color,sound)=>
    @color=color
    @sound=sound
    
export UpdateSingleView=(view)->
  Border view.color
  Beep view.sound


export Border=(color)->poke(0x3FF8,color)
export Beep=(sound)->sfx(sound,sound,0,3)


export gmpdOne=Gamepad("Player one")
export gmpdTwo=Gamepad("Player two",8,13,10)
export NOT_ACTIVE=ViewParam(8,-1)
export BOTH_ACTIVE=ViewParam(15,23)


export TIC=->
  cls 8
  print(TITLE,70,30)
  
  gmpdOne\Update(50,80,116)
  gmpdTwo\Update(160,80,126)


  views={NOT_ACTIVE}
  gmpdOne\AddViewTo(views)
  gmpdTwo\AddViewTo(views)
  UpdateSingleView #views>2 and BOTH_ACTIVE or views[#views]