game.lua 5.4 KB

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