game.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. -- Load the AI script
  25. game:getScriptController():loadScript("res/ai.lua")
  26. ScreenDisplayer.finish()
  27. end
  28. function update(elapsedTime)
  29. end
  30. -- Avoid allocating new objects every frame.
  31. textColor = Vector4.new(0, 0.5, 1, 1)
  32. function render(elapsedTime)
  33. -- Clear the color and depth buffers.
  34. Game.getInstance():clear(Game.CLEAR_COLOR_DEPTH, Vector4.zero(), 1.0, 0)
  35. -- Visit all the nodes in the scene, drawing the models/mesh.
  36. _scene:visit("drawScene")
  37. -- Draw the fps.
  38. local buffer = string.format("%u\n%s", Game.getInstance():getFrameRate(), _stateMachine:getActiveState():getId())
  39. _font:start()
  40. _font:drawText(buffer, 5, 1, textColor, 18)
  41. _font:finish()
  42. end
  43. function finalize()
  44. _font = nil
  45. _scene = nil
  46. end
  47. function drawScene(node)
  48. local model = node:getModel()
  49. if model then
  50. model:draw()
  51. end
  52. return true
  53. end
  54. function drawSplash()
  55. local game = Game.getInstance()
  56. game:clear(Game.CLEAR_COLOR_DEPTH, 0, 0, 0, 1, 1.0, 0)
  57. local batch = SpriteBatch.create("res/logo_powered_white.png")
  58. batch:start()
  59. 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)
  60. batch:finish()
  61. end
  62. function keyEvent(evt, key)
  63. if evt == Keyboard.KEY_PRESS then
  64. if key == Keyboard.KEY_ESCAPE then
  65. Game.getInstance():exit()
  66. end
  67. end
  68. end
  69. function touchEvent(evt, x, y, contactIndex)
  70. if evt == Touch.TOUCH_PRESS then
  71. _touchTime = Game.getAbsoluteTime()
  72. _touched = true
  73. _touchX = x
  74. elseif evt == Touch.TOUCH_RELEASE then
  75. _touched = false
  76. _touchX = 0
  77. -- Basic emulation of tap to change state
  78. if (Game.getAbsoluteTime() - _touchTime) < 200 then
  79. toggleState()
  80. end
  81. elseif evt == Touch.TOUCH_MOVE then
  82. local deltaX = x - _touchX
  83. _touchX = x
  84. _modelNode:rotateY(math.rad(deltaX * 0.5))
  85. end
  86. end
  87. function createGridModel()
  88. local lineCount = 41
  89. local pointCount = lineCount * 4
  90. local verticesSize = pointCount * (3 + 3)
  91. local vertices = {}
  92. local gridLength = math.floor(lineCount / 2)
  93. local value = -gridLength
  94. while #vertices + 1 < verticesSize do
  95. -- Default line color is dark grey
  96. local red, green, blue = 0.3, 0.3, 0.3
  97. -- Every 10th line is brighter grey
  98. if math.floor(value + 0.5) % 10 == 0 then
  99. red, green, blue = 0.45, 0.45, 0.45
  100. end
  101. -- The Z axis is blue
  102. if value == 0 then
  103. red, green, blue = 0.15, 0.15, 0.7
  104. end
  105. -- Build the lines
  106. vertices[#vertices+1] = value
  107. vertices[#vertices+1] = 0.0
  108. vertices[#vertices+1] = -gridLength
  109. vertices[#vertices+1] = red
  110. vertices[#vertices+1] = green
  111. vertices[#vertices+1] = blue
  112. vertices[#vertices+1] = value
  113. vertices[#vertices+1] = 0.0
  114. vertices[#vertices+1] = gridLength
  115. vertices[#vertices+1] = red
  116. vertices[#vertices+1] = green
  117. vertices[#vertices+1] = blue
  118. -- The X axis is red
  119. if value == 0.0 then
  120. red, green, blue = 0.7, 0.15, 0.15
  121. end
  122. vertices[#vertices+1] = -gridLength
  123. vertices[#vertices+1] = 0.0
  124. vertices[#vertices+1] = value
  125. vertices[#vertices+1] = red
  126. vertices[#vertices+1] = green
  127. vertices[#vertices+1] = blue
  128. vertices[#vertices+1] = gridLength
  129. vertices[#vertices+1] = 0.0
  130. vertices[#vertices+1] = value
  131. vertices[#vertices+1] = red
  132. vertices[#vertices+1] = green
  133. vertices[#vertices+1] = blue
  134. value = value + 1.0
  135. end
  136. local elements = {
  137. VertexFormat.Element.new(VertexFormat.POSITION, 3),
  138. VertexFormat.Element.new(VertexFormat.COLOR, 3)
  139. }
  140. local mesh = Mesh.createMesh(VertexFormat.new(elements, 2), pointCount, false)
  141. if mesh == nil then
  142. return nil, "Error creating grid mesh."
  143. end
  144. mesh:setPrimitiveType(Mesh.LINES)
  145. mesh:setVertexData(vertices, 0, pointCount)
  146. local model = Model.create(mesh)
  147. model:setMaterial("res/lua.material#grid")
  148. return model
  149. end