Update bunny mark

This commit is contained in:
Rob Loach 2019-01-28 15:50:29 -05:00
parent ba1809a425
commit a67ebad586
No known key found for this signature in database
GPG Key ID: 627C60834A74A21A
2 changed files with 44 additions and 39 deletions

View File

@ -1,9 +1,9 @@
// Copyright (c) 2018 KANO Computing Ltd.
// Copyright (c) 2018 KANO Computing Ltd.
// Licensed under the GNU GPL v2
//
//
// Original by Iain Lobb
// Inspired by PixieJS
// Bunny Image by Amanda Lobb
// Bunny Image by Amanda Lobb
// https://github.com/rishavs/love2d-bunnymark
// ChaiLove version by Rob Loach
@ -15,20 +15,20 @@ def conf(t) {
}
def load() {
global bunnies = []
global bunnies = List()
global gravity = 0.98f
global maxX = love.graphics.getWidth( )
global minX = 0
global maxY = love.graphics.getHeight( )
global minY = 0
// optimise the bunny size for embedded devices
global baseLitterSize = 50
global litterSizeIncrement = 50
global litterSize = baseLitterSize
global stdOutText = ""
global bunnyImg = love.graphics.newImage("bunny.png")
@ -36,11 +36,7 @@ def load() {
}
def draw() {
// enable memory profiling
//collectgarbage("collect")
love.graphics.print(to_string(bunnies.size()) + " Total Bunnies", 20, 10)
//love.graphics.print(debug.traceback(), 400, 10)
love.graphics.print(to_string(litterSize) + " bunnies in each Litter", 20, 20)
// print the current memory usage
@ -49,36 +45,43 @@ def draw() {
love.graphics.print("Current FPS: " + to_string(love.timer.getFPS()), 20, 40)
//love.graphics.print(string.format("Elapsed clock cycles: " + to_string(%.4f", (os.clock() - x) *1000), 20, 50)
for (value : bunnies) {
var tempBunnyId = value[0]
var tempBunnyPosX = value[1]
var tempBunnyPosY = value[2]
love.graphics.draw(bunnyImg, tempBunnyPosX, tempBunnyPosY)
for (bunny : bunnies) {
love.graphics.draw(bunnyImg, bunny[0], bunny[1])
}
}
def mousepressed(x, y, button) {
for (var i = 0; i < litterSize; ++i) {
procreate(x, y)
}
}
def joystickpressed(joystick, button) {
for (var i = 0; i < litterSize; ++i) {
procreate(love.graphics.getWidth() / 2.0f, love.graphics.getHeight() / 8.0f)
}
}
def update(dt) {
var i = -1
for (value : bunnies) {
i = i + 1
var tempBunnyId = value[0]
var tempBunnyPosX = value[1]
var tempBunnyPosY = value[2]
var tempBunnySpeedX = value[3]
var tempBunnySpeedY = value[4]
tempBunnyPosX = tempBunnyPosX + tempBunnySpeedX;
// Initialize variables once per cycle.
var tempBunnyPosX = 0.0f
var tempBunnyPosY = 0.0f
var tempBunnySpeedX = 0.0f
var tempBunnySpeedY = 0.0f
for (bunny : bunnies) {
// Retrieve the bunny values.
tempBunnyPosX = bunny[0]
tempBunnyPosY = bunny[1]
tempBunnySpeedX = bunny[2]
tempBunnySpeedY = bunny[3]
// Move the bunny.
tempBunnyPosX = tempBunnyPosX + tempBunnySpeedX;
tempBunnyPosY = tempBunnyPosY + tempBunnySpeedY;
// Adjust coordinates with world.
tempBunnySpeedY = tempBunnySpeedY + gravity;
if (tempBunnyPosX > maxX) {
tempBunnySpeedX = tempBunnySpeedX * -0.9f;
tempBunnyPosX = maxX;
@ -86,7 +89,6 @@ def update(dt) {
tempBunnySpeedX = tempBunnySpeedX * -0.9f;
tempBunnyPosX = minX;
}
if (tempBunnyPosY > maxY) {
tempBunnySpeedY = tempBunnySpeedY * -0.9f;
tempBunnyPosY = maxY;
@ -94,26 +96,25 @@ def update(dt) {
tempBunnySpeedY = tempBunnySpeedY * -0.9f;
tempBunnyPosY = minY;
}
// push all values back in the tables
bunnies[i] = [tempBunnyId, tempBunnyPosX, tempBunnyPosY, tempBunnySpeedX, tempBunnySpeedY]
// Update the bunny
bunny[0] = tempBunnyPosX
bunny[1] = tempBunnyPosY
bunny[2] = tempBunnySpeedX
bunny[3] = tempBunnySpeedY
}
}
def quit() {
print("Quitting app!")
}
//------------------------------------------------
// Custom functions
//------------------------------------------------
def procreate(x,y) {
var bunnyId = bunnies.size()
var bunnyPosX = x
var bunnyPosY = y
var bunnySpeedX = love.math.random(-5.0f, 5.0f)
var bunnySpeedY = love.math.random(-5.0f, 5.0f)
var bunny = [bunnyId, bunnyPosX, bunnyPosY, bunnySpeedX, bunnySpeedY]
bunnies.push_back(bunny)
var bunny = [bunnyPosX, bunnyPosY, bunnySpeedX, bunnySpeedY]
bunnies.push_back_ref(bunny)
}

View File

@ -114,9 +114,13 @@ script::script(const std::string& file) {
// ChaiScript Standard Library Additions
// This adds some basic type definitions to ChaiScript.
chai.add(bootstrap::standard_library::vector_type<std::vector<bool>>("VectorBool"));
chai.add(bootstrap::standard_library::vector_type<std::vector<int>>("VectorInt"));
chai.add(bootstrap::standard_library::vector_type<std::vector<float>>("VectorFloat"));
chai.add(bootstrap::standard_library::vector_type<std::vector<std::string>>("StringVector"));
chai.add(bootstrap::standard_library::map_type<std::map<std::string, bool>>("StringBoolMap"));
chai.add(bootstrap::standard_library::map_type<std::map<std::string, int>>("StringIntMap"));
chai.add(bootstrap::standard_library::map_type<std::map<std::string, float>>("StringFloatMap"));
auto stringmethods = chaiscript::extras::string_methods::bootstrap();
chai.add(stringmethods);