summaryrefslogtreecommitdiffstats
path: root/src/WiimotePlayer.hs
diff options
context:
space:
mode:
authorMatthias Schiffer <matthias@gamezock.de>2010-04-07 13:28:38 +0200
committerMatthias Schiffer <matthias@gamezock.de>2010-04-07 13:28:38 +0200
commitc0d2d54ea1687a80ff76fa032ad4dc89670d2988 (patch)
treefce2ebd5d0fea025004e666a083fa38207c23663 /src/WiimotePlayer.hs
parent736ad91b328c23287b6065f6dc8f75c424d84b8b (diff)
downloadhtanks-c0d2d54ea1687a80ff76fa032ad4dc89670d2988.tar
htanks-c0d2d54ea1687a80ff76fa032ad4dc89670d2988.zip
Added Wiimote support
Diffstat (limited to 'src/WiimotePlayer.hs')
-rw-r--r--src/WiimotePlayer.hs44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/WiimotePlayer.hs b/src/WiimotePlayer.hs
new file mode 100644
index 0000000..2487d17
--- /dev/null
+++ b/src/WiimotePlayer.hs
@@ -0,0 +1,44 @@
+{-# LANGUAGE DeriveDataTypeable #-}
+
+module WiimotePlayer ( WiimotePlayer(..)
+ , newWiimotePlayer
+ ) where
+
+import Control.Monad
+import Data.Bits
+import Data.Ratio ((%))
+import Data.Typeable
+import HWiid
+
+import Player
+
+
+data WiimotePlayer = WiimotePlayer Wiimote
+ deriving (Typeable, Show)
+
+instance Player WiimotePlayer where
+ playerUpdate (WiimotePlayer wiimote) tank = do
+ buttons <- hwiidGetState wiimote >>= return . stateButtons
+ messages <- hwiidGetMesg wiimote
+
+ let shoot = any (\m -> (mesgType m == hwiidMesgTypeButton) && (test (mesgButtons m) hwiidButtonB)) $ messages
+ x = (if (test buttons hwiidButtonLeft) then (-1) else 0) + (if (test buttons hwiidButtonRight) then 1 else 0)
+ y = (if (test buttons hwiidButtonDown) then (-1) else 0) + (if (test buttons hwiidButtonUp) then 1 else 0)
+ --ax = aimx - (fromRational . toRational . tankX $ tank)
+ --ay = aimy - (fromRational . toRational . tankY $ tank)
+ move = (x /= 0 || y /= 0)
+
+ angle = if move then Just $ fromRational $ round ((atan2 y x)*1000000*180/pi)%1000000 else Nothing
+ --aangle = if (ax /= 0 || ay /= 0) then Just $ fromRational $ round ((atan2 ay ax)*1000000*180/pi)%1000000 else Nothing
+ return (WiimotePlayer wiimote, angle, move, Nothing, shoot)
+
+
+newWiimotePlayer :: IO WiimotePlayer
+newWiimotePlayer = do
+ wiimote <- hwiidOpen bdAddrAny (hwiidFlagMesgInterface .|. hwiidFlagNonblock)
+ when (wiimote == nullWiimote) $ fail "Wiimote error"
+ hwiidSetReportMode wiimote hwiidReportButtons
+ return $ WiimotePlayer wiimote
+
+test :: (Bits a) => a -> a -> Bool
+test field bits = (field .&. bits) == bits \ No newline at end of file