handle joystick input

use rotation from joystick
This commit is contained in:
Anish Lakhwara 2022-06-11 00:59:19 +10:00
parent a210317ab4
commit e55ba81d94
3 changed files with 52 additions and 17 deletions

View file

@ -1,10 +1,11 @@
love.conf = function(t)
t.gammacorrect = true
t.title, t.identity = "change-me", "Minimal"
t.modules.joystick = false
t.modules.joystick = true
t.modules.physics = false
t.window.width = 720
t.window.height = 450
t.window.vsync = false
t.console = true
t.version = "11.4"
end

View file

@ -7,41 +7,64 @@
(local sheet (love.graphics.newImage "images/MRMOTEXT.png"))
(fn split-images [sheet height width]
(var images {})
(var count 0)
(var images [])
(for [y 0 (- (sheet:getHeight) height) height]
(for [x 0 (- (sheet:getWidth) width) width]
(tset images count (love.graphics.newQuad x y width height (sheet:getDimensions)))
(set count (+ count 1))
(table.insert images (love.graphics.newQuad x y width height (sheet:getDimensions)))
))
images)
(local sprites (split-images sheet 8 8))
(fn get-image [sprites row column]
(. sprites (+ (* 32 row) column)))
(. sprites (+ (* 32 row) (+ column 1))))
(local player (get-image sprites 13 1))
(var player-data {:x 100
:y 100})
:y 100
:rotation 0
:image (get-image sprites 13 1)})
(fn get-player-x []
(. player-data :x))
(fn get-player-y []
(. player-data :y))
(fn get-player-rotation []
(. player-data :rotation))
(fn move-player [x1 y1]
(tset player-data :x (+ (. player-data :x) x1))
(tset player-data :y (+ (. player-data :y) y1)))
(fn rotate-player [rx ry]
(tset player-data :rotation (math.atan2 rx ry)))
(fn handle-input [joystick]
(local x (joystick.obj:getAxis 1))
(local y (joystick.obj:getAxis 2))
(local rx (joystick.obj:getAxis 4))
(local ry (joystick.obj:getAxis 5))
(move-player x y)
(rotate-player rx ry))
(fn draw-player []
(love.graphics.setColor 0.12 0.78 0.55)
(love.graphics.draw sheet (. player-data :image) (get-player-x) (get-player-y) (get-player-rotation) 2 2 4 4))
{:draw (fn draw [message]
(local (w h _flags) (love.window.getMode))
(love.graphics.draw sheet player (get-player-x) (get-player-y) 0 2 2)
(draw-player)
)
:update (fn update [dt set-mode]
(incf time dt))
:update (fn update [dt joystick set-mode]
(handle-input joystick)
(incf time dt))
:keypressed (fn keypressed [key set-mode]
(let [step 16]
(if (love.keyboard.isDown ".")
(tset player-data :y (- (. player-data :y) 8)))
(tset player-data :y (- (. player-data :y) step)))
(if (love.keyboard.isDown "e")
(tset player-data :y (+ (. player-data :y) 8)))
(tset player-data :y (+ (. player-data :y) step)))
(if (love.keyboard.isDown "u")
(tset player-data :x (+ (. player-data :x) 8)))
(tset player-data :x (+ (. player-data :x) step)))
(if (love.keyboard.isDown "o")
(tset player-data :x (- (. player-data :x) 8)))
)}
(tset player-data :x (- (. player-data :x) step)))
))}

View file

@ -4,6 +4,7 @@
(love.graphics.newCanvas w h)))
(var scale 1)
(var Joystick {})
;; set the first mode
(var (mode mode-name) nil)
@ -14,6 +15,16 @@
(match (pcall mode.activate ...)
(false msg) (print mode-name "activate error" msg))))
(fn love.joystickadded [joystick]
(tset Joystick :name (joystick:getName))
(tset Joystick :axesNum (joystick:getAxisCount))
(tset Joystick :buttonsNum (joystick:getButtonCount))
(tset Joystick :hatsNum (joystick:getHatCount))
(tset Joystick :guid (joystick:getGUID))
(tset Joystick :id (joystick:getID))
(print (. Joystick :id))
(tset Joystick :obj joystick))
(fn love.load [args]
(set-mode :mode-intro)
(canvas:setFilter "nearest" "nearest")
@ -35,7 +46,7 @@
(fn love.update [dt]
(when mode.update
(safely #(mode.update dt set-mode))))
(safely #(mode.update dt Joystick set-mode))))
(fn love.keypressed [key]
(if (and (love.keyboard.isDown "lctrl" "rctrl" "capslock") (= key "q"))