Post by tsh73 on Jan 8, 2019 15:02:01 GMT -5
So.
In thread that got winded into discussion of LB5 desired features,
Liberty BASIC 5 update
I've asked for "native multikey support"
But I've told that "I really have now idea how it could/should work".
But now I kind of - have.
We don't need "multikey" for "any keys" - we rather need if for game making.
That is, they should more or less emulate JOYSTICK.
And - LB already have such infrastructure:
call of
READJOYSTICK 1
and global variables
Joy1x, Joy1y, Joy1z, Joy1button1, Joy1button2
So I did just about the same. Set of keys chosen more or less randomly (you can make yours)
call readJStick 3
reads cursor keys, LCtrl, LShift
sets global vars
Joy3x, Joy3y, Joy3z, Joy3button1, Joy3button2
call readJStick 4
reads WASD, RCtrl, RShift
sets global vars
Joy4x, Joy4y, Joy4z, Joy4button1, Joy4button2
And - they are used just like real joystick!
(oh, and GetAsynkKeyState remove all typematic delays).
Now - is it all good or did I miss somethging?
In thread that got winded into discussion of LB5 desired features,
Liberty BASIC 5 update
I've asked for "native multikey support"
But I've told that "I really have now idea how it could/should work".
But now I kind of - have.
We don't need "multikey" for "any keys" - we rather need if for game making.
That is, they should more or less emulate JOYSTICK.
And - LB already have such infrastructure:
call of
READJOYSTICK 1
and global variables
Joy1x, Joy1y, Joy1z, Joy1button1, Joy1button2
So I did just about the same. Set of keys chosen more or less randomly (you can make yours)
call readJStick 3
reads cursor keys, LCtrl, LShift
sets global vars
Joy3x, Joy3y, Joy3z, Joy3button1, Joy3button2
call readJStick 4
reads WASD, RCtrl, RShift
sets global vars
Joy4x, Joy4y, Joy4z, Joy4button1, Joy4button2
And - they are used just like real joystick!
(oh, and GetAsynkKeyState remove all typematic delays).
Now - is it all good or did I miss somethging?
'by tsh73, Jan 2008, +keyboard removed, GetAsynkKeyState added - 2019
'v 06 - so for LB I add GetAsynkKeyState, reading keys into buffer like joystick
' Joy1x, Joy1y, Joy1z, Joy1button1, Joy1button2
nomainwin
dim m(100,4)
global Joy3x, Joy3y, Joy3z, Joy3button1, Joy3button2 'cursor keys, LCtrl, LShift
global Joy4x, Joy4y, Joy4z, Joy4button1, Joy4button2 'WASD, RCtrl, RShift
'Test for joystick - works for me
'it seems that if joystick connected it returns some middle position (32767 for me)
'and if not connected then 0
readjoystick 1
JoysticPresent = Joy1x<>0 OR Joy1y<>0
'notice Joy1x;" ";Joy1y
NOTICE "Joystick test"+chr$(13)+"Joystick "+iif$(JoysticPresent ,"", "NOT")+" detected"
ans=2
prompt "Please select controls"+chr$(13)+_
iif$(JoysticPresent ,"1 - Joystic", "")+_
" 2 - cursor keys, LCtrl/Shift"+_
" 3 - WASD, RCtrl/Shift"; ans
WindowWidth = 600
WindowHeight = 430
UpperLeftX = (DisplayWidth - WindowWidth)/2
UpperLeftY = (DisplayHeight - WindowHeight)/2
open "Joystick test" for graphics_nsb_nf as #gr
#gr, "trapclose [quit]"
#gr, "setfocus"
#gr, "down; rule XOR"
x = 300
y = 200
zero = 32767
'main loop
10
'draw all thing
'main thing
#gr, "color blue; size 20; set ";x;" ";y
'zap thing
if Fired then 'clear
n = 32
for i = 0 to n-1
xx = x0+d*cos(i/n*2*3.1416)
yy = y0+d*sin(i/n*2*3.1416)
#gr, "color red; size 5; set ";xx;" ";yy
next
end if
'draw missile things
for i = 1 to nM
#gr, "color green; size 10; set ";m(i,1);" ";m(i,2)
next
'end of 'draw all thing
'timer delay
timer 50, [nextt]
wait
[nextt]
timer 0
#gr, "discard"
'clear all thing - with a XOR we just draw it again!
'clear main thing
#gr, "color blue; size 20; set ";x;" ";y
'clear zap thing
if Fired then 'clear
n = 32
for i = 0 to n-1
xx = x0+d*cos(i/n*2*3.1416)
yy = y0+d*sin(i/n*2*3.1416)
#gr, "color red; size 5; set ";xx;" ";yy
next
end if
'clear missile things
for i = 1 to nM
#gr, "color green; size 10; set ";m(i,1);" ";m(i,2)
next
'end of 'clear all thing
'read and change coords
'now we add INKEY thing
scan
'print Joy1x, Joy1y, Joy1z, Joy1button1, Joy1button2
select case ans
case 1
readjoystick 1
dx = (Joy1x>zero)-(Joy1x<zero) 'sign function for ((Joy1x - 32767))
dy = (Joy1y>zero)-(Joy1y<zero) 'sign function
case 2
call readJStick 3
dx = (Joy3x>zero)-(Joy3x<zero) 'sign function for ((Joy1x - 32767))
dy = (Joy3y>zero)-(Joy3y<zero) 'sign function
case 3
call readJStick 4
dx = (Joy4x>zero)-(Joy4x<zero) 'sign function for ((Joy1x - 32767))
dy = (Joy4y>zero)-(Joy4y<zero) 'sign function
end select
'This thing only because I cannot move and shoot by keyboard.
'So I have to store last <>0 speed
if dx <>0 or dy <>0 then 'speed not 0
last.dx = dx: last.dy = dy
end if
MegaBlastWanted = (Joy1button2<>0) or (Joy3button2<>0) or (Joy4button2<>0)
RocketWanted = (Joy1button1<>0) or (Joy3button1<>0) or (Joy4button1<>0)
aKey$ = ""
x = x+3*dx
y = y+3*dy
if MegaBlastWanted and Fired=0 then 'start a zap/ This is one-time thing - you cannot issue next zap till first one dissipates
Fired=1
x0 = x
y0 = y
d = 0
end if
'change zap state
d = d+10
if d>350 then Fired = 0 'ready for next zap
if RocketWanted and nM<100 then 'fire a fire-and-forget missile. Of cource player can forget but we should keep it - so we have 100
if last.dx <>0 or last.dy <>0 then 'speed not 0
' if m(nM,1)<>x or m(nM,2)<>y then 'not same point
nM = nM+1
m(nM,1)=x+15*last.dx 'coords. Move them out of ship initially
m(nM,2)=y+15*last.dy
m(nM,3)=15*last.dx 'speed
m(nM,4)=15*last.dy
end if
' end if
end if
'change missile state
'somehow should check out of screen condition
'nMM = nM
for i = nM to 1 step -1
m(i,1) = m(i,1) + m(i,3)
m(i,2) = m(i,2) + m(i,4)
next
while nM>0
if m(nM,1)<0 or m(nM,1)>600 or m(nM,2)<0 or m(nM,2)>400 then
nM = nM - 1 'just that.
else
exit while
end if
wend
'show some stats
#gr, "color black; rule over"
#gr, "place 10 20;\Rockets in flight ";using("###",nM);iif$(Fired, " Megablast"," ")
#gr, "rule xor"
goto 10
[keyPressed]
aKey$ = Inkey$
'if aKey$<>"" then print ">";aKey$;"<"
wait
'-----------------------
[quit]
timer 0
close #gr
end
'=========================================
function iif$(test, valYes$, valNo$)
iif$ = valNo$
if test then iif$ = valYes$
end function
'========================================
function GetAsyncKeyState(key) 'from Demo By KcDan
calldll #user32, "GetAsyncKeyState",key as long,GetAsyncKeyState as long
if GetAsyncKeyState<>0 then GetAsyncKeyState=1
end function
sub readJStick n
zero = 32767
one = 65535
select case n
'global Joy3x, Joy3y, Joy3z, Joy3button1, Joy3button2 'cursor keys, space, Enter
'global Joy4x, Joy4y, Joy4z, Joy4button1, Joy4button2 'WASD, Ctrl, CapsLock
case 3
Joy3y=zero
Joy3x=zero
Joy3button1=0
Joy3button2=0
if GetAsyncKeyState(_VK_UP) then Joy3y=0
if GetAsyncKeyState(_VK_DOWN) then Joy3y=one
if GetAsyncKeyState(_VK_LEFT) then Joy3x=0
if GetAsyncKeyState(_VK_RIGHT) then Joy3x=one
if GetAsyncKeyState(_VK_LCONTROL) then Joy3button1=1
if GetAsyncKeyState(_VK_LSHIFT) then Joy3button2=2 'like real joypad
case 4
Joy4y=zero
Joy4x=zero
Joy4button1=0
Joy4button2=0
if GetAsyncKeyState(asc("W")) then Joy4y=0
if GetAsyncKeyState(asc("S")) then Joy4y=one
if GetAsyncKeyState(asc("A")) then Joy4x=0
if GetAsyncKeyState(asc("D")) then Joy4x=one
if GetAsyncKeyState(_VK_RCONTROL) then Joy4button1=1
if GetAsyncKeyState(_VK_RSHIFT) then Joy4button2=2 'like real joypad
end select
end sub