From: Alexis Laferrière Date: Sun, 2 Apr 2017 17:38:28 +0000 (-0400) Subject: asteronits: use the new touch gamepad X-Git-Url: http://nitlanguage.org asteronits: use the new touch gamepad Signed-off-by: Alexis Laferrière --- diff --git a/contrib/asteronits/Makefile b/contrib/asteronits/Makefile index fef1d76..aee8027 100644 --- a/contrib/asteronits/Makefile +++ b/contrib/asteronits/Makefile @@ -9,14 +9,10 @@ bin/asteronits: $(shell ${NITLS} -M src/asteronits.nit linux) pre-build bin/texture_atlas_parser: ../../lib/gamnit/texture_atlas_parser.nit ${NITC} ../../lib/gamnit/texture_atlas_parser.nit -o $@ -src/controls.nit: art/controls.svg - make -C ../inkscape_tools/ - ../inkscape_tools/bin/svg_to_png_and_nit art/controls.svg -a assets/ -s src/ -x 2.0 -g - src/spritesheet.nit: bin/texture_atlas_parser bin/texture_atlas_parser art/sheet.xml --dir src/ -n spritesheet -pre-build: src/controls.nit src/spritesheet.nit +pre-build: src/spritesheet.nit check: bin/asteronits NIT_TESTING=true bin/asteronits diff --git a/contrib/asteronits/art/controls.svg b/contrib/asteronits/art/controls.svg deleted file mode 100644 index 903898d..0000000 --- a/contrib/asteronits/art/controls.svg +++ /dev/null @@ -1,2198 +0,0 @@ - - - - - - image/svg+xml - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/contrib/asteronits/src/asteronits.nit b/contrib/asteronits/src/asteronits.nit index 080957b..2555352 100644 --- a/contrib/asteronits/src/asteronits.nit +++ b/contrib/asteronits/src/asteronits.nit @@ -224,15 +224,15 @@ redef class KeyEvent # How does this event affect the ship thrust? fun thrust: Float do - if is_arrow_up or name == "w" then return 1.0 + if name == "up" or name == "w" then return 1.0 return 0.0 end # How does this event affect the ship thrust? fun rotation: Float do - if is_arrow_right or name == "d" then return -1.0 - if is_arrow_left or name == "a" then return 1.0 + if name == "right" or name == "d" then return -1.0 + if name == "left" or name == "a" then return 1.0 return 0.0 end end diff --git a/contrib/asteronits/src/touch_ui.nit b/contrib/asteronits/src/touch_ui.nit index 388cd35..bd8bfa5 100644 --- a/contrib/asteronits/src/touch_ui.nit +++ b/contrib/asteronits/src/touch_ui.nit @@ -15,103 +15,20 @@ # Touchscreen UI for mobile devices module touch_ui +import gamnit::virtual_gamepad + import asteronits -import controls redef class App - - # Controls texture - var spritesheet_controls = new ControlsImages - - private var joystick_x = 200.0 - private var joystick_y = 100.0 - - redef fun accept_event(event) - do - super - - var display = display - if display == null then return false - - if event isa PointerEvent and not event.is_move then - - # Convert input coordinates from screen coordinates to UI units. - # Effectively reverting the transformation created by `ui_camera.reset_height`. - var ui_pos = ui_camera.camera_to_ui(event.x, event.y) - - var ship = world.ship - - if ui_pos.y.to_i > display.height/2 then - # Lower half of the screen - if ui_pos.x.to_i > display.width/2 then - # Bottom right - if event.pressed then ship.fire - else - # Bottom left - var dx = ui_pos.x - joystick_x - var dy = ui_pos.y - (ui_camera.height - joystick_y) - - # Any touch in the joystick reset all joystick effects. - # It prevents leaving a button without releasing by moving - # the pointer over another button. - ship.applied_rotation = 0.0 - ship.applied_thrust = 0.0 - - if not event.pressed then return true - - if dy > 0.0 then - # Bottom part of the joystick, turns left or right - if dx < 0.0 then - ship.applied_rotation = 1.0 - else - ship.applied_rotation = -1.0 - end - else - # Upper part of the joystick, detect action using 45d angles - if dx < dy then - ship.applied_rotation = 1.0 - else if dx > -dy then - ship.applied_rotation = -1.0 - else - ship.applied_thrust = 1.0 - end - end - end - end - return true - end - - return false - end - redef fun on_create do super - var display = display - assert display != null - - # Standardize the UI size to look like a 1600 pixels high screen. - # Meaning that the controls have a size proportional to the height of each screen. - # In the code, we can use "pixel" precision as if the target screen was 1600 pixels high. - ui_camera.reset_height 800.0 - - # Add the joystick to the UI - ui_sprites.add new Sprite(spritesheet_controls.forward, - ui_camera.bottom_left.offset(joystick_x, 200.0, 0.0)) - ui_sprites.add new Sprite(spritesheet_controls.left, - ui_camera.bottom_left.offset(joystick_x-100.0, joystick_y, 0.0)) - ui_sprites.add new Sprite(spritesheet_controls.right, - ui_camera.bottom_left.offset(joystick_x+100.0, joystick_y, 0.0)) - - # Purely cosmetic joystick background - ui_sprites.add new Sprite(spritesheet_controls.joystick_back, - ui_camera.bottom_left.offset(joystick_x, joystick_y, -1.0)) # In the back - ui_sprites.add new Sprite(spritesheet_controls.joystick_down, - ui_camera.bottom_left.offset(joystick_x, 0.0, 1.0)) - - # Add the "open fire" button - ui_sprites.add new Sprite(spritesheet_controls.fire, - ui_camera.bottom_right.offset(-150.0, 150.0, 0.0)) + var gamepad = new VirtualGamepad + gamepad.add_dpad + gamepad.controls.first.as(DPad).show_down = false + gamepad.add_button("space", gamepad_spritesheet.fire) + gamepad.visible = true + self.gamepad = gamepad end end