Main.lua 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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(0, 1, 0))
  37. function onKeyDown(keyCode)
  38. if keyCode == KEY_r then
  39. playerController:warpCharacter(Vector3(2, 1, 2))
  40. elseif keyCode == KEY_UP then
  41. walkSpeed = 0.05
  42. elseif keyCode == KEY_DOWN then
  43. walkSpeed = -0.05
  44. elseif keyCode == KEY_LEFT then
  45. rotateSpeed = 3
  46. elseif keyCode == KEY_RIGHT then
  47. rotateSpeed = -3
  48. elseif keyCode == KEY_SPACE then
  49. playerController:jump()
  50. end
  51. end
  52. function onKeyUp(keyCode)
  53. if keyCode == KEY_DOWN or keyCode == KEY_UP then
  54. walkSpeed = 0
  55. elseif keyCode == KEY_RIGHT or keyCode == KEY_LEFT then
  56. rotateSpeed = 0
  57. end
  58. end
  59. function Update(elapsed)
  60. playerDirection = playerDirection + rotateSpeed * elapsed
  61. player:setYaw(playerDirection * (180 / math.pi))
  62. playerController:setWalkDirection(Vector3(walkSpeed * math.cos(playerDirection), 0, walkSpeed * math.sin(-playerDirection)))
  63. if playerController:onGround() then
  64. onGroundLabel:setText("On Ground: YES")
  65. else
  66. onGroundLabel:setText("On Ground: NO")
  67. end
  68. res = scene:testCollision(player, testBox)
  69. if res.collided then
  70. testBox:setColor(1, 1, 0, 0.5)
  71. else
  72. testBox:setColor(0, 1, 1, 0.5)
  73. end
  74. end