game.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. timer = 0.0
  2. cursor = false
  3. world = nil
  4. camera = nil
  5. up_pressed = false
  6. down_pressed = false
  7. left_pressed = false
  8. right_pressed = false
  9. camera_size = 1.0
  10. ovest = -10
  11. east = 10
  12. north = 10 / 1.6
  13. south = -10 / 1.6
  14. spd_x = 0
  15. spd_y = 0
  16. footsteps = nil
  17. speed = 12
  18. function spawn_obstacle(pos)
  19. if math.random(0, 10) >= 0 and math.random(0, 10) <= 3 then
  20. World.spawn_unit(world, "box", Vector3(pos, -2, 0))
  21. elseif math.random(0, 10) > 3 and math.random(0, 10) <=7 then
  22. World.spawn_unit(world, "box", Vector3(pos, 6, 0))
  23. else
  24. World.spawn_unit(world, "box", Vector3(pos, 3, 0))
  25. end
  26. end
  27. function spawn_obstacles(dim)
  28. for t = 0, dim, 15 do
  29. spawn_obstacle(t)
  30. end
  31. end
  32. function update_player(world, unit, dt)
  33. -- 3 m/s
  34. -- speed = speed + 0.1 * dt;
  35. if (Keyboard.button_pressed(Keyboard.w)) then up_pressed = true end
  36. if (Keyboard.button_pressed(Keyboard.s)) then down_pressed = true end
  37. if (Keyboard.button_pressed(Keyboard.a)) then left_pressed = true end
  38. if (Keyboard.button_pressed(Keyboard.d)) then right_pressed = true end
  39. if (Keyboard.button_released(Keyboard.w)) then up_pressed = false end
  40. if (Keyboard.button_released(Keyboard.s)) then down_pressed = false end
  41. if (Keyboard.button_released(Keyboard.a)) then left_pressed = false end
  42. if (Keyboard.button_released(Keyboard.d)) then right_pressed = false end
  43. if (Touch.pointer_down(1)) then up_pressed = true end
  44. if (Touch.pointer_up(1)) then up_pressed = false end
  45. contr = Unit.controller(unit)
  46. if up_pressed then
  47. spd_y = speed * dt
  48. end
  49. if right_pressed then
  50. spd_x = speed * dt
  51. end
  52. if left_pressed then
  53. spd_x = -speed * dt
  54. end
  55. if not left_pressed and not right_pressed then
  56. spd_x = 0
  57. end
  58. if not up_pressed and not down_pressed then
  59. spd_y = 0
  60. end
  61. Controller.move(contr, Vector3(spd_x, spd_y, 0.0))
  62. spd_y = spd_y - 0.5 * dt
  63. -- local k = 0
  64. -- if spd_x > 0.0 then k = -1
  65. -- elseif spd_x < 0.0 then k = 1 end
  66. -- spd_x = spd_x + k * dt
  67. -- if spd_x < 0.001 and spd_x > -0.001 then spd_x = 0.0 end
  68. end
  69. function update_camera(player_unit, camera_unit, camera)
  70. local player_pos = Unit.world_position(player_unit, 0)
  71. Camera.set_local_position(camera, camera_unit, Vector3(Vector3.x(player_pos), -south / 2.5, 1))
  72. end
  73. function init()
  74. -- -- Set the title of the main window
  75. Window.set_title("Hello world!")
  76. -- Window.show_cursor(cursor)
  77. -- -- Window.minimize();
  78. package = Device.create_resource_package("level")
  79. ResourcePackage.load(package)
  80. ResourcePackage.flush(package)
  81. world = Device.create_world()
  82. -- Spawn camera
  83. camera_unit = World.spawn_unit(world, "camera")
  84. camera = Unit.camera(camera_unit, "camera")
  85. -- Spawn terrain
  86. terrain = World.spawn_unit(world, "terrain", Vector3(0, -3.75, 0), Quaternion(Vector3(0, 0, 1), 1.57))
  87. -- Spawn bounce
  88. bounce = World.spawn_unit(world, "bounce")
  89. sun = World.spawn_unit(world, "sun", Vector3(5, 3, 0))
  90. spawn_obstacles(400)
  91. Camera.set_near_clip_distance(camera, 0.01)
  92. Camera.set_far_clip_distance(camera, 1000)
  93. Camera.set_projection_type(camera, Camera.ORTHOGRAPHIC)
  94. Camera.set_orthographic_metrics(camera, ovest, east, south, north)
  95. Camera.set_viewport_metrics(camera, 0, 0, 1000, 625)
  96. Camera.set_local_position(camera, camera_unit, Vector3(0, 0, 1))
  97. end
  98. function frame(dt)
  99. Device.update_world(world, dt)
  100. -- Update window's viewport
  101. win_w, win_h = Window.get_size()
  102. Camera.set_viewport_metrics(camera, 0, 0, win_w, win_h)
  103. -- Stop the engine when the 'ESC' key is released
  104. if Keyboard.button_released(Keyboard.ESCAPE) then
  105. Device.stop()
  106. end
  107. -- -- Spawn balls when SPACE pressed
  108. -- if Keyboard.button_pressed(Keyboard.SPACE) then
  109. -- World.spawn_unit(world, "b")
  110. -- end
  111. -- Play gunshoot on left click
  112. if Mouse.button_pressed(Mouse.LEFT) then
  113. World.play_sound(world, "sounds/gunshoot")
  114. end
  115. -- Print FPS on window title bar
  116. timer = timer + Device.last_delta_time()
  117. if (timer >= 5) then
  118. Window.set_title(1.0 / Device.last_delta_time())
  119. timer = 0.0
  120. end
  121. -- Update the player
  122. update_player(world, bounce, dt)
  123. -- Update the camera
  124. update_camera(bounce, camera_unit, camera)
  125. -- Render world
  126. Device.render_world(world, camera)
  127. end
  128. function shutdown()
  129. ResourcePackage.unload(package)
  130. Device.destroy_resource_package(package)
  131. Device.destroy_world(world)
  132. end