home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK2.toast / Development Kits (Disc 2) / ScriptX / Draggable ScriptX Folders / utils / DTK / Examples / Custom Classes / SmartBounce.cls < prev    next >
Encoding:
Text File  |  1995-11-30  |  2.7 KB  |  106 lines  |  [TEXT/ttxt]

  1. --------------------------- Class SmartBounce ---------------------------
  2. format debug "--  Compiling SmartBounce Class . . .\n" undefined undefined
  3.  
  4. class SmartBounce(TwoDController)
  5. inst vars
  6.     walls, friction, escapeVelocity
  7. end
  8.  
  9. function checkWall obj sb ->
  10. (
  11.      if not (isAKindof obj Projectile) do (
  12.          print "Not a Projectile" --debug
  13.          return "Not a Projectile"
  14.      )
  15.     local sceneRect := sb.walls
  16.     local friction := sb.friction
  17.     local myRect := obj.globalBoundary
  18.     local x1 := myRect.x1
  19.     local x2 := myRect.x2
  20.     local y1 := myRect.y1
  21.     local y2 := myRect.y2
  22.     local objVelocity := obj.velocity
  23.     local ovy := objVelocity.y
  24.     local ovx := objVelocity.x
  25.     if ovy = 0 and ovx = 0 do return OK -- orb is at rest
  26.     local obbox := obj.bbox
  27.     local elast := obj.elasticity
  28.     local whichWall := undefined
  29.     local mySpace := sb.space
  30.     local myPortals := mySpace.portals
  31.     
  32.     if (x1 < sceneRect.x1) do (
  33.         obj.x := sceneRect.x1 - obbox.x1
  34.         ovx := abs(ovx * elast)
  35.         whichWall := @west
  36.     )
  37.  
  38.     if (y1 < sceneRect.y1) do (
  39.         obj.y := sceneRect.y1 - obbox.y1
  40.         ovy := abs(ovy * elast)
  41.         whichWall := @north
  42.     )
  43.  
  44.     if (x2 > sceneRect.x2) do (
  45.         obj.x := sceneRect.x2 - (obj.width + obbox.x1)
  46.         ovx := -(ovx * elast)
  47.         whichWall := @east
  48.     )
  49.  
  50.     if (y2 > sceneRect.y2) do (
  51.         obj.y := sceneRect.y2 - (obj.height + obbox.y1)
  52.         ovy := -(ovy * elast)
  53.         whichWall := @south
  54.     )
  55.     
  56.     if whichWall <> undefined do (
  57.         local toScene := myPortals[whichWall]
  58.         local amplitude := (abs(ovx) + abs(ovy))
  59.         if toScene = undefined then (
  60.             makeSound obj #(@wall,amplitude)
  61.             objVelocity.x := ovx * friction
  62.             objVelocity.y := ovy * friction
  63.         ) else (
  64.             if amplitude > sb.escapeVelocity then (
  65. --                format debug "Exiting to the %*...\n" (whichWall as string) @unadorned
  66.                 local newScene := showScene mySpace.myStage toScene
  67.                 case whichWall of
  68.                     @east: obj.x := sceneRect.x1 - obbox.x1
  69.                     @north: obj.y := sceneRect.y2 - (obj.height + obbox.y1)
  70.                     @west: obj.x := sceneRect.x2 - (obj.width + obbox.x1)
  71.                     @south:obj.y := sceneRect.y1 - obbox.y1
  72.                 end
  73. --                format debug "Putting orb in %*\n" newScene @normal
  74.                 if obj.size = 2 do (
  75.                     stop obj[1]
  76.                     pop obj
  77.                 )
  78.                 prepend newScene obj
  79.                 hook obj
  80.             ) else (
  81.                 makeSound obj #(@portal,amplitude)
  82.                 objVelocity.x := ovx * friction
  83.                 objVelocity.y := ovy * friction
  84.             )
  85.         )
  86.     )
  87.     return OK
  88. )
  89.  
  90. method afterInit self {Class SmartBounce} #rest args->
  91. (
  92.     emptyOut self.protocols
  93.     append self.protocols Projectile
  94.     self.walls := self.space.bbox
  95.     self.friction := 0.95
  96.     self.escapeVelocity := 10
  97.     apply nextMethod self args
  98. )
  99. method tickle self {Class SmartBounce} myClock->
  100. (
  101. --    nextMethod self myClock
  102.     forEach self checkWall self
  103. )
  104. --------------------------- End SmartBounce ---------------------------
  105. #(SmartBounce,#("escapeVelocity"),#())
  106.