From a27926edef702d13011a68db85b84b046682f61d Mon Sep 17 00:00:00 2001 From: Matt Hunzinger Date: Sat, 22 Feb 2025 22:07:07 -0500 Subject: [PATCH] feat: setup pong example --- Pong.hs | 93 ++++++++++++++++++++++++++++++++++++++++++ README.md | 8 ++++ assets/ball.png | Bin 0 -> 221 bytes assets/paddle.png | Bin 0 -> 116 bytes aztecs-examples.cabal | 12 ++++++ 5 files changed, 113 insertions(+) create mode 100644 Pong.hs create mode 100644 README.md create mode 100644 assets/ball.png create mode 100644 assets/paddle.png diff --git a/Pong.hs b/Pong.hs new file mode 100644 index 0000000..d2b7898 --- /dev/null +++ b/Pong.hs @@ -0,0 +1,93 @@ +{-# LANGUAGE Arrows #-} +{-# LANGUAGE DeriveAnyClass #-} +{-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE TypeApplications #-} + +module Main where + +import Aztecs +import qualified Aztecs.ECS.Access as A +import qualified Aztecs.ECS.Query as Q +import qualified Aztecs.ECS.System as S +import qualified Aztecs.SDL as SDL +import Aztecs.SDL.Image (Image (..)) +import qualified Aztecs.SDL.Image as IMG +import Control.Arrow +import Control.DeepSeq +import GHC.Generics +import SDL ( V2 (..),) + +data PlayerKind = LeftPlayer | RightPlayer + deriving (Show, Eq, Generic, NFData) + +data Player = Player + { playerKind :: PlayerKind, + playerPosition :: Float + } + deriving (Show, Eq, Generic, NFData) + +instance Component Player + +setup :: Schedule IO () () +setup = proc () -> do + ballTexture <- system . load $ asset "assets/ball.png" () -< () + paddleTexture <- system . load $ asset "assets/paddle.png" () -< () + + access + ( \(ballTexture, paddleTexture) -> do + A.spawn_ $ bundle Window {windowTitle = "Aztecs"} + A.spawn_ $ bundle Camera {cameraViewport = V2 1000 500, cameraScale = 5} <> bundle transform2d + A.spawn_ $ + bundle Player {playerKind = LeftPlayer, playerPosition = 0} + <> bundle transform2d + <> bundle Image {imageTexture = paddleTexture} + A.spawn_ $ + bundle Player {playerKind = RightPlayer, playerPosition = 0} + <> bundle transform2d {transformTranslation = V2 50 0} + <> bundle Image {imageTexture = paddleTexture} + A.spawn_ $ + bundle Image {imageTexture = ballTexture} + <> bundle transform2d {transformTranslation = V2 10 10} + ) + -< + (ballTexture, paddleTexture) + +update :: System () () +update = proc () -> do + kb <- S.single Q.fetch -< () + let delta = 0.01 + makeDelta keyUp keyDown = + if isKeyPressed keyUp kb then -delta else if isKeyPressed keyDown kb then delta else 0 + leftDelta = makeDelta KeyW KeyS + rightDelta = makeDelta KeyUp KeyDown + S.map + ( proc (leftDelta, rightDelta) -> do + player <- Q.fetch -< () + transform <- Q.fetch @_ @Transform2D -< () + let d = case playerKind player of + LeftPlayer -> leftDelta + RightPlayer -> rightDelta + V2 x _ = transformTranslation transform + y = playerPosition player + d + Q.set -< transform {transformTranslation = V2 x $ round y} + Q.set -< player {playerPosition = y} + ) + -< + (leftDelta, rightDelta) + returnA -< () + +game :: Schedule IO () () +game = + SDL.setup + >>> system IMG.setup + >>> setup + >>> forever_ + ( IMG.load + >>> SDL.update + >>> system update + >>> system IMG.draw + >>> SDL.draw + ) + +main :: IO () +main = runSchedule_ game diff --git a/README.md b/README.md new file mode 100644 index 0000000..0acae74 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# Aztecs Examples + +Example applications using [Aztecs](https://github.com/aztecs-hs/aztecs) + +## Running examples + +Examples can be run from the root folder of this repo with `cabal run {example}` (such as `cabal run ECS`). + diff --git a/assets/ball.png b/assets/ball.png new file mode 100644 index 0000000000000000000000000000000000000000..b441812cbfb82c94feed7fe543d0e76888533095 GIT binary patch literal 221 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdz#^NA%Cx&(BWL^R}(>+}rLn2z= zPCqTwpupj}`|p4I%U;L5xI^T)BTS>_DW8*gFoQ#B(LDX-}n;9vw?rek3S+o6@GWmQzoh+45YRH{VA~ UWzT6Bpc5H9UHx3vIVCg!03{VuwEzGB literal 0 HcmV?d00001 diff --git a/assets/paddle.png b/assets/paddle.png new file mode 100644 index 0000000000000000000000000000000000000000..43416922ba26e1c9fa06f9a220a3fd27d4ccea4d GIT binary patch literal 116 zcmeAS@N?(olHy`uVBq!ia0vp^0zmA*!3HFSYrjteQjEnx?oJHr&dIz4a`Zf1977^n z-=5jX%fP^K*xbP0l+XkK DMo%5( literal 0 HcmV?d00001 diff --git a/aztecs-examples.cabal b/aztecs-examples.cabal index 4fc77dc..856ba64 100644 --- a/aztecs-examples.cabal +++ b/aztecs-examples.cabal @@ -45,6 +45,18 @@ executable window aztecs-sdl >=0.4 && <0.5, sdl2 +executable pong + main-is: Pong.hs + default-language: Haskell2010 + ghc-options: -Wall + build-depends: + base, + aztecs >=0.7 && <0.8, + aztecs-sdl >=0.4 && <0.5, + aztecs-sdl-image >=0.4 && <0.5, + deepseq >=1, + sdl2 + executable image main-is: Image.hs default-language: Haskell2010