game.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. -- This lua script file represents a lua implementation translation of sample00-mesh with a box instead of a duck.
  2. function initialize()
  3. -- Display splash screen for at least 1 second.
  4. ScreenDisplayer.start("drawSplash", 1000)
  5. _touched = false
  6. _touchX = 0
  7. -- Load font
  8. _font = Font.create("res/ui/arial.gpb")
  9. -- Load mesh/scene from file
  10. _scene = Scene.load("res/lua.scene")
  11. -- Get the box node
  12. _modelNode = _scene:findNode("box")
  13. -- Find the light node
  14. local lightNode = _scene:findNode("directionalLight1")
  15. -- Bind the light node's direction into the box material.
  16. _modelNode:getModel():getMaterial():getParameter("u_directionalLightColor[0]"):setValue(lightNode:getLight():getColor())
  17. _modelNode:getModel():getMaterial():getParameter("u_directionalLightDirection[0]"):bindValue(lightNode, "&Node::getForwardVectorWorld")
  18. -- Update the aspect ratio for our scene's camera to match the current device resolution
  19. local game = Game.getInstance()
  20. _scene:getActiveCamera():setAspectRatio(game:getWidth() / game:getHeight())
  21. -- Create the grid and add it to the scene.
  22. local model = createGridModel()
  23. _scene:addNode("grid"):setModel(model)
  24. dofile("res/ai.lua")
  25. ScreenDisplayer.finish()
  26. end
  27. function update(elapsedTime)
  28. end
  29. -- Avoid allocating new objects every frame.
  30. textColor = Vector4.new(0, 0.5, 1, 1)
  31. function render(elapsedTime)
  32. -- Clear the color and depth buffers.
  33. Game.getInstance():clear(Game.CLEAR_COLOR_DEPTH, Vector4.zero(), 1.0, 0)
  34. -- Visit all the nodes in the scene, drawing the models/mesh.
  35. _scene:visit("drawScene")
  36. -- Draw the fps.
  37. local buffer = string.format("%u\n%s", Game.getInstance():getFrameRate(), _stateMachine:getActiveState():getId())
  38. _font:start()
  39. _font:drawText(buffer, 5, 1, textColor, _font:getSize())
  40. _font:finish()
  41. end
  42. function finalize()
  43. _font = nil
  44. _scene = nil
  45. end
  46. function drawScene(node)
  47. local model = node:getModel()
  48. if model then
  49. model:draw()
  50. end
  51. return true
  52. end
  53. function drawSplash()
  54. local game = Game.getInstance()
  55. game:clear(Game.CLEAR_COLOR_DEPTH, 0, 0, 0, 1, 1.0, 0)
  56. local batch = SpriteBatch.create("res/logo_powered_white.png")
  57. batch:start()
  58. batch:draw(game:getWidth() * 0.5, game:getHeight() * 0.5, 0.0, 512.0, 512.0, 0.0, 1.0, 1.0, 0.0, Vector4.one(), true)
  59. batch:finish()
  60. end
  61. function keyEvent(evt, key)
  62. if evt == Keyboard.KEY_PRESS then
  63. if key == Keyboard.KEY_ESCAPE then
  64. Game.getInstance():exit()
  65. end
  66. end
  67. end
  68. function touchEvent(evt, x, y, contactIndex)
  69. if evt == Touch.TOUCH_PRESS then
  70. _touchTime = Game.getAbsoluteTime()
  71. _touched = true
  72. _touchX = x
  73. elseif evt == Touch.TOUCH_RELEASE then
  74. _touched = false
  75. _touchX = 0
  76. -- Basic emulation of tap to change state
  77. if (Game.getAbsoluteTime() - _touchTime) < 200 then
  78. toggleState()
  79. end
  80. elseif evt == Touch.TOUCH_MOVE then
  81. local deltaX = x - _touchX
  82. _touchX = x
  83. _modelNode:rotateY(math.rad(deltaX * 0.5))
  84. end
  85. end
  86. function createGridModel()
  87. local lineCount = 41
  88. local pointCount = lineCount * 4
  89. local verticesSize = pointCount * (3 + 3)
  90. local vertices = {}
  91. local gridLength = math.floor(lineCount / 2)
  92. local value = -gridLength
  93. while #vertices + 1 < verticesSize do
  94. -- Default line color is dark grey
  95. local red, green, blue = 0.3, 0.3, 0.3
  96. -- Every 10th line is brighter grey
  97. if math.floor(value + 0.5) % 10 == 0 then
  98. red, green, blue = 0.45, 0.45, 0.45
  99. end
  100. -- The Z axis is blue
  101. if value == 0 then
  102. red, green, blue = 0.15, 0.15, 0.7
  103. end
  104. -- Build the lines
  105. vertices[#vertices+1] = value
  106. vertices[#vertices+1] = 0.0
  107. vertices[#vertices+1] = -gridLength
  108. vertices[#vertices+1] = red
  109. vertices[#vertices+1] = green
  110. vertices[#vertices+1] = blue
  111. vertices[#vertices+1] = value
  112. vertices[#vertices+1] = 0.0
  113. vertices[#vertices+1] = gridLength
  114. vertices[#vertices+1] = red
  115. vertices[#vertices+1] = green
  116. vertices[#vertices+1] = blue
  117. -- The X axis is red
  118. if value == 0.0 then
  119. red, green, blue = 0.7, 0.15, 0.15
  120. end
  121. vertices[#vertices+1] = -gridLength
  122. vertices[#vertices+1] = 0.0
  123. vertices[#vertices+1] = value
  124. vertices[#vertices+1] = red
  125. vertices[#vertices+1] = green
  126. vertices[#vertices+1] = blue
  127. vertices[#vertices+1] = gridLength
  128. vertices[#vertices+1] = 0.0
  129. vertices[#vertices+1] = value
  130. vertices[#vertices+1] = red
  131. vertices[#vertices+1] = green
  132. vertices[#vertices+1] = blue
  133. value = value + 1.0
  134. end
  135. local elements = {
  136. VertexFormat.Element.new(VertexFormat.POSITION, 3),
  137. VertexFormat.Element.new(VertexFormat.COLOR, 3)
  138. }
  139. local mesh = Mesh.createMesh(VertexFormat.new(elements, 2), pointCount, false)
  140. if mesh == nil then
  141. return nil, "Error creating grid mesh."
  142. end
  143. mesh:setPrimitiveType(Mesh.LINES)
  144. mesh:setVertexData(vertices, 0, pointCount)
  145. local model = Model.create(mesh)
  146. model:setMaterial("res/lua.material#grid")
  147. return model
  148. end