remstart
   ==============================================================
   =  Title  : Hi Score Example 2
   =  Author : Latch Grapple
   =  Date   : 06/06/2008
   =  Update :
   =  Version:
   ==============================================================
   Comments
   ==============================================================
remend
 
rem =============================================================
rem = SET UP DISPLAY
rem =============================================================
   autocam off
   set display mode 800,600,32
   sync on
   sync rate 60
   hide mouse
   randomize timer()
 
rem =============================================================
rem = MAIN
rem =============================================================
_main:
   gosub _init
   do
      gosub _scroll_matrix
      gosub _roll_cylinder
 
      sync
   loop
 
   end
 
rem =============================================================
rem = SUBROUTINES - PROCEDURES
rem =============================================================
   _init:
      rem create the player score image
      cls 0
      ink rgb(255,255,255),0
      set text font "arial bold",1
      set text size 20
      sync
 
      dim name$(10)
      dim score(10)
      for n=10 to 1 step -1
         name$(n)="Player "+str$(n)
         score(n)=10000*(11-n)
         text 0,(10-n)*20,name$(n)
         text 80,(10-n)*20,str$(score(n))
      next n
      plwd=text width(name$(1))
      plwd=plwd+(80-plwd)
      textwd=text width(str$(score(1)))+plwd
      textht=10*20
 
      get image 1,0,0,textwd+1,(textht)+1
      sync
 
      rem texture a plain to rotate the image
      make object plain 2,textwd,textht
      zdist#=(screen height()/2.0)/(tan(31))
      position object 2,0,0,zdist#
      texture object 2,1
      zrotate object 2,90
      position camera 0,0,0
      get image 3,(object screen x(2))-(object size y(2)/2.0),object screen y(2)-((object size x(2)/2.0)-11),(object screen x(2))+object size y(2)/2.0,object screen y(2)+(object size x(2)/2.0)
      sync
 
      rem get an image to texture cylinder so we can see it roll
      `box 5,5,10,10
      `get image 2,0,0,16,16
 
      rem create rolling object
      make object cylinder 1,100
      zrotate object 1,270
      fix object pivot 1
      scale object 1,98,50,50
      texture object 1,3
      `ghost object on 1
      `set object 1,1,1,0
 
      rem initialize matrix related variables
      matx#=1000
      matz#=1000
      tilex=13
      tilez=13
      xpertile#=matx#/tilex
      zpertile#=matx#/tilez
 
      rem create matrix
      gosub _ground
      make matrix 1,matx#,matz#,tilex,tilez
      randomize matrix 1,50
      prepare matrix texture 1,4,1,1
 
      rem place cylinder in the middle of the matrix
      position object 1,matx#/2,get ground height(1,matx#/2,matz#/2)+50,matz#/2
 
      rem cleanup a bit
      delete object 2
      delete image 1
 
      gosub _lighting
 
      position camera matx#/2,100,430
      xrotate camera 30
   return
`----------------------------------------------------------------
   _scroll_matrix:
      inc matposz#,1
      rem check if we are at the end of the matrix
      if matposz# >= zpertile#
         matposz#=-1
         shift matrix down 1
         position matrix 1,0,0,0
      endif
      position matrix 1,matrix position x(1),matrix position y(1),matposz#
      update matrix 1
   return
`----------------------------------------------------------------
   _roll_cylinder:
      rem get the ground heights for the sides and center of cylinder
      lefty#=get ground height(1,object position x(1)-50,object position z(1)-20-matposz#)
      righty#=get ground height(1,object position x(1)+50,object position z(1)+20-matposz#)
      centery#=get ground height(1,object position x(1),object position z(1)-matposz#)+30
 
      rem let's make the cyl rotate forward so it looks like it's rolling
      xang#=wrapvalue(xang#-1.5)
      xrotate object 1,xang#
 
      rem pivot and raise cylinder
      zang#=wrapvalue(lefty#-righty#)
      zangadj#=curveangle(zang#,object angle z(1),20)
      zrotate object 1,wrapvalue(zangadj#)
      ypos#=curvevalue(centery#,object position y(1),10)
      position object 1,object position x(1),ypos#,object position z(1)
 
   return
`----------------------------------------------------------------
   _lighting:
      set ambient light 70
      color ambient light 0
      set directional light 0,1,-.5,1
      calc_mat_normals(1,tilex,tilez,xpertile#/6,zpertile#/6)
 
      set object specular 1,rgb(255,255,255),20
 
   return
 
`----------------------------------------------------------------
   _ground:
      create bitmap 1,150,150
      ink rgb(10,100,10),0
      box 0,0,100,100
      for n=1 to 10
         blur bitmap 1,3
      next n
      get image 4,0,0,112,112
      sync
      delete bitmap 1
   return
rem =============================================================
rem = FUNCTIONS
rem =============================================================
   function calc_mat_normals(mat,tilex,tilez,sizex#,sizez#)
   Rem By Lee Bamber From DB Example - Adds shaded areas to matrix to give depth
   rem added tile and tile size factor for normal depth adjustment - latch
      for z=1 to tilez
         for x=1 to tilex
 
            rem Get matrix heights
            h8#=get matrix height(mat,x,z-1)
            h4#=get matrix height(mat,x-1,z)
            h#=get matrix height(mat,x,z)
            h2#=get matrix height(mat,x-1,z-1)
 
            rem Calculate projected angle X using heights
            x1#=(x-1)*sizex# : y1#=h#
            x2#=(x+0)*sizex# : y2#=h4#
            dx#=x2#-x1#
            dy#=y2#-y1#
            ax#=atanfull(dx#,dy#)
            ax#=wrapvalue(90-ax#)
 
            rem Calculate projected angle Z using heights
            z1#=(z-1)*sizez# : y1#=h2#
            z2#=(z+0)*sizez# : y2#=h8#
            dz#=z2#-z1#
            dy#=y2#-y1#
            az#=atanfull(dz#,dy#)
            az#=wrapvalue(90-az#)
 
            rem Make normal from projected angle
            nx#=sin(ax#)
            ny#=cos(ax#)
            nz#=sin(az#)
 
            rem Setting matrix normal for smoothness
            set matrix normal mat,x,z,nx#,ny#,nz#
 
         next x
      next z
      update matrix mat
   endfunction
rem =============================================================
rem = DATA STATEMENTS
rem =============================================================