game.lua 3.5 KB

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