Pseudo Physic Engine — File Browser

PseudoPhysicEnginev1.0 / PseudoPhysicEnginev1.0 / Pseudo physic / PseudoPhysic.mrc

PseudoPhysic.mrc — 26.71 KB — Download this file

;;;;;;;;;;;;
;
;
;      ...........
;      .         .
;      .      |¯¯.¯¯¯¯¯¯|
;      .      |  .      |
;      .......|...      |
;             |_________|
;
;
;      Pseudo physic engine,
;      by visionz (g_baril@sympatico.ca)
;
;
;
;
;                                              ;
;                                              ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
menu menubar,status,channel {
  -
  PseudoPhysic
  .Open
  ..Empty projet { pseudophysic }
  ..-
  ..Template
  ...$submenu($_cb.TemplateList($1))
  .-
  .Unload
  ..Are you sure?
  ...Yes { .unload -rs $shortfn($script) }
}


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; [environment file]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
alias PseudoPhysic {

  var %win = @Physics, %w = 500, %h = 560
  var %env = $1-

  _cb.initSystem %win %w %h %env

  .timer_cb 0 0 _cb.loop %win %w %h
}


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; [environment file]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
alias _cb.initSystem {

  var %win = $1
  var %env = $shortfn($+($scriptdir,Env\,$4-))

  window -c %win
  window -Cdofpk0w +ft %win 0 0 $2 $3

  if ($hget(_cb)) hfree _cb
  hmake _cb 1000

  drawpic -c

  if ($isFile(%env)) {

    hload -i _cb %env
  }
  else {

    hadd _cb _keyrpt $false
    hadd _cb _BlockCol 1
    hadd _cb _MouseVelMult 1
    hadd _cb _ShowPopUp 0
    hadd _cb _ShowID 0
    hadd _cb _ResizeGrip 0
    hadd _cb _SizingId 0
    hadd _cb _SizingRect 0
    hadd _cb _BGColor $rgb(0,0,0)

    ; pour le calcul du fps
    hadd _cb fps 0
    hadd _cb tempDebut $ticks
    hadd _cb frameCount 0

    ; ennemis
    hadd _cb Object.Count 0
    hadd _cb Object.NextId 1

    ;                 id w   h   f   texture
    Object.Type.Create 1 010 150 2.0 woodtile2.bmp
    Object.Type.Create 2 150 010 2.0 woodtile2.bmp
    Object.Type.Create 3 005 005 2.0 woodtile2.bmp
    Object.Type.Create 4 010 010 2.0 woodtile2.bmp
    Object.Type.Create 5 025 025 2.0 woodtile2.bmp
    Object.Type.Create 6 050 050 2.0 woodtile2.bmp
    Object.Type.Create 7 100 100 2.0 woodtile2.bmp
  }
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
alias Object.GetFreeID {

  var %nextID = $base($hget(_cb,Object.NextID),10,10,4)
  var %ObjectList = $hget(_cb,Object.List)

  while ($istok(%ObjectList,%nextID,58)) {

    %nextID = $calc(%nextID + 1)
    if (%nextID > 9999) {

      %nextID = 1
    }

    %nextID = $base(%nextID,10,10,4)
  }

  hadd _cb Object.NextID $calc(%NextID + 1)

  return %NextID
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; iteration_count
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
alias _cb.TemplateList {

  var %iteration = $1
  var %menuItem
  var %file

  if ((%iteration != begin) && (%iteration != end)) {

    %file = $findfile($scriptdirEnv\,*.env,%iteration)
    %menuItem = $nopath(%file) $+ : $+ PseudoPhysic $nopath(%file)
  }

  return %menuItem
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; id width height friction img
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
alias Object.Type.Create {

  var %id = $1
  var %Width = $2
  var %Height = $3
  var %friction = $4
  var %img = $5-

  hadd _cb $+(et,%id) %id
  hadd _cb $+(et,%id,w) %Width
  hadd _cb $+(et,%id,h) %Height
  hadd _cb $+(et,%id,friction) %friction
  hadd _cb $+(et,%id,img) %img
}


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; @win width height
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
alias _cb.loop {

  drawrect -nrf $1 $hget(_cb,_BGColor) 1 0 0 $2 $3

  _cb.render $1 $2 $3

  hinc _cb frameCount

  if ($calc($ticks - $hget(_cb,tempsDebut)) > 1000) {
    hadd _cb fps $hget(_cb,frameCount)
    hadd _cb frameCount 0
    hadd _cb tempsDebut $ticks
  }

  drawtext -nr $1 $rgb(255,0,0) arial 8 0 0 $hget(_cb,fps)
  ; drawtext -nr $1 $rgb(255,0,0) arial 10 0 10 Object count: $+ $hget(_cb,Object.Count)

  drawdot $1
}


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; @win
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
alias _cb.render {

  var %win = $1
  var %mouse.x = $hget(_cb,mouse.x)
  var %mouse.y = $hget(_cb,mouse.y)
  var %BlockCol = $hget(_cb,_BlockCol)
  var %showID = $hgeT(_cb,_ShowID)
  var %resizeGrip = $hget(_cb,_resizeGrip)
  var %IsSizing =  $hget(_cb,_SizingID)
  var %ObjectList = $hget(_cb,Object.List)
  var %ObjectLen = $numtok(%ObjectList,58)
  var %ObjectId = 0
  var %background = $hget(_cb,_Background)

  if (%background) {
    drawpic -csnl %win 0 0 500 560 $ShortFn($+($scriptdir,Textures\,%background))
  }

  var %i = 1
  while (%i <= %ObjectLen) {

    %ObjectId = $gettok(%ObjectList,%i,58)

    var %x = $hget(_cb,$+(%ObjectId,x))
    var %y = $hget(_cb,$+(%ObjectId,y))
    var %w = $hget(_cb,$+(%ObjectId,w))
    var %h = $hget(_cb,$+(%ObjectId,h))
    var %TypeId = $hget(_cb,$+(%ObjectId,TypeId))
    var %hook = $hget(_cb,$+(%ObjectId,hook))

    var %fill = $hget(_cb,$+(%ObjectId,fill))
    if (%fill == 1) {

      drawrect -rfn %win $rgb($calc(%typeId * 25 + 50),$calc(%hook * 255),0) 1 %x %y %w %h
      drawrect -rn %win $rgb(255,255,255) 1 %x %y %w %h
    }
    elseif (%fill == 2) {
      if ($hget(_cb,$+(%ObjectID,Trans))) {
        drawpic -clsnt %win $hget(_cb,$+(%ObjectID,TransCol)) %x %y %w %h $Shortfn($+($Scriptdir,Textures\,$hget(_cb,$+(%Objectid,img))))
      }
      else {
        drawpic -clsn %win %x %y %w %h $Shortfn($+($Scriptdir,Textures\,$hget(_cb,$+(%Objectid,img))))
      }
      if (%hook) {

        drawrect -rifn %win $rgb(0,0,0) 1 %x %y %w %h
      }
    }
    elseif (%fill == 0) {

      drawrect -rn %win $rgb(255,255,255) 1 %x %y %w %h
    }

    if (%showId) {

      drawrect -rfn %win $rgb(32,64,98) 1 $calc(%x + 3) $calc(%y + 3) 35 15
      drawrect -rfn %win $rgb(64,128,196) 1 $calc(%x + 2) $calc(%y + 2) 35 15

      drawtext -rn %win $rgb(0,0,0) arial 12 $calc(%x + 5) $calc(%y + 3) %ObjectId
      drawtext -rn %win $rgb(255,255,255) arial 12 $calc(%x + 4) $calc(%y + 2) %ObjectId
    }

    if (%resizeGrip) {

      drawrect -rn %win $rgb(255,255,255) 1 $calc(%x - 3) $calc(%y - 3) 6 6 $calc(%x + %w - 3) $calc(%y - 3) 6 6
      drawrect -rn %win $rgb(255,255,255) 1 $calc(%x - 3) $calc(%y + %h - 3) 6 6 $calc(%x + %w - 3) $calc(%y + %h - 3) 6 6
    }

    if ((%Hook == 0) && (%IsSizing == 0)) {

      Object.ApplyPhysics %ObjectId %x %y %w %h %BlockCol
    }

    inc %i 
  }

  if ($hget(_cb,_ShowPopUp)) {
    var %mouseOverId = $hget(_cb,_MouseOver)
    if (%mouseOverId) {
      _cb.popupInfos %win %mouseOverId %mouse.x %mouse.y
    }
  }
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; win id x y
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
alias _cb.PopupInfos {

  var %win = $1
  var %id = $2
  var %x = $3, %y = $4

  drawrect -rnif %win $rgb(0,0,0) 1 %x %y 100 50

  drawrect -rn %win $rgb(255,255,255) 1 %x %y 100 50
  drawrect -rin %win $rgb(0,0,0) 1 $calc(%x +1) $calc(%y +1) 100 50

  drawtext -rn %win $rgb(0,0,0) arial 10 $calc(%x + 50) $calc(%y + 3) ID: %id
  drawtext -rn %win $rgb(255,255,255) arial 10 $calc(%x + 49) $calc(%y + 2) ID: %id

  drawtext -rn %win $rgb(0,0,0) arial 10 $calc(%x + 14) $calc(%y + 13) V velocity: $Object.Get(%id).vVel
  drawtext -rn %win $rgb(255,255,255) arial 10 $calc(%x + 13) $calc(%y + 12) V velocity: $Object.Get(%id).vVel

  drawtext -rn %win $rgb(0,0,0) arial 10 $calc(%x + 14) $calc(%y + 23) H velocity: $Object.Get(%id).hVel
  drawtext -rn %win $rgb(255,255,255) arial 10 $calc(%x + 13) $calc(%y + 22) H velocity: $Object.Get(%id).hVel

  drawtext -rn %win $rgb(0,0,0) arial 10 $calc(%x + 14) $calc(%y + 33) B. factor: $Object.Get(%id).friction
  drawtext -rn %win $rgb(255,255,255) arial 10 $calc(%x + 13) $calc(%y + 32) B. factor: $Object.Get(%id).friction
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ID x y w h verify_block_collision
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
alias Object.ApplyPhysics {

  var %id = $1
  var %x = $2
  var %y = $3
  var %w = $4
  var %h = $5
  var %Gh = $hget(_cb,$+(%id,gh))
  var %vVel = $hget(_cb,$+(%id,vVel))
  var %hVel = $hget(_cb,$+(%id,hVel))
  var %friction = $hget(_cb,$+(%id,Friction))
  var %VerifyBlockCollision = $6
  var %inBounds = 1
  var %right = $calc(%x + %w)
  var %bottom = $calc(%y + %h)
  var %onGround = 0

  if (%VerifyBlockCollision) {

    var %newvVel = %vVel
    var %newhVel = %hVel

    var %ObjectList = $hget(_cb,Object.List)
    var %ObjectLen = $numtok(%ObjectList,58)
    var %ObjectId = 0

    ;  var %i = $hget(_cb,_FirstColBlock)
    var %i = 1
    while (%i <= %ObjectLen) {

      %ObjectId = $gettok(%ObjectList,%i,58)
      if (%ObjectId != %id) {

        var %check.x = $hget(_cb,$+(%ObjectId,x))
        var %check.y = $hget(_cb,$+(%ObjectId,y))
        var %check.w = $hget(_cb,$+(%ObjectId,w))
        var %check.h = $hget(_cb,$+(%ObjectId,h))
        var %check.r = $calc(%check.w + %check.x)
        var %check.b = $calc(%check.h + %check.y)

        if ((%check.x <= %right) && (%check.r >= %x) && (%check.y <= %bottom) && (%check.b >= %y)) {

          var %int.x = %x
          if (%check.x > %x) {
            %int.x = %check.x
          }

          var %int.y = %y
          if (%check.y > %y) {
            %int.y = %check.y
          }

          var %int.w = $calc(%right - %int.x)
          if (%check.r < %right) {
            %int.w = $calc(%check.r - %int.x)
          }

          var %int.h = %bottom - %int.y
          if (%check.b < %bottom) {
            %int.h = $calc(%check.b - %int.y)
          }

          if (%int.w > %int.h) {

            %NewVVel = $calc((-1 * %vVel) / %friction)
            %NewHVel = $calc(%NewHVel / %friction)

            if ($abs(%NewVVel) < 1.5) {
              %NewVVel = 0
            }

            %hVel = $calc(%hVel / %friction)

            if ((%bottom > %check.b) || (%y > %check.y)) {

              %check.y = $calc(%y - %check.h )
              ; if (%NewVVel == 0) %NewVVel = 1
            }
            else {

              %y = $calc(%check.y - %h)
            }
          }
          else {

            %NewHVel = $calc((-1 * %hVel) / %friction)

            ; pour que les blocks collent moins les uns sur les autres verticalement 
            ;  if ($abs(%NewHVel) < 1.5) %NewHVel = 0

            %NewVVel = $calc(%NewVVel / %friction)

            if (%x > %check.x) {
              ; %check.x = $calc(%x - %check.w - 1)
              %x = $calc(%check.r + 0)
            }
            else {
              %x = $calc(%check.x - %w - 0)
            }
          }
        }

        hadd _cb $+(%Objectid,x) %check.x
        hadd _cb $+(%Objectid,y) %check.y
      }

      inc %i 
    }

    %hVel = %newHVel
    %vVel = %newVVel
  }

  if (%onGround == 0) {

    inc %y %vVel
    inc %x %hVel

    inc %vVel 0.5

    if (%x < 0) {
      %x = 0
      %hVel = $calc((%hVel * -1) / %friction)

      %inBounds = 0
    }

    if (%x > $calc(500 - %w)) {
      %x = $calc(500 - %w)
      %hVel = $calc((%hVel * -1) / %friction)

      %inBounds = 0
    }

    if (%y < 0) {
      %y = 0
      %vVel = $calc((%vVel * -1) / %friction)

      if (%vVel < 0) %vVel = 0
      ;      echo -a vVel = %vVel
      %inBounds = 0
    }

    if (%y > $calc(%gh - %h)) {

      if (%hVel > 0) {
        dec %hVel 0.6
        if (%hVel < 0) %hVel = 0
      }

      if (%hVel < 0) {
        inc %hVel 0.6
        if (%hVel > 0) %hVel = 0
      }

      %y = $calc(%gh - %h)
      %vVel = $calc((%vVel * -1) / %friction)
      if ($abs(%vVel) < 1.25) {
        %vVel = 0
        %onGround = 1
      }

      %inBounds = 0
    }
  }

  hadd _cb $+(%id,x) %x
  hadd _cb $+(%id,y) %y

  hadd _cb $+(%id,vVel) %vVel
  hadd _cb $+(%id,hVel) %hVel

  return %inBounds 
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; typeId x y
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
alias Object.Create {

  var %id = 0
  var %ObjectList = $hget(_cb,Object.List)

  var %typeId = $1
  var %x = $2
  var %y = $3

  %id = $Object.GetFreeID
  if (%id > 0) {

    hadd _cb $+(%id,typeid) %TypeId
    hadd _cb $+(%id,x) %x
    hadd _cb $+(%id,y) %y
    hadd _cb $+(%id,Gh) 560
    hadd _cb $+(%id,Hook) 0
    hadd _cb $+(%id,vVel) 0
    hadd _cb $+(%id,hVel) 0
    hadd _cb $+(%id,fill) 2
    hadd _cb $+(%id,Coll) 1
    hadd _cb $+(%id,trans) 0

    hadd _cb $+(%id,Friction) $Object.Type.Get(%typeId).Friction
    hadd _cb $+(%id,w) $Object.Type.Get(%typeId).w
    hadd _cb $+(%id,h) $Object.Type.Get(%typeId).h
    hadd _cb $+(%id,img) $Object.Type.Get(%typeId).img

    %ObjectList = $+(%ObjectList,%id,:)
    hadd _cb Object.List %ObjectList

    hinc _cb Object.Count
  }

  return %id
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; id
; prop = img x y friction
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
alias Object.Get {

  var %Id = $1

  return $hget(_cb,$+($base(%Id,10,10,4),$prop))
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; typeid
; prop = img friction w h
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
alias Object.Type.Get {

  var %typeId = $1

  return $hget(_cb,$+(et,%typeId,$prop))
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; keycode
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
alias _cb.getKeyState {
  return $hget(_cb,$+(keydown.,$1))
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
on *:keyup:@Physics:*:{
  hadd _cb $+(keydown.,$keyval) 0
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
on *:keydown:@Physics:*:{

  hadd _cb _keyrpt $keyrpt
  hadd _cb $+(keydown.,$keyval) 1
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
on *:close:@Physics:{
  .timer_cb off
  hfree _cb
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; id rect_id mouse.x mouse.y
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
alias Object.Resize {

  var %SizingID = $1
  var %SizingRect = $2
  var %mouse.x = $3
  var %mouse.y = $4

  var %x = $Object.Get(%sizingId).x
  var %y = $Object.Get(%sizingId).y
  var %w = $Object.Get(%sizingId).w
  var %h = $Object.Get(%sizingId).h

  var %NewX = %x
  var %NewY = %y
  var %NewW = %w
  var %NewH = %h

  if (%SizingRect == 1) {
    %NewX = %mouse.x
    %NewY = %mouse.y
    %NewW = $calc(%w + %x - %mouse.x)
    %NewH = $calc(%h + %y - %mouse.y)
  }
  elseif (%SizingRect == 3) {
    %NewY = %mouse.y
    %NewH = $calc(%h + %y - %mouse.y)
    %newW = $calc(%mouse.x - %x)
  }
  elseif (%sizingRect == 5) {
    %newW = $calc(%mouse.x - %x)
    %newH = $calc(%mouse.y - %y)
  }
  elseif (%SizingRect == 7) {
    %NewX = %mouse.x
    %NewW = $calc(%w + %x - %mouse.x)
    %newH = $calc(%mouse.y - %y)
  }

  if (%newW < 5) %newW = 5
  if (%newH < 5) %newH = 5

  hadd _cb $+(%SizingID,x) $int(%newX)
  hadd _cb $+(%SizingID,y) $int(%newY)
  hadd _cb $+(%SizingID,w) $int(%newW)
  hadd _cb $+(%SizingID,h) $int(%newH)
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; id
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
alias Object.Delete {

  var %id = $base($1,10,10,4)
  var %ObjectList = $hget(_cb,Object.List)
  var %newObjectList = $remove(%ObjectList,$+(%id,:))

  if (%ObjectList != %newObjectList) {
    hadd _cb Object.List %newObjectList
    hdel -w _cb $+(%id,*)
    hdec _cb Object.Count
  }
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; [-v] id prop [min][max] value
; [min][max] only if -v is set
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
alias Object.Set {

  var %id, %prop, %value, %min, %max

  if ($1 == -v) {
    %id = $2
    %prop = $3
    %min = $4
    %max = $5
    %value = $6-

    if (%value < %min) %value = %min
    if (%value > %max) %value = %max
  }
  else {
    %id = $1
    %prop = $2
    %value = $3-
  }

  hadd _cb $+(%id,%prop) %value
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; mouse.x mouse.y
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
alias _GetContextMenuBlockId {

  var %mouse.x = $1, %mouse.y = $2
  var %ObjectId = $Object.IDByCoor(%mouse.x, %mouse.y)

  hadd _cb _ContextMenuId %ObjectId

  return %ObjectId
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; mouse.x mouse.y
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
alias Object.IDByCoor {

  var %ObjectList = $hget(_cb,Object.List)
  var %ObjectLen = $numtok(%ObjectList,58)

  var %ObjectId = 0
  var %srcX = $1, %srcY = $2
  var %IDFound = 0

  var %i = 1
  while (%i <= %ObjectLen) {

    %ObjectId = $gettok(%ObjectList,%i,58)

    var %x = $Object.Get(%ObjectId).x
    var %y = $Object.Get(%ObjectId).y
    var %w = $Object.Get(%ObjectId).w
    var %h = $Object.Get(%ObjectId).h

    if ($inrect(%srcX,%srcY,%x,%y,%w,%h)) {

      %IDFound = %ObjectId
    }

    inc %i 
  }

  return %IDFound
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; iteration_count
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
alias _cb.GenerateObjectList {

  var %iteration = $1
  var %menuItem
  var %ObjectList
  var %ObjectId = 0
  var %ObjectCount = 0

  if (%iteration != end) {
    if (%iteration == begin) {
      %MenuItem = -
    }
    else {

      %ObjectList = $hget(_cb,Object.List)
      %ObjectCount = $numtok(%ObjectList,58)

      if ($1 <= %ObjectCount) {
        %ObjectId = $gettok(%ObjectList,%iteration,58)
        %MenuItem = %ObjectId $+ : $+ Object.Delete %ObjectID
      }
    }
  }

  return %menuItem
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; x y
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
alias Object.OnSizingGrip {

  var %ObjectList = $hget(_cb,Object.List)
  var %ObjectLen = $numtok(%ObjectList,58)
  var %ObjectId = 0
  var %srcX = $1, %srcY = $2
  var %IDFound = 0

  var %i = 1
  while (%i <= %ObjectLen) {

    %ObjectId = $gettok(%ObjectList,%i,58)

    var %x = $Object.Get(%ObjectId).x
    var %y = $Object.Get(%ObjectId).y
    var %w = $Object.Get(%ObjectId).w
    var %h = $Object.Get(%ObjectId).h

    if ($inrect(%srcX,%srcY,$calc(%x - 3),$calc(%y - 3),6,6)) {
      %IDFound = %ObjectId 1
    }
    elseif ($inrect(%srcX,%srcY,$calc(%x + %w - 3),$calc(%y - 3),6,6)) {
      %IDFound = %ObjectId 3
    }
    elseif ($inrect(%srcX,%srcY,$calc(%x + %w - 3),$calc(%y + %h - 3),6,6)) {
      %IDFound = %ObjectId 5
    }
    elseif ($inrect(%srcX,%srcY,$calc(%x - 3),$calc(%y + %h - 3),6,6)) {
      %IDFound = %ObjectId 7
    }

    inc %i 
  }

  return %IDFound
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
menu @Physics {
  mouse {

    var %sizingID = $hget(_cb,_SizingID)
    if (%sizingID) {

      var %sizingRect = $hgeT(_cb,_SizingRect)
      Object.Resize %sizingID %sizingRect $mouse.x $mouse.y
    }

    var %HookId = $hgeT(_cb,HookId)
    if (%HookId) {

      var %x = $calc($mouse.x - $hget(_cb,HookRelX))
      var %y = $calc($mouse.y - $hget(_cb,HookRelY))
      var %w = $Object.Get(%HookId).w
      var %h = $Object.Get(%HookId).h
      var %mouseVelMult = $hgeT(_cb,_MouseVelMult)

      hadd _cb $+(%HookId,x) %x
      hadd _cb $+(%HookId,y) %y

      hadd _cb _MouseVelX $calc(($mouse.x - $hgeT(_cb,_lastMouseX)) * %mouseVelMult)
      hadd _cb _MouseVelY $calc(($mouse.y - $hgeT(_cb,_lastMouseY)) * %mouseVelMult)

      hadd _cb _LastMouseX $mouse.x
      hadd _cb _LastMouseY $mouse.y
    }
    else {

      var %ObjectId = $Object.IDByCoor($mouse.x, $mouse.y)
      if (%ObjectId) {
        hadd _cb _MouseOver %ObjectId
      }
      else {
        hadd _cb _MouseOver 0
      }
    }

    hadd _cb mouse.x $mouse.x
    hadd _cb mouse.y $mouse.y
  }

  sclick {

    var %sizing = $Object.OnSizingGrip($mouse.x,$mouse.y)
    if (%sizing && $hget(_cb,_resizeGrip)) {

      hadd _cb _SizingID $gettok(%sizing,1,32)
      hadd _cb _SizingRect $gettok(%sizing,2,32)
    }
    else {

      var %ObjectId = $Object.IDByCoor($mouse.x, $mouse.y)
      if (%ObjectId) {

        hadd _cb $+(%ObjectID,hook) 1

        hadd _cb HookId %ObjectId

        hadd _cb HookRelX $calc($mouse.x - $Object.Get(%ObjectId).X)
        hadd _cb HookRelY $calc($mouse.y - $Object.Get(%ObjectId).Y)

        hadd _cb _LastMouseX $mouse.x
        hadd _cb _LastMouseY $mouse.y
      }
    }
  }

  uclick {

    var %HookId = $hgeT(_cb,HookId)

    if (%HookId) {

      hadd _cb $+(%HookId,hVel) $hgeT(_cb,_MouseVelX)
      hadd _cb $+(%HookId,vVel) $hgeT(_cb,_MouseVelY)

      hadd _cb $+(%HookId,hook) 0
      hadd _cb HookId 0
    }

    hadd _cb _SizingID 0
    hadd _cb _SizingRect 0
    ;   hadd _cb _ContextMenuID 0
  }

  leave {

    var %HookId = $hgeT(_cb,HookId)

    if (%HookId) {
      hadd _cb $+(%HookId,hook) 0
      hadd _cb HookId 0
    }

    hadd _cb _SizingID 0
    hadd _cb _SizingRect 0
    ;  hadd _cb _ContextMenuID 0
  }

  Blocks
  .Add
  ..Random { Object.Create $r(1,7) $r(0,450) $r(0,350) }
  ..-
  ..Vertical bar { Object.Create 1 $r(0,450) $r(0,350) }
  ..Horizontal bar { Object.Create 2 $r(0,450) $r(0,350) }
  ..Tiny { Object.Create 3 $r(0,450) $r(0,350) }
  ..Small { Object.Create 4 $r(0,450) $r(0,350) }
  ..Medium { Object.Create 5 $r(0,450) $r(0,350) }
  ..Large { Object.Create 6 $r(0,450) $r(0,350) }
  ..Huge { Object.Create 7 $r(0,450) $r(0,350) }
  .-
  .$iif($hget(_cb,Object.Count) == 0,$style(2)) Delete
  ..Delete all
  ...Yes {

    var %ObjectList = $hget(_Cb,Object.List)
    var %ObjectId = 0
    var %ObjectLen = $numtok(%ObjectList,58)

    var %i = 1

    while (%i <= %ObjectLen) {

      %ObjectID = $gettok(%ObjectList,%i,58)
      Object.Delete %ObjectID

      inc %i
    }
  }
  ..$submenu($_cb.GenerateObjectList($1))
  -
  Collision detection
  .$iif($hget(_cb,_BlockCol) == 1,$style(1)) On { hadd _cb _BlockCol 1 }
  .$iif($hget(_cb,_BlockCol) == 0,$style(1)) Off { hadd _cb _BlockCol 0 }
  Misc  
  .Mouse velocity multiplier ( $+ $hget(_cb,_MouseVelMult) $+ ) {

    var %val = $input(Enter new multiplier (0.0 to 10.0),e,Physic engine)
    if ($len(%val) > 0) {

      ; facon cheap de s'assurer un nombre
      %val = $calc(%val + 0)

      if (%val < 0) %val = 0
      if (%val > 10) %val = 10
      hadd _cb _MouseVelMult %val
    }
  }
  .$iif($hget(_cb,_showpopup),$style(1)) Show mouseover popups {
    hadd _cb _ShowPopUp $abs($calc($hget(_cb,_showPopUp) - 1))
  }
  .$iif($hget(_cb,_showID),$style(1)) Show ID {
    hadd _cb _ShowID $abs($calc($hget(_cb,_showID) - 1))
  }
  .$iif($hget(_cb,_resizeGrip),$style(1)) Show resize grip {
    hadd _cb _resizeGrip $abs($calc($hget(_cb,_resizeGrip) - 1))
  }
  .Background color
  ..Select {

    var %msg = Input Red, green and blue value separated by commas (ex.: 255,128,64)
    var %rgb = $input(%msg,es,@physics,Physic engine)

    if ($len(%rgb) > 0) {

      tokenize 44 %rgb
      %rgb = $rgb($1,$2,$3)
      if ($len(%rgb) > 0) {
        hadd _cb _BGColor %rgb
      }
    }
  }
  .Background image
  ..Select {

    var %sFile = $sfile($+($scriptdir,Textures\,*.bmp),Select a background,Open)
    if ($isFile(%sFile)) {

      hadd _cb _background $nopath(%sFile)
    }
  }
  ..-
  ..None { hdel _cb _background }
  -
  Save environment { 

    var %sFile = $sfile($+($scriptdir,env\,*.env),Select a filename,Save)
    var %msg = The file already exists, do you want to overwrite it?
    var %ret

    if ($len(%sFile) > 0) {

      if ($gettok(%sFile,-1,46) != env) {
        %sFile = %sFile $+ .env
      }

      %sFile = $+(",%sFile,")

      if ($isFile(%sFile)) {

        %ret = $input(%msg,ysv,@Physics,Physics)
      }
      else {
        %ret = $yes
      }

      if (%ret == $yes) {

        hsave -i _cb %sFile
        echo -a Save successfull
      }  
    }
  }
  Load environment {

    var %sFile = $sfile($+($scriptdir,Env\,*.env),Select a filename,Load)
    if ($len(%sFile) > 0) {
      hfree _cb
      hmake _cb 1000
      hload -i _cb $shortfn(%sFile)
    }
  }
  -
  $iif(!$_GetContextMenuBlockId($mouse.x,$mouse.y),$style(2)) Selected block
  .Size  
  ..Width { 
    var %width = $input(Enter new width (5 to 500),e,Physic engine)
    if ($len(%width) > 0) {

      %width = $calc(%width + 0)
      Object.Set -v $hget(_cb,_ContextMenuId) w 5 500 %width
    }
  }
  ..Height {
    var %height = $input(Enter new height (5 to 500),e,Physic engine)
    if ($len(%height) > 0) {

      %height = $calc(%height + 0)
      Object.Set -v $hget(_cb,_ContextMenuId) h 5 500 %height
    }
  }
  .Bouncing factor {
    var %friction = $input(Enter new bouncing factor (1.0 to 100.0, default is 2.0),e,Physic engine)
    if ($len(%friction) > 0) {

      %friction = $calc(%friction + 0)
      Object.Set -v $hget(_cb,_ContextMenuId) friction 1 100 %friction
    }
  }
  .Filling
  ..None { Object.set $hget(_cb,_ContextMenuId) fill 0 }
  ..Solid { Object.set $hget(_cb,_ContextMenuId) fill 1 }
  ..Texture { Object.set $hget(_cb,_ContextMenuId) fill 2 }
  .Texture
  ..grass1 { Object.set $hget(_cb,_ContextMenuId) img grass1.bmp }
  ..gwrap1 { Object.set $hget(_cb,_ContextMenuId) img gwrap1.bmp }
  ..gwrap3 { Object.set $hget(_cb,_ContextMenuId) img gwrap3.bmp }
  ..hifi3 { Object.set $hget(_cb,_ContextMenuId) img hifi3.bmp }
  ..tree4 { Object.set $hget(_cb,_ContextMenuId) img tree4.bmp }
  ..woodtile2 { Object.set $hget(_cb,_ContextMenuId) img woodtile2.bmp }
  ..xozbrick1 { Object.set $hget(_cb,_ContextMenuId) img xozbrick1.bmp }
  .Transparent color
  ..None { Object.set $hget(_cb,_ContextMenuId) trans 0 }
  ..-
  ..Select {

    var %msg = Input Red, green and blue value separated by commas (ex.: 255,128,64)
    var %rgb = $input(%msg,es,@physics,Physic engine)

    if ($len(%rgb) > 0) {

      tokenize 44 %rgb
      %rgb = $rgb($1,$2,$3)
      if ($len(%rgb) > 0) {

        Object.set $hget(_cb,_ContextMenuId) transcol %rgb
        Object.set $hget(_cb,_ContextMenuId) trans 1
      }
    }
  }

  .-
  .Delete
  ..Yes {
    Object.Delete $hget(_cb,_ContextMenuId)
  }
}

; Congratulation, you have reach the end of the file