summaryrefslogtreecommitdiffstats
path: root/src/WiimotePlayer.hs
diff options
context:
space:
mode:
authorMatthias Schiffer <matthias@gamezock.de>2010-04-10 14:17:08 +0200
committerMatthias Schiffer <matthias@gamezock.de>2010-04-10 14:17:08 +0200
commit546da85814945ed2188e670ddf9c2dfd409d6241 (patch)
tree266627f8d0de15a5507ebb441e8ad9091be10510 /src/WiimotePlayer.hs
parent083619cc87aa24b6ad32b92cf40798efc11d4ff9 (diff)
downloadhtanks-546da85814945ed2188e670ddf9c2dfd409d6241.tar
htanks-546da85814945ed2188e670ddf9c2dfd409d6241.zip
Added player cursor
Diffstat (limited to 'src/WiimotePlayer.hs')
-rw-r--r--src/WiimotePlayer.hs46
1 files changed, 31 insertions, 15 deletions
diff --git a/src/WiimotePlayer.hs b/src/WiimotePlayer.hs
index 035c975..75f923b 100644
--- a/src/WiimotePlayer.hs
+++ b/src/WiimotePlayer.hs
@@ -12,16 +12,21 @@ import Data.Maybe
import Data.Ratio ((%))
import Data.Typeable
import HWiid
+import Graphics.Rendering.OpenGL.GL (GLfloat, Vector3(..))
+import Graphics.Rendering.OpenGL.GL.BeginEnd (unsafeRenderPrimitive, PrimitiveMode(..))
+import Graphics.Rendering.OpenGL.GL.CoordTrans (unsafePreservingMatrix, translate)
+import Graphics.Rendering.OpenGL.GL.VertexSpec
+
import Player
import Tank
-data WiimotePlayer = WiimotePlayer Wiimote
+data WiimotePlayer = WiimotePlayer Wiimote (Float, Float)
deriving (Typeable, Show)
instance Player WiimotePlayer where
- playerUpdate (WiimotePlayer wiimote) tank = do
+ playerUpdate (WiimotePlayer wiimote oldaim) tank = do
state <- hwiidGetState wiimote
messages <- hwiidGetMesg wiimote
@@ -39,23 +44,34 @@ instance Player WiimotePlayer where
ny = ((fromIntegral . extNunchukStickY $ ext) - 0x80)/0x80
in if (nx*nx + ny*ny) < 0.4 then (x, y) else (x+nx, y+ny)
- aim = handleIR state
- aangle = if isJust aim
- then
- let aimx = fst $ fromJust aim
- aimy = snd $ fromJust aim
- ax = aimx - (fromRational . toRational . tankX $ tank)
- ay = aimy - (fromRational . toRational . tankY $ tank)
- in if (ax /= 0 || ay /= 0) then Just $ fromRational $ round ((atan2 ay ax)*1000000*180/pi)%1000000 else Nothing
- else
- Nothing
+ iraim = handleIR state
+ (aimx, aimy) = if isJust iraim then fromJust iraim else oldaim
+ ax = aimx - (fromRational . toRational . tankX $ tank)
+ ay = aimy - (fromRational . toRational . tankY $ tank)
+ aangle = if (ax /= 0 || ay /= 0) then Just $ fromRational $ round ((atan2 ay ax)*1000000*180/pi)%1000000 else Nothing
move = (mx /= 0 || my /= 0)
angle = atan2 my mx
moveangle = if move then Just $ fromRational $ round ((angle - (sin $ 8*x)/8)*1000000*180/pi)%1000000 else Nothing
when foo $ print $ state
- return (WiimotePlayer wiimote, moveangle, move, aangle, shoot)
-
+ return (WiimotePlayer wiimote (aimx, aimy), moveangle, move, aangle, shoot)
+
+ renderPlayer (WiimotePlayer _ (x, y)) = unsafePreservingMatrix $ do
+ translate $ Vector3 x y (0 :: GLfloat)
+
+ unsafeRenderPrimitive Quads $ do
+ texCoord $ TexCoord2 (0 :: GLfloat) (0 :: GLfloat)
+ vertex $ Vertex2 (-0.2 :: GLfloat) (-0.2 :: GLfloat)
+
+ texCoord $ TexCoord2 (0 :: GLfloat) (1 :: GLfloat)
+ vertex $ Vertex2 (-0.2 :: GLfloat) (0.2 :: GLfloat)
+
+ texCoord $ TexCoord2 (1 :: GLfloat) (1 :: GLfloat)
+ vertex $ Vertex2 (0.2 :: GLfloat) (0.2 :: GLfloat)
+
+ texCoord $ TexCoord2 (1 :: GLfloat) (0 :: GLfloat)
+ vertex $ Vertex2 (0.2 :: GLfloat) (-0.2 :: GLfloat)
+
irXScale :: Float
irXScale = 20
@@ -110,7 +126,7 @@ newWiimotePlayer = do
wiimote <- hwiidOpen bdAddrAny (hwiidFlagMesgInterface .|. hwiidFlagNonblock)
when (wiimote == nullWiimote) $ fail "Wiimote error"
hwiidSetReportMode wiimote (hwiidReportButtons .|. hwiidReportAcc .|. hwiidReportIR .|. hwiidReportNunchuk)
- return $ WiimotePlayer wiimote
+ return $ WiimotePlayer wiimote (0, 0)
test :: (Bits a) => a -> a -> Bool
test field bits = (field .&. bits) == bits