'######################################################
'## "R Type"
'## Coded For Drew 101010 At The Yabasic.Co.Uk Forums
'## By Nick Simpson Aka Mr. Shockwave.
'## Hope That This IS The Sort Of Thing That You Wanted
'## Drew, Have Fun With It! (Sorry It Took A While!)
'######################################################
'## Program Notes (26/02/02).
'## ~~~~~~~~~~~~~~~~~~~~~~~~~
'## The orb works as it did in R-type, you can shoot it
'## away from the front of your ship by holding R1, it 
'## will slowly make its' way back to the ship or you can
'## press R2 to bring it back quickly.
'## The Orb is indestructable and will kill any mines it
'## touches.
'## Control the ship with the Dpad, press (X) to fire, the
'## Orb and the player's ship both fire thier own bullets.
'## Power shot has been left out as the original author
'## didn't request it as has the scenery although I have
'## tarted it up a bit with some simple lighting effects.
'## I added a simple scoring system and energy bar to make
'## it look more finished, I added a simple scrolling star
'## background to give the illusion of scrolling.
'## The player control is inertia based for a nicer "feel"
'## The Game will end when the energy bar is used up.
'## Collisions between ship and mines deplete energy.
'## Enemy firing has been omitted because the original
'## author wanted to add it himself.
'## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'## Technical aspects:
'## ~~~~~~~~~~~~~~~~~~
'##*The graphics have been kept simple, they can be easily
'## improved, this is to save typing time, my own version
'## has much nicer looking mines and player ship.
'##*I had to stagger the collisions over three updates to
'## ensure that this ran smoothly at 50fps.
'##*There is probably enough free time for about half a
'## dozen enemy bullets, I'd suggest that the code to
'## impliment them is placed on the cycles colman=0 and
'## colman=1 as there is more free time available during
'## these two updates, the bullet/enemy cycle uses a lot
'## of processor time.
'##*The addition of enemy bullets would make this game a
'## lot more chalenging.
'##*The mine/player collisions test each point of the 
'## triangle making up the player ship.
'## Shockwave: 20/02/02.
'######################################################
gosub setup
'######################################################
'## Main Loop
'######################################################
repeat
  colman=colman+1
  if colman>2 colman=0
  gosub starfield
  gosub control
  gosub drawmines
  gosub drawplayer
  if firing=1 gosub bullets
  gosub drawbullets
  gosub stats
  if colman=0 gosub mineplayercol
  if colman=1 gosub orbminecol
  if colman=2 gosub bulletminecol
until (energy<=0)

exit
label stats
setrgb 1,250,100,100
fill rect 50,480 to 50+energy,500
setrgb 1,255,255,255
rect 50,480 to 250,500
setrgb 1,20,80,220
text 480,494,"SCORE:"+str$(score),"cc"
text 60,494,"ENERGY"
return

label starfield
  setdrawbuf dw
  dw=1-dw
  setdispbuf dw
setrgb 1,red,20*sin(mm/2),0
setrgb 2,20*sin(mm/3),grn,0
setrgb 3,red,0,20*sin(mm/4)+blu
red=red-(red/10)
grn=grn-(grn/10)
blu=blu-(blu/10)

gtriangle 0,0 to 640,512 to 0,512
gtriangle 0,0 to 640,512 to 640,0
  mm=mm+.1

setrgb 1,50,20,20
for a=1 to stars
 fill rect starx(a),stary(a) to starx(a)-stars(a),stary(a)-stars(a)
 starx(a)=starx(a)+stars(a)
 if starx(a)<0 starx(a)=starx(a)+640
next a
return


label bulletminecol
'#######################################################
'## Check For Collisions Between Bullets And Mines
'#######################################################
for a=1 to maxbul
 if pss(a)=1 then
  for b=1 to mine
 if mines(b)=1 then
 if psx(a)>minex(b)-15 and psx(a)<minex(b)+15 and psy(a)<miney(b)+15 and psy(a)>miney(b)-15 then
  mines(b)=-50
  pss(a)=0
  red=red+50
  grn=grn+50
  blu=blu+50
  score=score+10
fi
fi
  next b
fi
 if oss(a)=1 then
  for b=1 to mine
 if mines(b)=1 then
 if osx(a)>minex(b)-15 and osx(a)<minex(b)+15 and osy(a)<miney(b)+15 and osy(a)>miney(b)-15 then
  mines(b)=-50
  oss(a)=0
  red=red+50
  grn=grn+50
  blu=blu+50
  score=score+11
fi
fi
  next b

fi
next a

return

label orbminecol
'#######################################################
'## Check For Collisions Between Orb And Mines
'#######################################################
for a=1 to mine
if mines(a)=1 then
 if orbx>minex(a)-15 and orbx<minex(a)+15 and orby<miney(a)+15 and orby>miney(a)-15 then
 mines(a)=-50
  red=red+80
  grn=grn+80
  blu=blu+80
  score=score+15
 fi
fi
next a
return

label mineplayercol
'#######################################################
'## Check For Collisions Between Mines And Player
'#######################################################
for a=1 to mine
collide=0

 if mines(a)=1 then
'#######################################
'## Check top corner
'#######################################
 if plx>minex(a)-10 and plx<minex(a)+10 and ply-10>miney(a)-10 and ply-10<miney(a)+10 then
  collide=1
  fi
'#######################################
'## Check Bottom Corner
'#######################################
 if plx>minex(a)-10 and plx<minex(a)+10 and ply+10>miney(a)-10 and ply+10<miney(a)+10 then
  collide=1
  fi
'#######################################
'## Check Front
'#######################################
 if plx+40>minex(a)-10 and plx+40<minex(a)+10 and ply>miney(a)-10 and ply<miney(a)+10 then
  collide=1
  fi
 if collide=1 then
  mines(a)=-50
  red=red+150
  grn=grn+150
  blu=blu+150
energy=energy-10
 fi

fi
next a
return

return

label drawbullets
'########################################################
'## Draw Player And Orb Bullets
'########################################################
setrgb 1,255,50,ran(250)
 for a=1 to maxbul
  if pss(a)=1 then
    fill rect psx(a)-2,psy(a)-2 to psx(a)+2,psy(a)+2
    psx(a)=psx(a)+10
 if psx(a)>640 pss(a)=0
  fi
 next a
setrgb 1,50,ran(250),255
 for a=1 to maxbul
  if oss(a)=1 then
    fill rect osx(a)-2,osy(a)-2 to osx(a)+2,osy(a)+2
    osx(a)=osx(a)+10
 if osx(a)>640 oss(a)=0
  fi
 next a

return

label bullets
'#######################################################
'## Set Up New Bullet
'#######################################################
for a=1 to maxbul
 if pss(a)=0 then
   psx(a)= plx+40
   psy(a)= ply
   pss(a)=1
   a=maxbul+1
 fi
next a

for a=1 to maxbul
 if oss(a)=0 then
   osx(a)= orbx
   osy(a)= orby
   oss(a)=1
   a=maxbul+1
 fi
next a
firedelay=0
return

label control

'######################################################
'## Read Dpad And Update Player + Orb Accordingly
'######################################################
if firedelay<10 firedelay=firedelay+1
firing=0: rem de-activate firing flag
c=peek("port1"):rem read pad

if my>0 my=my-.5:rem These Lines Cancel
if my<0 my=my+.5:rem Out Movement Of
if mx>0 mx=mx-.5:rem The Player To Give
if mx<0 mx=mx+.5:rem An Inertia Based Control

  if and (c,16)<>0 my=-4 : rem Move Up
   if and (c,32)<>0 mx=4 : rem Move Right
   if and (c,64)<>0 my=4 : rem Move Down
 if and (c,128)<>0 mx=-4 : rem Move Left
if (and (c,16384)<>0 and (firedelay=10)) firing=1 :rem Fire!
 if and (c,2048)<>0 then stuck=-1 : orbx=orbx+5:fi
'#######################################################
'## Quickly Retrieve Orb If R2 Pressed.
'#######################################################
 if and (c,512)<>0 then
      orbx=orbx-5
      orby=orby+((ply-orby)/20)
 fi
'#######################################################
'## Orb Drifts Back Slowly If Unstuck
'#######################################################
 if stuck=-1 then
 orbx=orbx-1
 orby=orby+((ply-orby)/40)
 fi

if orbx<plx+50 then stuck=1 : orbx=plx+50:fi:rem Reattach 

    if orbx>600 orbx=600:rem Limit Orb

    plx=plx+mx :rem Update Player
    ply=ply+my

'#######################################################
'## Move Orb With Ship If Stuck
'#######################################################
        if stuck=1 then
         orbx=orbx+mx
         orby=orby+((ply-orby)/5)
        fi
    if plx<0 plx=0:rem Limit Player To Screen.
if plx>550 plx=550
  if ply<20 ply=20
if ply>492 ply=492
return

label drawplayer
'######################################################
'## Draws The Player And "Orb"
'######################################################
setrgb 1,255,255,255
fill triangle plx,ply-10 to plx,ply+10 to plx+40,ply
fill rect orbx-5,orby-5 to orbx+6,orby+6
return

label drawmines
'#######################################################
'## This Routine Draws All Graphics To Do With Mines And
'## Also Moves & Resets Thier Positions.
'#######################################################
    w=12*sin(mm):rem Generate Sine No.s For Animation
    v=10*cos(mm):rem As Above.

for a=1 to mine
setrgb 1,10*w,10*v,170
'#######################################################
'## Draw Mine If It's Not Dead.
'#######################################################
if mines(a)=1 then
  fill rect minex(a)-v,miney(a)-v to minex(a)+v,miney(a)+v
       rect minex(a)-w,miney(a)-w to minex(a)+w,miney(a)+w
fi
if mines(a)<0 then
 setrgb 1,255,255,255
 mines(a)=mines(a)+5
 rect minex(a)-mines(a),miney(a)-mines(a) to minex(a)+mines(a),miney(a)+mines(a)
fi
'#######################################################
'## Move Mine And Check If Still On Screen
'#######################################################
minex(a)=minex(a)-minev(a)
miney(a)=miney(a)+3*sin(mm+a)
if minex(a)<0 then
 mines(a)=1
 miney(a)=ran(512)
 minex(a)=minex(a)+640
fi
next a
return

label setup
'#####################################################
'## Initialise Screen And Variables.
'#####################################################
open window 640,512
stars=30
dim starx(stars)
dim stary(stars)
dim stars(stars)
 
for a=1 to stars
  starx(a)=ran(640)
  stary(a)=ran(512)
  stars(a)=-4-ran(3)
next a

   mine=12
   dim minex(mine):rem Mine X Pos
   dim miney(mine):rem Mine Y Pos
   dim minev(mine):rem Mine Velocity.
   dim mines(mine):rem Mine State. 1:ok <0:explode 0:dead
'#####################################################
'## Set Up The Mines.
'#####################################################
   for a=1 to mine 
      minex(a)=ran(640)
      miney(a)=ran(512)
      minev(a)=1+ran(3)
      mines(a)=1
   next a
'#######################################################
'## Player Variables.
'#######################################################
 energy=200
    score=0
     plx=50
    ply=256
    orbx=100
    orby=256
    stuck=1
    maxbul=7
    dim psx(maxbul)
    dim psy(maxbul)
    dim pss(maxbul)
    dim osx(maxbul)
    dim osy(maxbul)
    dim oss(maxbul)
    firedelay=10
return


