|
| 1 | +// Copyright (c) 2018 KANO Computing Ltd. |
| 2 | +// Licensed under the GNU GPL v2 |
| 3 | +// |
| 4 | +// Original by Iain Lobb |
| 5 | +// Inspired by PixieJS |
| 6 | +// Bunny Image by Amanda Lobb |
| 7 | +// https://github.com/rishavs/love2d-bunnymark |
| 8 | +// ChaiLove version by Rob Loach |
| 9 | + |
| 10 | +def conf(t) { |
| 11 | + t.console = true |
| 12 | + t.window.title = "Bunnymark Mark II ; a little better!" |
| 13 | + t.window.width = 800 |
| 14 | + t.window.height = 600 |
| 15 | +} |
| 16 | + |
| 17 | +def load() { |
| 18 | + global bunnies = List() |
| 19 | + global gravity = 0.98f |
| 20 | + |
| 21 | + global maxX = love.graphics.getWidth( ) |
| 22 | + global minX = 0 |
| 23 | + |
| 24 | + global maxY = love.graphics.getHeight( ) |
| 25 | + global minY = 0 |
| 26 | + |
| 27 | + // optimise the bunny size for embedded devices |
| 28 | + global baseLitterSize = 50 |
| 29 | + global litterSizeIncrement = 50 |
| 30 | + global litterSize = baseLitterSize |
| 31 | + |
| 32 | + global stdOutText = "" |
| 33 | + |
| 34 | + global bunnyImg = love.graphics.newImage("bunny.png") |
| 35 | + procreate(0, 0) |
| 36 | +} |
| 37 | + |
| 38 | +def draw() { |
| 39 | + love.graphics.print(to_string(bunnies.size()) + " Total Bunnies", 20, 10) |
| 40 | + love.graphics.print(to_string(litterSize) + " bunnies in each Litter", 20, 20) |
| 41 | + |
| 42 | + // print the current memory usage |
| 43 | + // rounding down mem to three dig: math.floor(mem+0.5) / math.pow(10,dig) |
| 44 | + //love.graphics.print(floor(collectgarbage("count") + 0.5)/pow(10,3) + " MB Mem Usage", 20, 30) |
| 45 | + love.graphics.print("Current FPS: " + to_string(love.timer.getFPS()), 20, 40) |
| 46 | + //love.graphics.print(string.format("Elapsed clock cycles: " + to_string(%.4f", (os.clock() - x) *1000), 20, 50) |
| 47 | + |
| 48 | + for (bunny : bunnies) { |
| 49 | + love.graphics.draw(bunnyImg, bunny[0], bunny[1]) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +def mousepressed(x, y, button) { |
| 54 | + for (var i = 0; i < litterSize; ++i) { |
| 55 | + procreate(x, y) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +def joystickpressed(joystick, button) { |
| 60 | + for (var i = 0; i < litterSize; ++i) { |
| 61 | + procreate(love.graphics.getWidth() / 2.0f, love.graphics.getHeight() / 8.0f) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +def update(dt) { |
| 66 | + // Initialize variables once per cycle. |
| 67 | + var tempBunnyPosX = 0.0f |
| 68 | + var tempBunnyPosY = 0.0f |
| 69 | + var tempBunnySpeedX = 0.0f |
| 70 | + var tempBunnySpeedY = 0.0f |
| 71 | + |
| 72 | + for (bunny : bunnies) { |
| 73 | + // Retrieve the bunny values. |
| 74 | + tempBunnyPosX = bunny[0] |
| 75 | + tempBunnyPosY = bunny[1] |
| 76 | + tempBunnySpeedX = bunny[2] |
| 77 | + tempBunnySpeedY = bunny[3] |
| 78 | + |
| 79 | + // Move the bunny. |
| 80 | + tempBunnyPosX = tempBunnyPosX + tempBunnySpeedX; |
| 81 | + tempBunnyPosY = tempBunnyPosY + tempBunnySpeedY; |
| 82 | + |
| 83 | + // Adjust coordinates with world. |
| 84 | + tempBunnySpeedY = tempBunnySpeedY + gravity; |
| 85 | + if (tempBunnyPosX > maxX) { |
| 86 | + tempBunnySpeedX = tempBunnySpeedX * -0.9f; |
| 87 | + tempBunnyPosX = maxX; |
| 88 | + } else if (tempBunnyPosX < minX) { |
| 89 | + tempBunnySpeedX = tempBunnySpeedX * -0.9f; |
| 90 | + tempBunnyPosX = minX; |
| 91 | + } |
| 92 | + if (tempBunnyPosY > maxY) { |
| 93 | + tempBunnySpeedY = tempBunnySpeedY * -0.9f; |
| 94 | + tempBunnyPosY = maxY; |
| 95 | + } else if (tempBunnyPosY < minY) { |
| 96 | + tempBunnySpeedY = tempBunnySpeedY * -0.9f; |
| 97 | + tempBunnyPosY = minY; |
| 98 | + } |
| 99 | + |
| 100 | + // Update the bunny |
| 101 | + bunny[0] = tempBunnyPosX |
| 102 | + bunny[1] = tempBunnyPosY |
| 103 | + bunny[2] = tempBunnySpeedX |
| 104 | + bunny[3] = tempBunnySpeedY |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +//------------------------------------------------ |
| 109 | +// Custom functions |
| 110 | +//------------------------------------------------ |
| 111 | + |
| 112 | +def procreate(x,y) { |
| 113 | + var bunnyPosX = x |
| 114 | + var bunnyPosY = y |
| 115 | + var bunnySpeedX = love.math.random(-5.0f, 5.0f) |
| 116 | + var bunnySpeedY = love.math.random(-5.0f, 5.0f) |
| 117 | + var bunny = [bunnyPosX, bunnyPosY, bunnySpeedX, bunnySpeedY] |
| 118 | + |
| 119 | + bunnies.push_back_ref(bunny) |
| 120 | +} |
0 commit comments