Files
prosperon/examples/bunnymark/main.ce
2026-01-19 18:57:25 -06:00

70 lines
1.9 KiB
Plaintext

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:
for (var i = 0; i < 100; i++) {
bunnies.push({
x: random.random() * config.width,
y: random.random() * config.height,
vx: (random.random() * 300) - 150,
vy: (random.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 (length(fpsSamples) > 60) fpsSamples.shift()
var sum = reduce(fpsSamples, function(a,b) { return a + b })
fpsAvg = sum / length(fpsSamples)
// 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: (random.random() * 300) - 150,
vy: (random.random() * 300) - 150
})
}
// Update bunny positions and bounce them inside the screen:
for (var i = 0; i < length(bunnies); 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: ' + length(bunnies)
draw.text(msg, {x:0, y:0, width:config.width, height:40}, null, 0, color.white, 0)
}