SYNC ON
SYNC RATE 60
SET DISPLAY MODE 800,600,32
HIDE MOUSE
 
`Make the grass
CREATE BITMAP 1,128,128
SET CURRENT BITMAP 1
FOR I=1 TO 16
   FOR J=1 TO 16
      IF RND(1)=1
         INK RGB(255,0,0),0
      ELSE
         INK RGB(255,255,0),0
      ENDIF
      BOX ((J*8)-8),((I*8)-8),(J*8),(I*8)
   NEXT J
NEXT I
BLUR BITMAP 1,6
GET IMAGE 1,1,1,128,128
SET CURRENT BITMAP 0
DELETE BITMAP 1
 
`Set gun holding-limb position
gunOffsetX AS FLOAT = 15.0
gunOffsetY AS FLOAT = -20.0
gunOffsetZ AS FLOAT = 20.0
 
`Make main player object and hide it
MAKE OBJECT CUBE 1,10
HIDE OBJECT 1
`Attach a limb and delete the mesh
MAKE MESH FROM OBJECT 1,1
ADD LIMB 1,1,1
DELETE MESH 1
 
`Make a weapon and attach it to the limb
MAKE OBJECT BOX 2,10,10,100
`MAKE OBJECT BOX 3,10,10,100
GLUE OBJECT TO LIMB 2,1,1
`LINK LIMB 3,1,2
 
CREATE BITMAP 1,128,128
SET CURRENT BITMAP 1
FOR I=1 TO 16
   FOR J=1 TO 16
      IF RND(1)=1
         INK RGB(0,200,0),0
      ELSE
         INK RGB(0,100,0),0
      ENDIF
      BOX ((J*8)-8),((I*8)-8),(J*8),(I*8)
   NEXT J
NEXT I
GET IMAGE 1,1,1,128,128
SET CURRENT BITMAP 0
DELETE BITMAP 1
 
MAKE MATRIX 1,10000,10000,100,100
PREPARE MATRIX TEXTURE 1,1,100,100
SET MATRIX TEXTURE 1,1,1
RANDOMIZE MATRIX 1,200:UPDATE MATRIX 1
 
`Variables
xPos AS FLOAT
zPos AS FLOAT
speed AS FLOAT = 10.0
 
xLook AS FLOAT
yAng AS FLOAT
lookSpeed AS FLOAT = 0.2
 
`The gun bobbing values
gunBobSpeed AS FLOAT = 6.0
gunBobMove AS FLOAT = 3.0
gunBobHeight AS FLOAT = 1.5
 
gunBobAng AS FLOAT
gunTurnAng AS FLOAT
gunLookAng AS FLOAT
 
mouseMovementX AS FLOAT
mouseMovementY AS FLOAT
 
GOSUB camera_height
 
DO
   GOSUB gun_sway
   SYNC
LOOP
 
gun_sway:
`Store mouse movement
mouseMovementX = MOUSEMOVEX()
mouseMovementY = MOUSEMOVEY()
 
`Control movement using trig
IF UPKEY()+DOWNKEY()+LEFTKEY()+RIGHTKEY() > 0
   IF UPKEY() = 1
      INC xPos,SIN(yAng)*speed
      INC zPos,COS(yAng)*speed
   ENDIF
 
   IF DOWNKEY() = 1
      DEC xPos,SIN(yAng)*speed
      DEC zPos,COS(yAng)*speed
   ENDIF
 
   IF RIGHTKEY() = 1
      INC xPos,COS(yAng)*speed
      INC zPos,-SIN(yAng)*speed
   ENDIF
 
   IF LEFTKEY() = 1
      DEC xPos,COS(yAng)*speed
      DEC zPos,-SIN(yAng)*speed
   ENDIF
 
   `Increase gun-bobbing angle   to get gun bobbing
   gunBobAng = WRAPVALUE(gunBobAng+gunBobSpeed)
ELSE
   `Otherwise slowly change the value to nothing to bring the gun to the centre again
   gunBobAng = CURVEANGLE(0,gunBobAng,10)
ENDIF
 
`Control the gun swaying according to how much the player is turning
gunTurnAng = CURVEANGLE(WRAPVALUE(mouseMovementX),gunTurnAng,10)
gunLookAng = CURVEANGLE(WRAPVALUE(mouseMovementY),gunLookAng,10)
 
`Position the gun-holding limb and rotate it to give swaying effect
OFFSET LIMB 1,1,gunOffsetX+SIN(gunBobAng)*gunBobMove,gunOffsetY+ABS(COS(gunBobAng))*gunBobHeight,gunOffsetZ
ROTATE LIMB 1,1,gunLookAng,gunTurnAng,0
 
`Turn player
yAng = WRAPVALUE(yAng + mouseMovementX*lookSpeed)
xLook = WRAPVALUE(xLook + mouseMovementY*lookSpeed)
 
`Position player object and camera
POSITION OBJECT 1,xPos,cy#+100,zPos
ROTATE OBJECT 1,0,yAng,0
PITCH OBJECT DOWN 1,xLook
POSITION CAMERA xPos,cy#+100,zPos
ROTATE CAMERA xLook,yAng,0
 
camera_height:
  IF UPKEY()=1
    cx#=NEWXVALUE(cx#,a#,speed)
    cz#=NEWZVALUE(cz#,a#,speed)
  ENDIF
  IF DOWNKEY()=1
    cx#=NEWXVALUE(cx#,a#,-speed)
    cz#=NEWZVALUE(cz#,a#,-speed)
  ENDIF
  IF LEFTKEY()=1
    cx#=NEWXVALUE(cx#,WRAPVALUE(a#-90.0),speed)
    cz#=NEWZVALUE(cz#,WRAPVALUE(a#-90.0),speed)
  ENDIF
  IF RIGHTKEY()=1
    cx#=NEWXVALUE(cx#,WRAPVALUE(a#+90.0),speed)
    cz#=NEWZVALUE(cz#,WRAPVALUE(a#+90.0),speed)
  ENDIF
 
  cy# = GET GROUND HEIGHT(1,cx#,cz#)
RETURN