module MainState (newMState ) where import Graphics.Rendering.OpenGL import Graphics.UI.GLUT import States import VectorMaps import Control.Applicative import qualified Data.Map as Map --import EventFlags import ResionMap -----新しいMStateの作成 newMState :: MState -> KState -> MState newMState mst kst = (eventAllAct (putStone mst kst) kst) -------イベント実行 eventAct :: MState -> EventKey -> MState eventAct mst ekey = case ekey of -- EReset -> eventResetButton mst dst _ -> mst -------すべてのイベント実行 eventAllAct :: MState -> KState -> MState eventAllAct mst kst = let ekeys = Map.keys (events mst) in foldl eventAct mst ekeys -----クリックした位置のセルに石を置く putStone :: MState -> KState -> MState putStone mst kst = case (getClickCellPos mst kst) of Nothing -> mst Just x -> mStateChangeGBoardCellsID mst x (playerStone mst) -----クリックした位置のセルの位置を得る getClickCellPos :: MState -> KState -> (Maybe (Vector2 GLint)) getClickCellPos mst kst = if dClickPosInBoard mst kst then transPosIntoCellPos mst (fmap (getCellPos mst) (clickPos kst) ) else Nothing ----ボードの相対座標からそのセルの位置を得る transPosIntoCellPos :: MState -> Maybe (Vector2 GLfloat) -> Maybe (Vector2 GLint) transPosIntoCellPos mst mv = let tb = displayGameBoard mst w = boardWidth tb / (fromIntegral (divBoardNumX mst)) h = boardHeight tb / (fromIntegral (divBoardNumY mst)) in case mv of Nothing -> Nothing Just (Vector2 x y) -> Just (fmap floor (Vector2 (x / w) (y / h))) ----クリックした位置のテーブルの相対座標 getCellPos :: MState -> (Vector2 GLfloat) -> (Vector2 GLfloat) getCellPos mst v = let tb = displayGameBoard mst posX = boardPosX tb posY = boardPosY tb in (-) <$> v <*> (Vector2 posX posY) ----クリックした位置のテーブル内であるか dClickPosInBoard :: MState -> KState -> Bool dClickPosInBoard mst kst = let tb = displayGameBoard mst posX = boardPosX tb posY = boardPosY tb w = boardWidth tb h = boardHeight tb in case (clickPos kst) of Nothing -> False Just x -> (containIntervalCC2 (Vector2 posX posY) (Vector2 (posX + w) (posY + h)) x) == (Vector2 True True)