Unverified Commit 457c1a80 authored by RobLoach's avatar RobLoach Committed by GitHub
Browse files

Create main.chai

parent 67e775e9
// Copyright (c) 2018 KANO Computing Ltd.
// Licensed under the GNU GPL v2
//
// Original by Iain Lobb
// Inspired by PixieJS
// Bunny Image by Amanda Lobb
// https://github.com/rishavs/love2d-bunnymark
// ChaiLove version by Rob Loach
def conf(t) {
t.console = true
t.window.title = "Bunnymark Mark II ; a little better!"
t.window.width = 800
t.window.height = 600
}
def load() {
global bunnies = []
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")
procreate(0, 0)
}
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
// rounding down mem to three dig: math.floor(mem+0.5) / math.pow(10,dig)
//love.graphics.print(floor(collectgarbage("count") + 0.5)/pow(10,3) + " MB Mem Usage", 20, 30)
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)
}
}
def mousepressed(x, y, button) {
for (var i = 0; i < litterSize; ++i) {
procreate(x, y)
}
}
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;
tempBunnyPosY = tempBunnyPosY + tempBunnySpeedY;
tempBunnySpeedY = tempBunnySpeedY + gravity;
if (tempBunnyPosX > maxX) {
tempBunnySpeedX = tempBunnySpeedX * -0.9f;
tempBunnyPosX = maxX;
} else if (tempBunnyPosX < minX) {
tempBunnySpeedX = tempBunnySpeedX * -0.9f;
tempBunnyPosX = minX;
}
if (tempBunnyPosY > maxY) {
tempBunnySpeedY = tempBunnySpeedY * -0.9f;
tempBunnyPosY = maxY;
} else if (tempBunnyPosY < minY) {
tempBunnySpeedY = tempBunnySpeedY * -0.9f;
tempBunnyPosY = minY;
}
// push all values back in the tables
bunnies[i] = [tempBunnyId, tempBunnyPosX, tempBunnyPosY, tempBunnySpeedX, 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)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment