#CONSTANT CRATE_COUNT 10
#CONSTANT ARM_COUNT 6
#CONSTANT ARM_LENGTH 2.0
#CONSTANT DEFAULT_ANGLE 20.0
HALF_ARM_LENGTH# = ARM_LENGTH*0.5
 
 
 
 
 
set display mode 1280, 1024, 32
sync on
sync rate 0
autocam off
position camera 10, 10, 10
point camera 0, 0, 0
backdrop on
color backdrop 0
randomize timer()
 
 
`set directional light 0, -1, -1, -1
set point light 0, 10, 10, 10
set ambient light 0
 
`MAKE FLOOR
make object plain 2, 30, 30
position object 2, 0, 0.0, 0
point object 2, 0, 1, 0
color object 2, 0xFF77FF77
 
 
 
 
 
Type Coords
   x#
   y#
   z#
EndType
 
 
colSelected   as DWORD : colSelected   = 0x66FF0000
colUnselected as DWORD : colUnselected = 0x66FFFFFF
colHomeBox    as DWORD : colHomeBox    = 0x660000FF
colCrates     as DWORD : colCrates     = 0x66FFFF00
 
 
 
`Arm Template
make object cylinder 1, ARM_LENGTH
scale object 1, 10, 100, 10
make mesh from object 1, 1
 
`Point template
delete object 1
make object sphere 1, ARM_LENGTH * 0.2, 24, 24
make mesh from object 2, 1
 
`Hide control point because its the root point
hide limb 1, 0
`Add an arm to the control point
add limb    1, 1, 1
offset limb 1, 1, 0.0, HALF_ARM_LENGTH#, 0.0
link limb   1, 0, 1
 
`The next arms can be made in a loop
for i = 2 to ARM_COUNT*2 step 2
   `Create a new control point
   add limb    1, i, 2
   `Offset it by helf the length of a strut
   offset limb 1, i, 0.0, HALF_ARM_LENGTH#, 0.0
   `Link it to the last strut
   link limb   1, i-1, i
   `Color the joint unselected, but set the alpha to FF using BITWISE OR
   color limb 1, i, (colUnselected || 0xFF000000)
 
 
   `Add next arm
   add limb    1, i+1, 1
   `Offset it by helf the length of a strut
   offset limb 1, i+1, 0.0, HALF_ARM_LENGTH#, 0.0
   `Link it to the control point
   link limb   1, i, i+1
   `Color the arm unselected
   color limb 1, i+1, colUnselected
next i
 
i = ARM_COUNT*2+2
`Create Final point, used for pickups
add limb    1, i, 2
`Offset it by helf the length of a strut
offset limb 1, i, 0.0, HALF_ARM_LENGTH#, 0.0
`Link it to the last strut
link limb   1, i-1, i
`Color the joint unselected, but set the alpha to FF using BITWISE OR
color limb 1, i, (colUnselected || 0xFF000000)
`Enable transparency
set object transparency 1, 3
 
`Make temp object for detecting tip collisions
make object plain 3, ARM_LENGTH * 0.2, ARM_LENGTH * 0.2
glue object to limb 3, 1, i
hide object 3
 
 
`Create Pickups
G as Coords
G.x# = object size x(2) * 0.5
G.z# = object size y(2) * 0.5
 
for i = 10 to 10+CRATE_COUNT
   make object box i, 0.5+(rnd(5.0)*0.1), 0.5+(rnd(5.0)*0.1), 0.5+(rnd(5.0)*0.1)
   position object i, rnd(G.x#) - (G.x# * 0.5), object size y(i)*0.5, rnd(G.z#) - (G.z# * 0.5)
   yrotate object i, rnd(360)
   color object i, colCrates
   set object transparency i, 3
next i
 
`Make Home Crate
make object box 9, 1.5, 0.5, 1.5
position object 9, rnd(G.x#) - (G.x# * 0.5), 0.25, rnd(G.z#) - (G.z# * 0.5)
color object 9, colHomeBox
set object transparency 9, 3
 
 
 
 
 
`Declare some variables for control.
limbSelected = 0
mouseStatus = 0
 
`Color the selected limb in
color limb 1, limbSelected+1, colSelected
 
 
 
`LimbAngles
Dim LimbAngles(ARM_COUNT*2) as Coords
Dim TargetLimbAngles(ARM_COUNT*2) as Coords
 
for i = 0 to ARM_COUNT*2 step 2
   LimbAngles(i).x# = 0.0
   LimbAngles(i).y# = 0.0
   LimbAngles(i).z# = 0.0
 
   TargetLimbAngles(i).x# = 0.0
   TargetLimbAngles(i).y# = 0.0
   TargetLimbAngles(i).z# = DEFAULT_ANGLE
next i
`Exclusion - make sure the root strut is vertical
TargetLimbAngles(0).z# = 0.0
 
 
`Currently holding variable.. -1 if nothing, >=10 if holding
pickedUp = -1
CratesPutAway = 0
 
 
`Setup Arm Cam
make camera 1
set camera view 1, screen width() * 0.5, 0, screen width(), screen height() * .15
set camera aspect 1, (screen width() * 0.5) / (screen height() * .15)
color backdrop 1, 0
set current camera 0
 
 
frameTime# = 1.0
startTime = timer()
do
   frameTime# = (frameTime# * 0.8) + ((timer() - startTime) * 0.2)
   startTime = timer()
   text 10, 10, "FPS: " + str$(screen fps())
   text 10, 30, "SCORE: " + str$(CratesPutAway)
 
   `position arm cam
   camLimb = ARM_COUNT*2     : position camera 1, limb position x(1, camLimb), limb position y(1, camLimb) + 3.0, limb position z(1, camLimb)
   camLimb = ARM_COUNT*2 + 2 : point camera 1, limb position x(1, camLimb), limb position y(1, camLimb), limb position z(1, camLimb)
 
 
 
   `ALL ONCE-PER-CLICK STUFF GOES IN HERE
   if mouseclick() <> mouseStatus
      mouseStatus = mouseclick()
      `Clear the mousemovement functions so the arms dont initially jump (ie movement AFTER the click is considered, not pre-click).
      dx# = mousemovex()
      dy# = mousemovey()
 
      if mouseStatus && %0010
         `Right mouse button
         if pickedUp = -1
            collVal = object collision(3,0)
            if collVal > 9
               pickedUp = collVal
               position object pickedUp, 0, 0, 0
               glue object to limb pickedUp, 1, ARM_COUNT*2+2
            endif
         else
            unglue object pickedUp
            pickedUp = -1
         endif
      endif
   endif
 
 
 
   `Check all objects are either on the floor or removed from being "put away"
   for i = 10 to 10 + CRATE_COUNT
      if object exist(i) AND i <> pickedUp
         `Show score
         strScore$ = left$(str$(object size(i)*100.0), 3)
         if mid$(strScore$, len(strScore$)) = "." then strScore$ = left$(strScore$, len(strScore$)-1)
 
         center text object screen x(i), object screen y(i), strScore$
         if object collision(i, 2) = 0
            position object i, object position x(i), object position y(i) - frameTime#*0.001, object position z(i)
            if object collision(i, 9) then  inc CratesPutAway, val(left$(str$(object size(i)*100.0), 3)) : delete object i
         endif
      endif
   next i
 
 
 
   `CHECK MOUSE WHEEL FOR STRUT CHANGE
   dz# = mousemovez()
   if dz# > 0
      color limb 1, limbSelected+1, colUnselected
      inc limbSelected, 2
      if limb exist(1, limbSelected+1) = 0 then limbSelected = 0
      color limb 1, limbSelected+1, colSelected
   else
      if dz# < 0
         color limb 1, limbSelected+1, colUnselected
         dec limbSelected, 2
         if limbSelected < 0 then limbSelected = ARM_COUNT*2
         color limb 1, limbSelected+1, colSelected
      endif
   endif
 
 
   `THIS IS TO BE DONE WHILE EITHER LEFT OR RIGHT BUTTON IS HELD
   if mouseStatus && %0011
      if limbSelected = 0
         dx# = mousemovex()
         inc TargetLimbAngles(limbSelected).y#, dx#
      else
         dy# = mousemovey()
         inc TargetLimbAngles(limbSelected).z#, dy#
 
         `Set limb ranges
         maxAngle# =  120.0
         minAngle# = -120.0
 
 
         `Check for limits
         if TargetLimbAngles(limbSelected).z# > maxAngle#
            TargetLimbAngles(limbSelected).z# = maxAngle#
         else
            if TargetLimbAngles(limbSelected).z# < minAngle#
               TargetLimbAngles(limbSelected).z# = minAngle#
            endif
         endif
 
      endif
   endif
 
 
   `LOOP THROUGH ALL JOINTS AND CURVE ANGLE TO THEM
   for i = 0 to ARM_COUNT*2 step 2
      LimbAngles(i).x# = curvevalue(TargetLimbAngles(i).x#, LimbAngles(i).x#, 100.0 / frameTime#)
      LimbAngles(i).y# = curvevalue(TargetLimbAngles(i).y#, LimbAngles(i).y#, 100.0 / frameTime#)
      LimbAngles(i).z# = curvevalue(TargetLimbAngles(i).z#, LimbAngles(i).z#, 100.0 / frameTime#)
 
      rotate limb 1, i, LimbAngles(i).x#, LimbAngles(i).y#, LimbAngles(i).z#
   next i
 
   sync
loop