|
#1
|
||||
|
||||
|
Now that the SKD has been released and more people are getting into writing apps I thought it would be a good idea to have a thread devoted to useful reusable code functions that are not included in the standard Libraries.
I'll kick things off; :P Returns true if the point x,y is within the region at rx,ry with the width w and height h, else returns false: Code:
function withinRegion(x, y, rx, ry, w, h) if x >= rx and x <= (rx + w) and y >= ry and y <= (ry + h) then return true; else return false; end; end; Code:
function pointDistance(x,y,x1,y1) local xdist; local ydist; local dist; xdist = math.abs(x-x1); ydist = math.abs(y-y1); dist = math.sqrt((xdist*xdist)+(ydist*ydist)); return dist; end;
|
|
|
|||
|
|
|
#2
|
|||
|
|||
|
does zenlua support class structure?
can i say Quote:
var test= new one("test"); you'd get test1 if you went echo test.three() you get a result of 3 but if you went echo test.two() you'd get nothing or an error Last edited by badazzmofo; 02-08-2010 at 01:52 PM. |
|
#3
|
||||
|
||||
|
Nope - Lua doesn't support classes. You can try and emulate them but its still just functions :P (I'm doing this in the scrolling shooter to allow instances)
|
|
#4
|
|||
|
|||
|
you mean like
function one() public function one( arg ) echo arg . 1; end; private function two() return 2; end; public function three( arg ) return two() + 1; end; end; ive done that in javascript how about private/public? does that work? |
|
#5
|
||||
|
||||
|
You can't nest functions and there's no need for public/private stuff. There is just functions, that's it :P What you can do to emulate classes is to create a table which holds functions e.g. math.sqrt() is a function called sqrt in a table called math. This makes "math" like a class in other languages, but in Lua its just a table :P
|
|
#6
|
|||
|
|||
|
Classes can be emulated as Modules. Private/Public is done via the local command. Check out Apeopex for an example of that.
__________________
Only ONE more functions in the Wiki need addressing! Zen X-Fi2 LUA Wiki Want to protect your applications? Click Here! Tower Defense Thread Zen Lock - Protect Your Zen! |
|
#7
|
|||
|
|||
|
I have implemented simple viewer of tile maps. I just wanted to test if it can be done on X-fi 2, and how much of memory i will waste by map.
Currently there is not culling of not visible tiles, but it is still pretty fluent with map slightly bigger than screen. Tiles are from TILED editor (free editor) If someone finds it usefull, go ahead and use/change code. |
|
#8
|
||||
|
||||
|
Classes can best be simulated by metatables, e.g. something like this:
Code:
-- The class structure
local MyClass = {}
local MyClass_mt = {__index = MyClass}
function MyClass:New(a, b)
local instance = {aValue = a, bValue = b}
return setmetatable(instance, MyClass_mt)
end
function MyClass:GetSum()
return self.aValue + self.bValue
end
-- The instances
local object1 = MyClass:New(1, 3)
print( object1.aValue ) -- prints '1'
print( object1:GetSum() ) -- prints '4'
local object2 = MyClass:New(4, 5)
print (object2:GetSum() ) -- prints '9'
Last edited by xConStruct; 02-26-2010 at 11:55 AM. Reason: typo |
|
#9
|
|||
|
|||
|
these may not be common, but they are probably helpful if you need them...
the distance between coordinates function can be shortened to Code:
function dist(x1,y1,x2,y2)
return math.sqrt((math.abs(x1-x2)*math.abs(x1-x2))+(math.abs(y1-y2)*math.abs(y1-y2)))
end
Code:
function triarea(x1,y1,x2,y2,x3,y3)
local as = dist(x1,y1,x2,y2)
local bs = dist(x2,y2,x3,y3)
local cs = dist(x3,y3,x1,y1)
local s = (as+bs+cs)/2
return math.sqrt(s*(s-as)*(s-bs)*(s-cs))
end
Code:
function pointintriangle(ax,ay,bx,by,cx,cy)
local defined_tri = (triarea(ax,ay,bx,by,cx,cy))
local tri1 = (triarea(x,y,ax,ay,bx,by))
local tri2 = (triarea(x,y,bx,by,cx,cy))
local tri3 = (triarea(x,y,cx,cy,ax,ay))
if math.floor(tri1+tri2+tri3) == math.floor(defined_tri) then
return 1
else
return 0
end
end
Code:
function obsticalarea(xmin,xmax,ymin,ymax)
local ballright = ((bx ) + (ballsize / 2))
local ballleft = ((bx ) - (ballsize / 2))
local balltop = ((by ) - (ballsize / 2))
local ballbottom = ((by ) + (ballsize / 2))
local cright = xmax
local cleft = xmin
local ctop = ymin
local cbottom = ymax
cx = ((xmax-xmin)/2)+xmin
cy = ((ymax-ymin)/2)+ymin
xoffset = math.abs(bx-cx)
yoffset = math.abs(by-cy)
if bx < cx and xoffset > yoffset and ballright > cleft then
if bufferxavg < 0 then
xmove = 0
else
xmove = 1
end
ymove = 1
elseif bx > cx and xoffset > yoffset and ballleft < cright then
if bufferxavg > 0 then
xmove = 0
else
xmove = 1
end
ymove = 1
elseif by < cy and yoffset > xoffset and ballbottom > ctop then
if bufferyavg > 0 then
ymove = 0
else
ymove = 1
end
xmove = 1
elseif by > cy and yoffset > xoffset and balltop < cbottom then
if bufferyavg < 0 then
ymove = 0
else
ymove = 1
end
xmove = 1
else
xmove = 1
ymove = 1
end
end
http://forums.qj.net/psp-development...-snippets.html |
|
#10
|
||||
|
||||
|
Quote:
__________________
"If you are good enough at English to apologize, then there is no need to." - A good friend of mine Discovered something about the X-Fi2 you think others may not know? Post it here so others can learn about it! Have a question about X-Fi2 apps? Consult the FAQ before creating a thread about it. Like my work? Tell your friends. Don't like it? Tell me so I can improve. ^.^ |
|
#11
|
||||
|
||||
|
A function to find out, whether the application is running on the Zen or the simulator, maybe good for debugging functions:
Code:
local function isZen()
return debug.traceback():match("a:/") and true
end
- region a with coords (aX, aY) and size (aW, aH) - region b with coords (bX, bY) and size (bW, bH) Code:
function intersects(aX, aY, aW, aH, bX, bY, bW, bH)
return (aX < bX + bW) and (bX < aX + aW) and (aY < bY + bH) and (bY < aY + aH)
end
|
|
#12
|
||||
|
||||
|
A function for realtime animation:
Code:
--LeftAnim
function moveleft()
Timer = Timer + 1;
if Timer>= 9 or Timer<=0 then
Timer = 0;
end
if Timer==2 then
CurrentPlayerImg = manLeft1;
end
if Timer==4 then
CurrentPlayerImg = manLeft2;
end
if Timer==6 then
CurrentPlayerImg = manLeft3;
end
if Timer==8 then
CurrentPlayerImg = manLeft2;
end
end
-Put a CurrentPlayerImg:draw(playerx,playery); in the draw screen function to make it update
__________________
Current Languages: -English Last edited by Diddykong13; 03-10-2010 at 10:17 AM. Reason: Error |
|
#13
|
|||
|
|||
|
Want to compile your code like Creatives Sudoku game? If so read on:
Quote:
__________________
Only ONE more functions in the Wiki need addressing! Zen X-Fi2 LUA Wiki Want to protect your applications? Click Here! Tower Defense Thread Zen Lock - Protect Your Zen! |
|
#14
|
|||
|
|||
|
with the new firmware and everything, here is a script that creates a file "functions.lua" and lists all of the functions available.
Code:
local seen={}
file = io.open("functions.lua","w")
function dump(t,i)
seen[t]=true
local s={}
local n=0
for k in pairs(t) do
n=n+1 s[n]=k
end
table.sort(s)
for k,v in ipairs(s) do
print(i,v)
file:write(i, v, "\n")
v=t[v]
if type(v)=="table" and not seen[v] then
dump(v,i.."\t")
end
end
end
dump(_G,"")
|
|
#15
|
|||
|
|||
|
My internet is too slow to update right now, any new functions found with that dump function?
__________________
Only ONE more functions in the Wiki need addressing! Zen X-Fi2 LUA Wiki Want to protect your applications? Click Here! Tower Defense Thread Zen Lock - Protect Your Zen! |
|
#16
|
|||
|
|||
|
the accelerometer functions are the same as in the api. the only ones i have noticed are os.execute() and os.tmpname() i have not tested them yet though.
|
|
#17
|
|||
|
|||
|
New functions as listed by cilmaviel;
Quote:
Quote:
Dump of output of cilmaviel's function, assess at your will; Code:
_G _VERSION accelerometer close get_datatype get_samplerate getdata open set_datatype set_samplerate assert audio beep channel mute volume button click event hold home power up collectgarbage color a b g new r control isButton isSensor isTouch read coroutine create resume running status wrap yield debug debug getfenv gethook getinfo getlocal getmetatable getregistry getupvalue setfenv sethook setlocal setmetatable setupvalue traceback dofile dump error file gcinfo getfenv getmetatable image close create draw fill height load setbg setresource width io close flush input lines open output popen read stderr stdin stdout tmpfile type write ipairs load loadfile loadstring math abs acos asin atan atan2 ceil cos cosh deg div exp floor fmod frexp huge ldexp log log10 max min mod modf pi pow rad random randomseed sin sinh sqrt tan tanh module newproxy next os clock date difftime exit getenv ostime remove rename setlocale sleep time wait package config cpath loaded _G accelerometer audio button color control coroutine debug image io math os package screen backlight clear drawline drawpixel drawrect fillrect height orientation update width string byte char dump find format gfind gmatch gsub len lower match rep reverse sub upper table concat foreach foreachi getn insert maxn remove setn sort text color draw size touch click down event hold move pos up wav close load play time loaders 1 2 3 4 loadlib path preload seeall pairs pcall print rawequal rawget rawset require screen select setfenv setmetatable string table text toint tonumber tostring touch type unpack wav xpcall Last edited by Tetrajak; 03-24-2010 at 07:21 PM. |
|
#18
|
|||
|
|||
|
So accelerometer.set_senddatatype is now accelerometer.set_datatype. Same with the respective get().
I didn't notice os.execute() in the list?
__________________
Only ONE more functions in the Wiki need addressing! Zen X-Fi2 LUA Wiki Want to protect your applications? Click Here! Tower Defense Thread Zen Lock - Protect Your Zen! |
|
#19
|
|||
|
|||
|
Indeed, os.execute is not in the list. You'll have to ask cilmaviel about that one.
|
|
#20
|
|||
|
|||
|
it's weird those 2 functions were in the list, but i ran it again and they were gone. when i turned the player off and back on after i installed the firmware it reset like others said, i may have ran this before it reset. execute may be limited to a small window of use.
|
![]() |
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 04:50 PM.












Linear Mode
