Main.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. scene = PhysicsScene(0, Vector3(200, 200, 200))
  2. ground = ScenePrimitive(ScenePrimitive.TYPE_PLANE, 10, 10)
  3. ground:loadTexture("Resources/green_texture.png")
  4. scene:addPhysicsChild(ground, 6, 0)
  5. for i = 1, 1 do
  6. local box = ScenePrimitive(ScenePrimitive.TYPE_BOX, 0.8, 0.8, 0.8)
  7. box:loadTexture("Resources/pink_texture.png")
  8. box:Roll(-45 + math.random() % 90)
  9. box:Pitch(-45 + math.random() % 90)
  10. box:setPosition(-2 + math.random() % 4, i * 0.5, -2 + math.random() % 4)
  11. scene:addPhysicsChild(box, 0, 1)
  12. end
  13. player = ScenePrimitive(ScenePrimitive.TYPE_BOX, 0.5, 1, 0.5)
  14. player:loadTexture("Resources/pink_texture.png")
  15. player:setColor(1, 1, 0, 1)
  16. player:setPosition(2, 1, 2)
  17. playerController = scene:addCharacterChild(player, 10, 1, 0.5)
  18. local walkSpeed = 0
  19. local rotateSpeed = 0
  20. local playerDirection = 0
  21. testBox = ScenePrimitive(ScenePrimitive.TYPE_BOX, 2, 2, 2)
  22. testBox:loadTexture("Resources/pink_texture.png")
  23. testBox:setColor(0.3, 0.5, 1, 0.4)
  24. testBox:setPosition(2,1,-2)
  25. scene:addCollisionChild(testBox, 0)
  26. hud = Scene(Scene.SCENE_2D_TOPLEFT)
  27. onGroundLabel = SceneLabel("Arrow keys to control, spacebar to jump, press R to reset", 16)
  28. onGroundLabel:setAnchorPoint(Vector3(-1, -1, 0))
  29. onGroundLabel:setPosition(0,0)
  30. hud:addChild(onGroundLabel)
  31. onGroundLabel = SceneLabel("On Ground:", 16)
  32. onGroundLabel:setAnchorPoint(Vector3(-1, -1, 0))
  33. onGroundLabel:setPosition(0, 32)
  34. hud:addChild(onGroundLabel)
  35. scene:getDefaultCamera():setPosition(7, 7, 7)
  36. scene:getDefaultCamera():lookAt(Vector3(0, 0, 0), Vector3(1, 1, 1))
  37. function handleKeyEvent(t, e)
  38. if not e:getDispatcher() == CoreServices.getInstance():getCore():getInput() then return end
  39. local inputEvent = safe_cast(e, InputEvent)
  40. local eventKeyCode = e:getEventCode()
  41. if eventKeyCode == InputEvent.EVENT_KEYDOWN then
  42. local keyCode = inputEvent:keyCode()
  43. if keyCode == KEY_r then
  44. playerController:warpCharacter(Vector3(2, 1, 2))
  45. elseif keyCode == KEY_UP then
  46. walkSpeed = 0.05
  47. elseif keyCode == KEY_DOWN then
  48. walkSpeed = -0.05
  49. elseif keyCode == KEY_LEFT then
  50. rotateSpeed = 3
  51. elseif keyCode == KEY_RIGHT then
  52. rotateSpeed = -3
  53. elseif keyCode == KEY_SPACE then
  54. playerController:jump()
  55. end
  56. elseif eventKeyCode == InputEvent.EVENT_KEYUP then
  57. if inputEvent.key == KEY_DOWN then
  58. walkSpeed = 0
  59. elseif inputEvent.key == KEY_RIGHT then
  60. rotateSpeed = 0
  61. end
  62. end
  63. end
  64. CoreServices.getInstance():getCore():getInput():addEventListener(nil, handleKeyEvent, InputEvent.EVENT_KEYDOWN)
  65. CoreServices.getInstance():getCore():getInput():addEventListener(nil, handleKeyEvent, InputEvent.EVENT_KEYUP)
  66. function Update(elapsed)
  67. playerDirection = playerDirection + rotateSpeed * elapsed
  68. player:setYaw(playerDirection * (180 / math.pi))
  69. playerController:setWalkDirection(Vector3(walkSpeed * math.cos(playerDirection), 0, walkSpeed * math.sin(-playerDirection)))
  70. if playerController:onGround() then
  71. onGroundLabel:setText("On Ground: YES")
  72. else
  73. onGroundLabel:setText("On Ground: NO")
  74. end
  75. res = scene:testCollision(player, testBox)
  76. if res.collided then
  77. testBox:setColor(1, 1, 0, 0.5)
  78. else
  79. testBox:setColor(0, 1, 1, 0.5)
  80. end
  81. end