Advertisement
LPGhatguy

(old) GameEngineRev2.lua (for TI-Nspire CX)

Sep 29th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.62 KB | None | 0 0
  1. local Game = {
  2.     Ticks = 0,
  3.     Window = {0, 0},
  4.     VisibleTiles = {0, 0},
  5.     Redraw = true
  6. }
  7.  
  8. local UnitDraw = {}
  9.  
  10. local Board = {}
  11.  
  12. function NewBoard(width, height)
  13.     local New = {}
  14.  
  15.     for y = 1, height do
  16.         New[y] = {}
  17.         for x = 1, width do
  18.             New[y][x] = 0
  19.         end
  20.     end
  21.    
  22.     return New
  23. end
  24.  
  25. function on.create()
  26.     Board = NewBoard(100, 100)
  27. end
  28.  
  29. function on.paint(gc)
  30.     gc:setColorRGB(0, 0, 0)
  31.     gc:setFont("serif", "r", 12)
  32.     gc:drawString(DrawBoard(1, 1), 0, 0)
  33.    
  34.     gc:drawRect(0, 0, Game.Window[1] * 0.9, Game.Window[2] * 0.9)
  35. end
  36.  
  37. function DrawBoard(fromx, fromy)
  38.     local DrawString = ""
  39.     for y = fromy, #Board do
  40.         if (y - fromy > Game.VisibleTiles[2]) then
  41.             break
  42.         end
  43.         for x = fromx, #Board[y] do
  44.             if (x - fromx > Game.VisibleTiles[1]) then
  45.                 break
  46.             end
  47.            
  48.             if (Board[y][x] == 0) then
  49.                 DrawString = DrawString .. math.random(0, 9)
  50.             else
  51.                 --TODO: Draw other types
  52.             end
  53.         end
  54.         DrawString = DrawString .. "\n"
  55.     end
  56.    
  57.     return DrawString
  58. end
  59.  
  60. function on.timer()
  61.     Game.Ticks = Game.Ticks + 1
  62.    
  63.     Game.Redraw = true
  64.    
  65.     if (Game.Redraw) then
  66.         Game.Redraw = false
  67.         platform.window:invalidate()
  68.     end
  69. end
  70.  
  71. function on.activate()
  72.     timer.start(1 / 30)
  73.     Game.Redraw = true
  74. end
  75.  
  76. function on.deactivate()
  77.     timer.stop()
  78. end
  79.  
  80. function on.resize(width, height)
  81.     Game.Window = {width, height}
  82.     CalculatePercents()
  83. end
  84.  
  85. function CalculatePercents()
  86.     local gc = platform.gc()
  87.     gc:begin()
  88.     Game.VisibleTiles = {math.floor((Game.Window[1] * 0.9) / gc:getStringWidth("0")), math.floor((Game.Window[2] * 0.9) / gc:getStringHeight("0"))}
  89.     gc:finish()
  90.    
  91.     Game.Redraw = true
  92. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement