var draw = use('draw2d') var render = use('render') var graphics = use('graphics') var sprite = use('sprite') var geom = use('geometry') var input = use('controller') var config = use('config') 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: for (var i = 0; i < 100; i++) { bunnies.push({ x: Math.random() * config.width, y: Math.random() * config.height, vx: (Math.random() * 300) - 150, vy: (Math.random() * 300) - 150 }) } var fpsSamples = [] var fpsAvg = 0 this.update = function(dt) { // Compute FPS average over the last 60 frames: var currentFPS = 1 / dt fpsSamples.push(currentFPS) if (fpsSamples.length > 60) fpsSamples.shift() var sum = 0 for (var f of fpsSamples) sum += f fpsAvg = sum / fpsSamples.length // If left mouse is down, spawn some more bunnies: var mouse = input.mousestate() if (mouse.left) for (var i = 0; i < 50; i++) { bunnies.push({ x: mouse.x, y: mouse.y, vx: (Math.random() * 300) - 150, vy: (Math.random() * 300) - 150 }) } // Update bunny positions and bounce them inside the screen: for (var i = 0; i < bunnies.length; i++) { var 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 } } } this.hud = function() { draw.images(bunnyTex, bunnies) var msg = 'FPS: ' + fpsAvg.toFixed(2) + ' Bunnies: ' + bunnies.length draw.text(msg, {x:0, y:0, width:config.width, height:40}, undefined, 0, Color.white, 0) }