var draw = use('draw2d')
var render = use('render')
var graphics = use('graphics')
var sprite = use('sprite')
var geom = use('geometry')
var config = use('config')
var color = use('color')
var random = use('random')

var bunnyTex = graphics.texture("bunny")

// We'll store our bunnies in an array of objects: { x, y, vx, vy }
var bunnies = []

// Start with some initial bunnies:
var i = 0;
for (i = 0; i < 100; i++) {
  push(bunnies, {
    x: random.random() * config.width,
    y: random.random() * config.height,
    vx: (random.random() * 300) - 150,
    vy: (random.random() * 300) - 150
  })
}

var update = function(dt) {
  // If left mouse is down, spawn some more bunnies:
  var mouse = input.mousestate()
  var i = 0;
  var b = null;
  if (mouse.left)
    for (i = 0; i < 50; i++) {
      push(bunnies, {
        x: mouse.x,
        y: mouse.y,
        vx: (random.random() * 300) - 150,
        vy: (random.random() * 300) - 150
      })
    }

  // Update bunny positions and bounce them inside the screen:
  for (i = 0; i < length(bunnies); i++) {
    b = bunnies[i]
    b.x += b.vx * dt
    b.y += b.vy * dt

    // Bounce off left/right edges
    if (b.x < 0) { b.x = 0; b.vx = -b.vx }
    else if (b.x > config.width) { b.x = config.width; b.vx = -b.vx }

    // Bounce off bottom/top edges
    if (b.y < 0) { b.y = 0; b.vy = -b.vy }
    else if (b.y > config.height) { b.y = config.height; b.vy = -b.vy }
  }
}

var hud = function() {
  draw.images(bunnyTex, bunnies)
}
