# The play screen import a.ggez import a.math::TAU import a.math::PI import a.math::atan2 import a.math::cos import a.math::sin import a.time @class Bullet { [sprite_id, x, y, dx, dy] } def update(app, model) { W = H = 800 background_mesh = nil last_update_time = nil input_axis_x = input_axis_y = 0 def build_background_mesh(ctx) = { ggez::MeshBuilder().rect(0, 0, W, H, ggez::BLACK).build(ctx) } def force_rotation_update() { model.update_ship_rotation(input_axis_x * 10, input_axis_y * 10) } app.update( init=def(ctx) { nonlocal background_mesh nonlocal last_update_time ctx.set_window_size(W, H) ctx.set_screen_coordinates(0, 0, W, H) model.init(ctx, W, H) background_mesh = build_background_mesh(ctx) last_update_time = time::now() }, update=def(ctx) { nonlocal last_update_time now = time::now() diff = now - last_update_time last_update_time = now model.update(ctx, diff, input_axis_x, input_axis_y) }, draw=def(ctx) { background_mesh.draw(ctx) model.draw(ctx) }, mouse_down=def(ctx, btn, x, y) { # Mostly for debugging -- helps with figuring out coordinates of # what's drawn print(['mouse', btn, x, y]) }, key_down=def(ctx, key, mods) { nonlocal input_axis_x, input_axis_y if key is :Escape { ctx.quit() } elif key is :A { input_axis_x = -0.5 force_rotation_update() } elif key is :D { input_axis_x = 0.5 force_rotation_update() } elif key is :W { input_axis_y = 0.5 force_rotation_update() } elif key is :S { input_axis_y = -0.5 force_rotation_update() } elif key is :J { model.shoot(ctx) } else { print('Unrecognized key %r (%r)' % [key, mods]) } }, key_up=def(ctx, key, mods) { nonlocal input_axis_x, input_axis_y if key is :A or key is :D { input_axis_x = 0 force_rotation_update() } elif key is :W or key is :S { input_axis_y = 0 force_rotation_update() } }, gamepad_button_down=def(ctx, btn, gamepad) { if btn is :South { model.shoot(ctx) } else { print('Unrecognized button %r (%r)' % [btn, gamepad]) } }, gamepad_axis=def(ctx, axis, val, gamepad) { nonlocal input_axis_x, input_axis_y if axis is :LeftStickY { input_axis_y = val } elif axis is :LeftStickX { input_axis_x = val } else { print('Unrecognized axis %r, (%r, %r)' % [axis, val, gamepad]) } }, ) }