| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- require "core/game/camera"
- Game = Game or {}
- Game = {
- pw = nil,
- rw = nil,
- sg = nil,
- debug_graphics = false,
- debug_physics = false,
- mode = false,
- camera = nil,
- }
- GameBase.game = Game
- GameBase.game_level = "test"
- function Game.level_loaded()
- Device.enable_resource_autoload(true)
- Game.pw = World.physics_world(GameBase.world)
- Game.rw = World.render_world(GameBase.world)
- Game.sg = World.scene_graph(GameBase.world)
- -- Spawn camera
- local camera_unit = World.spawn_unit(GameBase.world, "core/units/camera")
- SceneGraph.set_local_position(Game.sg, camera_unit, Vector3(0, 6.5, -30))
- GameBase.game_camera = camera_unit
- Game.camera = FPSCamera(GameBase.world, camera_unit)
- -- Debug
- PhysicsWorld.enable_debug_drawing(Game.pw, debug_physics)
- RenderWorld.enable_debug_drawing(Game.rw, debug_graphics)
- end
- function Game.update(dt)
- -- Stop the engine when the 'ESC' key is released
- if Keyboard.released(Keyboard.button_id("escape")) then
- Device.quit()
- end
- if Keyboard.released(Keyboard.button_id("z")) then
- debug_physics = not debug_physics
- PhysicsWorld.enable_debug_drawing(Game.pw, debug_physics)
- end
- if Keyboard.released(Keyboard.button_id("x")) then
- debug_graphics = not debug_graphics
- RenderWorld.enable_debug_drawing(Game.rw, debug_graphics)
- end
- -- Spawn a sphere when left mouse button is pressed
- if Mouse.pressed(Mouse.button_id("left")) then
- local pos = SceneGraph.local_position(Game.sg, Game.camera:unit())
- local dir = Matrix4x4.z(SceneGraph.local_pose(Game.sg, Game.camera:unit()))
- local u1 = World.spawn_unit(GameBase.world, "sphere", pos)
- local a1 = PhysicsWorld.actor_instances(Game.pw, u1)
- Vector3.normalize(dir)
- PhysicsWorld.actor_add_impulse(Game.pw, a1, dir * 500.0)
- end
- -- Perform a raycast when middle mouse button is pressed
- if Mouse.pressed(Mouse.button_id("middle")) then
- local pos = SceneGraph.local_position(Game.sg, Game.camera:unit())
- local dir = Matrix4x4.z(SceneGraph.local_pose(Game.sg, Game.camera:unit()))
- local hits = PhysicsWorld.raycast(Game.pw, pos, dir, 100, "closest")
- if #hits > 0 then
- PhysicsWorld.actor_add_impulse(Game.pw, hits[1], dir * 400.0)
- end
- end
- -- Update camera
- local delta = Vector3.zero()
- if Mouse.pressed(Mouse.button_id("right")) then move = true end
- if Mouse.released(Mouse.button_id("right")) then move = false end
- if move then delta = Mouse.axis(Mouse.axis_id("cursor_delta")) end
- Game.camera:update(-delta.x, -delta.y)
- end
- function Game.render(dt)
- end
- function Game.shutdown()
- end
|