Advertisement
RobotBubble

Reaction

Oct 30th, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.23 KB | None | 0 0
  1. --JaC autogenerated code--
  2. local __Methods,_Classes,tArgs,__EXTENDS={},{},{...},{
  3. __TERM=term;
  4. __SHELL=shell;
  5. __FS=fs;
  6. __OS=os;
  7. __PAINTUTILS=paintutils;
  8. }
  9. _Class=function(func, extends)
  10. local class={print=print;write=write;read=read;__RawClass=func;math=math;table=table;error=error;printError=printError;coroutine=coroutine;pairs=pairs;ipairs=ipairs;tostring=tostring;tonumber=tonumber;}
  11. function class.__newMethodInstance(mtd)
  12. if __Methods[mtd] then
  13. for k,v in pairs(__Methods[mtd].__Classes) do
  14. _Classes[k]=v
  15. end
  16. else
  17. printError("Failed to load: \""..mtd.."\", method doesn't exist!")
  18. error()
  19. end
  20. end
  21. function class.__newClassInstance(cls,...)
  22. if _Classes[cls] then
  23. local funct=_Classes[cls].__RawClass
  24. local newClass={print=print;write=write;read=read;__RawClass=funct;math=math;table=table;error=error;printError=printError;coroutine=coroutine;pairs=pairs;ipairs=ipairs;}
  25. for k,v in pairs(_Classes[cls]) do
  26. newClass[k]=v
  27. end
  28. setfenv(funct, newClass)
  29. local ok, err = pcall(funct)
  30. if not ok then printError("Failed to create a class instance!") error() end
  31. newClass[cls](...)
  32. return newClass
  33. else
  34. printError("Failed to load: \""..cls.."\", class doesn't exist!")
  35. error()
  36. end
  37. end
  38. if extends then
  39. if type(extends)=="string" then
  40. if _Classes[extends] then
  41. class[extends]={}
  42. for k,v in pairs(_Classes[extends]) do
  43. class[extends][k]=v
  44. end
  45. end
  46. else
  47. printError("Can't extend to a non existing class.")
  48. end
  49. end
  50. if func then
  51. setfenv(func, class)
  52. local ok, err = pcall(func)
  53. if not ok then printError(err) error() end
  54. end
  55. return class
  56. end
  57. function _Method(func)
  58. local method={__Classes={};}
  59. method._Class=function(func, extends)
  60. local class={print=print;write=write;read=read;__RawClass=func;math=math;table=table;error=error;printError=printError;coroutine=coroutine;pairs=pairs;ipairs=ipairs;tostring=tostring;tonumber=tonumber;}
  61. class.__RawClassEnv=getfenv(func)
  62. function class.__newMethodInstance(mtd)
  63. if __Methods[mtd] then
  64. for k,v in pairs(__Methods[mtd].__Classes) do
  65. method.__Classes[k]=v
  66. end
  67. else
  68. printError("Failed to load: \""..mtd.."\", method doesn't exist!")
  69. error()
  70. end
  71. end
  72. function class.__newClassInstance(cls,...)
  73. if method.__Classes[cls] then
  74. local funct=method.__Classes[cls].__RawClass
  75. local functEnv=method.__Classes[cls].__RawClassEnv
  76. local newClass={__RawClassEnv=functEnv;__RawClass=funct;print=print;write=write;read=read;__RawClass=func;}
  77. for k,v in pairs(method.__Classes[cls]) do
  78. newClass[k]=v
  79. end
  80. setfenv(funct, newClass)
  81. local ok, err = pcall(funct)
  82. if not ok then printError("Failed to create a class instance!") error() end
  83. newClass[cls](...)
  84. setfenv(funct, functEnv)
  85. method.__Classes[cls].__RawClass=funct
  86. return newClass
  87. else
  88. printError("Failed to load: \""..cls.."\", class doesn't exist!")
  89. error()
  90. end
  91. end
  92. if extends then
  93. if type(extends)=="string" then
  94. if __EXTENDS[extends] then
  95. for k,v in pairs(__EXTENDS[extends]) do
  96. class[k]=v
  97. end
  98. elseif method.__Classes[extends] then
  99. class[extends]={}
  100. for k,v in pairs(method.__Classes[extends]) do
  101. class[extends][k]=v
  102. end
  103. else
  104. printError("Can't extend to \""..extends.."\". Class doesn't exist!")
  105. error()
  106. end
  107. else
  108. printError("Can't extend to a non existing class!")
  109. error()
  110. end
  111. end
  112. if func then
  113. setfenv(func, class)
  114. local ok, err = pcall(func)
  115. if not ok then printError(err) error() end
  116. end
  117. return class
  118. end
  119. if func then
  120. setfenv(func, method)
  121. local ok, err = pcall(func)
  122. if not ok then printError(err) error() end
  123. end
  124. return method
  125. end
  126.  
  127. __Methods["CraftOS"]=_Method(function()
  128. __Classes["Term"]=_Class(function()
  129. function Term() end
  130. end,"__TERM")
  131. __Classes["FileSystem"]=_Class(function()
  132. function FileSystem() end
  133. end,"__FS")
  134. __Classes["PaintUtils"]=_Class(function()
  135. function PaintUtils() end
  136. end,"__PAINTUTILS")
  137. __Classes["Shell"]=_Class(function()
  138. function Shell() end
  139. end,"__SHELL")
  140. __Classes["OS"]=_Class(function()
  141. function OS() end
  142. end,"__OS")
  143. end)
  144.  
  145. --End of: JaC autogenerated code--
  146.  
  147. __Methods["GUI"] = _Method(function()
  148. __Classes["Label"] = _Class(function()
  149. term=nil;
  150. textColor=nil;
  151. backgroundColor=nil;
  152. x=nil;
  153. y=nil;
  154. label=nil;
  155. function Label(t,name,xx,yy,txtCol,bckCol)
  156. term=t
  157. label=name or "Label"
  158. x=math.floor(xx) or 1
  159. y=math.floor(yy) or 1
  160. textColor=txtCol or 32768
  161. backgroundColor=bckCol or 1
  162. end
  163. function Render()
  164. lastTxtCol=term.getTextColor();
  165. lastBckCol=term.getBackgroundColor();
  166. term.setBackgroundColor(backgroundColor)
  167. term.setTextColor(textColor)
  168. term.setCursorPos(x,y)
  169. term.write(label)
  170. term.setBackgroundColor(lastBckCol)
  171. term.setTextColor(lastTxtCol)
  172. end
  173. end,nil)
  174. __Classes["Button"] = _Class(function()
  175. name=nil;
  176. x=nil;
  177. y=nil;
  178. width=nil;
  179. height=nil;
  180. bckColor=nil;
  181. function Button(t,txt,xx,yy,ww,hh,txtX,txtY,bCol)
  182. term=t
  183. name=txt or "Button"
  184. x=math.floor(xx) or 1
  185. y=math.floor(yy) or 1
  186. width=math.floor(ww) or 8
  187. height=math.floor(hh) or 3
  188. bckColor=bCol or 8
  189. textX=math.floor(txtX) or 2
  190. textY=math.floor(txtY) or 2
  191. end
  192. function AddEventHook(ev)
  193. if(ev[1]=="mouse_click") then
  194. if(ev[2]==1) then
  195. if(ev[3]>=x and ev[3]<=x+width-1 and ev[4]>=y and ev[4]<=y+height-1) then
  196. return true;
  197. else
  198. return false;
  199. end
  200. else
  201. return false;
  202. end
  203. end
  204. end
  205. function Render()
  206. lastTxtCol=term.getTextColor();
  207. lastBckCol=term.getBackgroundColor();
  208. for yy=y,y+height-1 do
  209. for xx=x,x+width-1 do
  210. term.setCursorPos(xx,yy)
  211. term.setBackgroundColor(bckColor)
  212. term.write(" ")
  213. end
  214. end
  215. term.setCursorPos(x+(textX-1),y+(textY-1))
  216. term.write(name)
  217. term.setBackgroundColor(lastBckCol)
  218. term.setTextColor(lastTxtCol)
  219. end
  220. end,nil)
  221. end)
  222.  
  223. __Methods["Main"] = _Method(function()
  224. __Classes["INI"] = _Class(function()
  225. fs=nil;
  226. function INI(f)
  227. fs=f
  228. end
  229. function retrieve(file)
  230. iniTable={};
  231. line,char,currString,currentSection,currentSectionPos="","","","","";
  232. f=fs.open(file,"r");
  233. sectionStarted,defineSection,foundEqual=false,false,false;
  234. while(true) do
  235. line=f.readLine()
  236. if( not line) then
  237. break
  238. else
  239. line=line.."\n"
  240. end
  241. for i=1,#line do
  242. char=line:sub(i,i)
  243. if(char=="[") then
  244. sectionStarted=true
  245. elseif(char=="]") then
  246. if(sectionStarted) then
  247. iniTable[currString]={}
  248. currentSection=currString
  249. sectionStarted=false
  250. currString=""
  251. else
  252. printError("Cant start section: ["..currString.."]! Usage:")
  253. printError("["..currString.."]")
  254. error()
  255. end
  256. elseif(char=="=") then
  257. currentSectionPos=currString
  258. currString=""
  259. foundEqual=true
  260. elseif(char=="\n") then
  261. if(sectionStarted) then
  262. printError("Cant end section: ["..currString.."]! Usage:")
  263. printError("["..currString.."]")
  264. error()
  265. end
  266. iniTable[currentSection][currentSectionPos]=currString
  267. currString=""
  268. currentSectionPos=""
  269. foundEqual=false
  270. else
  271. if(char==" ") then
  272. if(foundEqual) then
  273. currString=currString..char
  274. end
  275. else
  276. currString=currString..char
  277. end
  278. end
  279. end
  280. end
  281. f.close()
  282. return iniTable;
  283. end
  284.  
  285. function write(file,data)
  286. f=fs.open(file,"w");
  287. for k,v in pairs(data) do
  288. f.writeLine("["..k.."]")
  289. for _k,_v in pairs(data[k]) do
  290. f.writeLine(tostring(_k).."="..tostring(_v))
  291. end
  292. end
  293. f.close()
  294. end
  295. end,nil)
  296. __Classes["Rectangle"] = _Class(function()
  297. rect=nil;
  298. local function mathBind(val,min,max)
  299. return math.max(math.min(val,max),min);
  300. end
  301. function Rectangle(term,x,y,w,h,color)
  302. x=mathBind(x,1,term.Width-(w-1))
  303. y=mathBind(y,2,term.Height-(h-1))
  304. rect=__newClassInstance("Button",term,"", x, y, w, h, 1, 1, color)
  305. end
  306. function Update(ev)
  307. return rect.AddEventHook(ev);
  308. end
  309. function Render()
  310. rect.Render()
  311. end
  312. end,nil)
  313. __Classes["Game"] = _Class(function()
  314. term=nil;
  315. os=nil;
  316. _TITLE=nil;
  317. madeBy=nil;
  318. poweredBy=nil;
  319. playButton=nil;
  320. upgradeButton=nil;
  321. quitButton=nil;
  322. upgrades=nil;
  323. fs=nil;
  324. backButton=nil;
  325. gameOver=nil;
  326. healthLabel=nil;
  327. maxHealthButton=nil;
  328. maxHealthLabel=nil;
  329. points=0;
  330. pointsLabel=nil;
  331. ini=nil;
  332. colors={0x2,0x4,0x8,0x10,0x20,0x40,0x80,0x100,0x200,0x400,0x800,0x1000,0x2000,0x4000,0x8000};
  333. timerLabel=nil;
  334. restartButton=nil;
  335. GAME={START=1,PAUSE=2,STOP=3};
  336. state=GAME.STOP;
  337. room=1;
  338. rect=nil;
  339. health=3;
  340. maxHealth=3;
  341.  
  342. local function mathBind(val,min,max)
  343. return math.max(math.min(val,max),min);
  344. end
  345.  
  346. function Game(t,o,f)
  347. term=t
  348. os=o
  349. fs=f
  350. ini=__newClassInstance("INI",f)
  351. _TITLE=__newClassInstance("Label",term,"Reaction Time!", term.Width/2-6, 1, 0x80, nil)
  352. madeBy=__newClassInstance("Label",term,"Made by: LeDark Lua", term.Width-18, term.Height-1, 0x80, nil)
  353. poweredBy=__newClassInstance("Label",term,"Powered by: JaC", term.Width-18, term.Height, 0x80, nil)
  354. upgrades=__newClassInstance("Label",term,"Upgrades", term.Width/2-3, 1, 0x80, nil)
  355. gameOver=__newClassInstance("Label",term,"Game Over", term.Width/2-4, term.Height/2-2, 0x80, nil)
  356. healthLabel=__newClassInstance("Label",term,"Health: "..health, 1, 1, 0x80, nil)
  357. maxHealthLabel=__newClassInstance("Label",term,"MaxHealth: "..maxHealth, term.Width/2-6, term.Height/2, 0x80, nil)
  358. pointsLabel=__newClassInstance("Label",term,"Points: "..points, 11, 1, 0x80, nil)
  359. playButton=__newClassInstance("Button",term,"Play", term.Width/2-4, term.Height/2-4, 10, 3, 4, 2, 0x20)
  360. restartButton=__newClassInstance("Button",term,"Restart", term.Width/2-4, term.Height/2, 9, 3, 2, 2, 0x20)
  361. upgradeButton=__newClassInstance("Button",term,"Upgrades", term.Width/2-4, term.Height/2, 10, 3, 2, 2, 0x4)
  362. quitButton=__newClassInstance("Button",term,"Quit", term.Width/2-4, term.Height/2+4, 10, 3, 4, 2, 0x4000)
  363. backButton=__newClassInstance("Button",term,"Back", 1, term.Height-2, 10, 3, 4, 2, 0x4000)
  364. maxHealthButton=__newClassInstance("Button",term,"+", term.Width/2+7, term.Height/2-1, 3, 3, 2, 2, 0x4000)
  365. end
  366.  
  367. function saveGame()
  368. ini.write("settings.ini",{GAME={points=points,maxHp=maxHealth}})
  369. end
  370.  
  371. function loadGame()
  372. if(fs.exists("settings.ini")) then
  373. content=ini.retrieve("settings.ini");
  374. points=tonumber(content.GAME.points)
  375. maxHealth=tonumber(content.GAME.maxHp)
  376. end
  377. end
  378.  
  379. function CreateRect()
  380. w=math.random(3,8);
  381. h=math.random(3,8);
  382. color=colors[math.random(#colors)];
  383. rect=__newClassInstance("Rectangle",term,math.random(term.Width),math.random(term.Height),w,h,color)
  384. end
  385. function Reset()
  386. health=maxHealth
  387. CreateRect(w,h)
  388. healthLabel.label="Health: "..health
  389. pointsLabel.label="Points: "..points
  390. end
  391.  
  392. function Update(event)
  393. if(state~=GAME.STOP) then
  394. if(room==1) then
  395. prPlay=playButton.AddEventHook(event);
  396. prUpgrade=upgradeButton.AddEventHook(event);
  397. prQuit=quitButton.AddEventHook(event);
  398. if(prPlay==true) then
  399. return "PLAY";
  400. elseif(prUpgrade==true) then
  401. return "UPGRADE";
  402. elseif(prQuit==true) then
  403. return "QUIT";
  404. else
  405. return nil;
  406. end
  407. elseif(room==2) then
  408. ok=rect.Update(event);
  409. if(ok) then
  410. rect=nil
  411. term.clear()
  412. points=points+(1)
  413. pointsLabel.label="Points: "..points
  414. CreateRect()
  415. else
  416. if(event[1]=="mouse_click") then
  417. if(event[2]==1) then
  418. rect=nil
  419. term.clear()
  420. health=health-(1)
  421. healthLabel.label="Health: "..health
  422. points=points-(1)
  423. pointsLabel.label="Points: "..points
  424. CreateRect()
  425. end
  426. end
  427. end
  428. if(health<1) then
  429. term.clear()
  430. room=4
  431. end
  432. return nil;
  433. elseif(room==3) then
  434. prBack=backButton.AddEventHook(event);
  435. prHpUp=maxHealthButton.AddEventHook(event);
  436. if(prBack==true) then
  437. return "BACK";
  438. elseif(prHpUp==true) then
  439. if(points>=maxHealth*2) then
  440. points=points-(maxHealth*2)
  441. maxHealth=maxHealth*2
  442. maxHealthLabel.label="MaxHealth: "..maxHealth
  443. end
  444. else
  445. return nil;
  446. end
  447. elseif(room==4) then
  448. prBack=backButton.AddEventHook(event);
  449. prRestart=restartButton.AddEventHook(event);
  450. if(prBack==true) then
  451. return "BACK";
  452. elseif(prRestart==true) then
  453. return "RESTART";
  454. else
  455. return nil;
  456. end
  457. end
  458. end
  459. end
  460.  
  461. function Draw()
  462. if(state~=GAME.STOP) then
  463. if(room==1) then
  464. _TITLE.Render()
  465. playButton.Render()
  466. upgradeButton.Render()
  467. quitButton.Render()
  468. madeBy.Render()
  469. poweredBy.Render()
  470. elseif(room==2) then
  471. healthLabel.Render()
  472. pointsLabel.Render()
  473. if(rect) then
  474. rect.Render()
  475. end
  476. elseif(room==3) then
  477. upgrades.Render()
  478. backButton.Render()
  479. maxHealthButton.Render()
  480. maxHealthLabel.Render()
  481. elseif(room==4) then
  482. gameOver.Render()
  483. restartButton.Render()
  484. backButton.Render()
  485. end
  486. end
  487. end
  488. end,nil)
  489. __Classes["Main"] = _Class(function()
  490. __newMethodInstance("CraftOS")
  491. __newMethodInstance("GUI")
  492. term=__newClassInstance("Term")
  493. shell=__newClassInstance("Shell")
  494. os=__newClassInstance("OS")
  495. fileSystem=__newClassInstance("FileSystem")
  496. term.Width,term.Height=term.getSize();
  497. game=__newClassInstance("Game",term,os,fileSystem)
  498. function Main()
  499. event=nil;
  500. term.setBackgroundColor(1)
  501. term.clear()
  502. game.loadGame()
  503. game.state=game.GAME.PLAY
  504. while(true) do
  505. event={os.pullEventRaw()}
  506. type=game.Update(event);
  507. if(event[1]=="terminate") then
  508. type="QUIT"
  509. end
  510. if(type=="PLAY") then
  511. term.clear()
  512. game.room=2
  513. game.Reset()
  514. elseif(type=="UPGRADE") then
  515. term.clear()
  516. game.room=3
  517. elseif(type=="BACK") then
  518. term.clear()
  519. if(game.room==3) then
  520. game.room=1
  521. elseif(game.room==4) then
  522. game.room=1
  523. end
  524. elseif(type=="RESTART") then
  525. if(game.room==4) then
  526. term.clear()
  527. game.Reset()
  528. game.room=2
  529. end
  530. elseif(type=="QUIT") then
  531. term.setBackgroundColor(32768)
  532. term.setTextColor(16)
  533. term.setCursorPos(1,1)
  534. term.clear()
  535. print("Thanks for playing!")
  536. game.saveGame()
  537. game.state=game.GAME.STOP
  538. os.reboot()
  539. return ;
  540. end
  541. game.Draw()
  542. end
  543. end
  544. end,nil)
  545. end)
  546.  
  547. --JaC autogenerated code--
  548.  
  549. if __Methods["Main"] then
  550. if __Methods["Main"].__Classes["Main"] then
  551. local cls=__Methods["Main"].__Classes["Main"]
  552. if cls.Main then
  553. cls.Main(tArgs)
  554. end
  555. end
  556. end
  557. if _Classes.Main then
  558. if _Classes.Main.Main then
  559. _Classes.Main.Main(tArgs)
  560. end
  561. end
  562.  
  563. --End of: JaC autogenerated code--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement