-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.lua
179 lines (147 loc) · 5.09 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
-- Copyright (c) 2018 KANO Computing Ltd.
-- Licensed under the GNU GPL v2
--
-- Original by Iain Lobb
-- Inspired by PixieJS
-- Bunny Image by Amanda Lobb
------------------------------------------------
-- Modules
------------------------------------------------
------------------------------------------------
-- Base functions
------------------------------------------------
function love.load()
bunnies = {}
gravity = 0.98
maxX = love.graphics.getWidth( )
minX = 0
maxY = love.graphics.getHeight( )
minY = 0
-- optimise the bunny size for embedded devices
baseLitterSize = 1000
litterSizeIncrement = 1000
litterSize = baseLitterSize
stdOutText = ""
bunnyCount = 0
bunnyImg = love.graphics.newImage("bunny.png")
end
function love.draw()
local x = os.clock()
-- enable memory profiling
collectgarbage("collect")
love.graphics.print(bunnyCount .. " Total Bunnies", 20, 10)
love.graphics.print(debug.traceback(), 400, 10)
love.graphics.print(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(math.floor(collectgarbage("count") + 0.5)/math.pow(10,3) .. " MB Mem Usage", 20, 30)
love.graphics.print("Current FPS: "..tostring(love.timer.getFPS( )), 20, 40)
love.graphics.print(string.format("Elapsed clock cycles: %.4f", (os.clock() - x) *1000), 20, 50)
for index,value in ipairs(bunnies) do
local tempBunnyId = value[1]
local tempBunnyPosX = value[2]
local tempBunnyPosY = value[3]
love.graphics.draw(bunnyImg, tempBunnyPosX, tempBunnyPosY)
end
end
function love.mousepressed(x, y, button)
if button == 1 then
for variable = 1, litterSize, 1 do
procreate(x, y)
end
elseif button == 'wu' then
litterSize = litterSize + litterSizeIncrement
elseif button == 'wd' then
if litterSize > baseLitterSize then
litterSize = litterSize - litterSizeIncrement
end
end
end
function love.update(dt)
for index,value in ipairs(bunnies) do
local tempBunnyId = value[1]
local tempBunnyPosX = value[2]
local tempBunnyPosY = value[3]
local tempBunnySpeedX = value[4]
local tempBunnySpeedY = value[5]
tempBunnyPosX = tempBunnyPosX + tempBunnySpeedX;
tempBunnyPosY = tempBunnyPosY + tempBunnySpeedY;
tempBunnySpeedY = tempBunnySpeedY + gravity;
if (tempBunnyPosX > maxX) then
tempBunnySpeedX = tempBunnySpeedX * -0.9;
tempBunnyPosX = maxX;
elseif (tempBunnyPosX < minX) then
tempBunnySpeedX = tempBunnySpeedX * -0.9;
tempBunnyPosX = minX;
end
if (tempBunnyPosY > maxY) then
tempBunnySpeedY = tempBunnySpeedY * -0.9;
tempBunnyPosY = maxY;
elseif (tempBunnyPosY < minY) then
tempBunnySpeedY = tempBunnySpeedY * -0.9;
tempBunnyPosY = minY;
end
-- push all values back in the tables
local bunny = {tempBunnyId, tempBunnyPosX, tempBunnyPosY, tempBunnySpeedX, tempBunnySpeedY }
bunnies[index] = bunny
end
end
function love.quit()
stdOutPrint("Quitting app!")
end
------------------------------------------------
-- Custom functions
------------------------------------------------
function procreate(x,y) -- this function creates a new bunny
bunnyCount = bunnyCount + 1
local bunnyId = bunnyCount
local bunnyPosX = x
local bunnyPosY = y
local bunnySpeedX = (math.random() * 10) - 5
local bunnySpeedY = (math.random() * 10) - 5
local bunny = {bunnyId, bunnyPosX, bunnyPosY, bunnySpeedX, bunnySpeedY }
table.insert(bunnies, bunny)
end
------------------------------------------------
-- Utils. Toolbelt stuff needed to run this app
------------------------------------------------
------------------------------------------------
-- Debug. Stuff here gets removed after debugging is done
------------------------------------------------
function stdOutPrint(text)
stdOutText = text
print(text)
end
function table.val_to_str ( v )
if "string" == type( v ) then
v = string.gsub( v, "\n", "\\n" )
if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
return "'" .. v .. "'"
end
return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
else
return "table" == type( v ) and table.tostring( v ) or
tostring( v )
end
end
function table.key_to_str ( k )
if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
return k
else
return "[" .. table.val_to_str( k ) .. "]"
end
end
function table.tostring( tbl )
local result, done = {}, {}
for k, v in ipairs( tbl ) do
table.insert( result, table.val_to_str( v ) )
done[ k ] = true
end
for k, v in pairs( tbl ) do
if not done[ k ] then
table.insert( result,
table.key_to_str( k ) .. "=" .. table.val_to_str( v ) )
end
end
return "{" .. table.concat( result, "," ) .. "}"
end