
plotscript, follow loop, begin
  init mouse

  variable (xgo, ygo, box, box center, targetx, targety, mouse slice)
  box := create rect(20, 20)
  #set parent(box, lookup slice(sl: map layer 0))  # no effect
  set rect border(box, border:line)
  box center := create container
  set parent(box center, box)
  center slice(box center)
  mouse slice := create rect(1,1)

  show string at($0="[R] to resize rect", 170, 170)

  # Located at a tile surrounded by walls, for immediate error throwin
  variable(disallow square)
  disallow square := create container(20,20)
  put slice(disallow square, 220, 100)

  variable(friction)
  friction := 50

  variable(using mouse, using moveslice)
  using mouse := true
  using moveslice := false

  suspend player
  while(true) do (
    if (keyval(key:esc) > 1) then (game over)
    if (keyval(key:R) > 1) then (
      set slice width(box, random(1, 50))
      set slice height(box, random(1, 50))
    )
    if (keyval(key:plus) > 1)  then (friction += 10)
    if (keyval(key:minus) > 1) then (friction -= 10)
    if (keyval(key:m) > 1) then (using mouse := true)
    if (keyval(key:w) > 1) then (using moveslice := using moveslice, xor, true)

    xgo := (ygo := 0)
    if (keyval(key:left))  then (xgo := -4)
    if (keyval(key:right)) then (xgo := 4)
    if (keyval(key:up))    then (ygo := -4)
    if (keyval(key:down))  then (ygo := 4)

    # (This is a more verbose version of the below)
    # # Target map position
    # targetx := camera pixel x + mouse pixel x
    # targety := camera pixel y + mouse pixel y
    # # xgo/ygo = target -- current
    # xgo := (targetx -- (slice screen x(box center) + camera pixel x)) / 3
    # ygo := (targety -- (slice screen y(box center) + camera pixel y)) / 3


    if (xgo || ygo) then (using mouse := false)

    set slice visible(mouse slice, using mouse)
    if (using mouse) then (
      put slice(mouse slice, mouse pixel x, mouse pixel y)

      # Calculate the offset from the center of the box to the mouse,
      # and divide by three to move a third of the way there.
      xgo := (mouse pixel x -- slice screen x(box center)) / 3
      ygo := (mouse pixel y -- slice screen y(box center)) / 3
    )

    if (using moveslice) then (
            show value( move slice with wallchecking(box, xgo, ygo, friction) )
    ) else (
            variable(new xgo, new ygo)
            #tracevalue(slice x(box), slice y(box), slice width(box), slice height(box), xgo, ygo)
            new xgo := check wall collision x(slice x(box), slice y(box), slice width(box), slice height(box), xgo, ygo, friction)
            new ygo := check wall collision y(slice x(box), slice y(box), slice width(box), slice height(box), xgo, ygo, friction)
            move slice by(box, new xgo, new ygo, 1)
            if (slice collide(box, disallow square)) then (scripterror)
            show no value
    )

    show string at(string sprintf(2, $99="[+/-] Friction: %d%%", friction), 150, 190)
    if (using mouse) then (
      show string at($3="[Arrow keys] move", 0, 170)
    ) else (
      show string at($3="[M] to enable mouse", 0, 170)
    )
    if (using moveslice) then (
      show string at($1="[W] switch to checkwallcollisionx/y", 0, 180)
    ) else (
      show string at($1="[W] switch to moveslicewithwallchecking", 0, 180)
    )
    wait
  )
end
