const bounceBalls = state => {
  state.actors.forEach(ball => {
    if (ball.x < 0 || ball.x > 128 - 8) {
      ball.dx *= -1
    }
    if (ball.y < 0 || ball.y > 128 - 8) {
      ball.dy *= -1
    }
  })
}

const moveBalls = (state, extra = 0) => {
  state.actors.forEach(ball => {
    ball.x += ball.dx + extra
    ball.y += ball.dy + extra
  })
}

init = state => {
  // If you're going to use `state.actors`,
  // make sure to give each item in `actors`
  // a unique `id`.
  state.actors = [
    {
      id: 'ball0',
      x: 10,
      y: 60,
      dx: 2,
      dy: 1,
      sprite: 0
    },
    {
      id: 'ball1',
      x: 30,
      y: 90,
      dx: -1,
      dy: -2,
      sprite: 1
    },
    {
      id: 'ball2',
      x: 60,
      y: 30,
      dx: -1,
      dy: -1,
      sprite: 2
    }
  ]
}

update = (state, input, elapsed) => {
  bounceBalls(state)
  moveBalls(state, 0)
}

draw = state => {
  clear()
  rectStroke(0, 0, 128, 128, 5)

  // `drawActors` is a built-in SCRIPT-8 function.
  drawActors(state)
}