packages feed

mischief 0.1.0.0 → 0.1.1.0

raw patch · 5 files changed

+316/−39 lines, 5 filesdep +textdep ~mischief-assetsdep ~mischief-ecsdep ~mischief-inputnew-component:exe:example-rat-pickerPVP ok

version bump matches the API change (PVP)

Dependencies added: text

Dependency ranges changed: mischief-assets, mischief-ecs, mischief-input, mischief-math, mischief-render

API changes (from Hackage documentation)

Files

CHANGELOG.md view
@@ -1,5 +1,24 @@ # Revision history for mischief +## 0.1.1.0 ++### mischief-render 0.1.0.0 -> 0.1.1.0 ++* Added `windowToWorldPos` function. Can be used, for instance, to determine the world position of the mouse.+* Added comparisons, `select` and `pick` to shaders, for conditional control flow. +* Added `CameraBackground` and `CameraZoom` components. +* Added a field in the render settings for which default sampler the material will use. Useful for pixel art.  ++### mischief-math 0.1.0.0 -> 0.1.1.0 ++* Added Field Accessors for V4. +* Fixed a minor quaternion rotation bug. ++### mischief-ecs 0.2.1.0 -> 0.2.2.0 ++* Performance improvements when chaining multiple Changed filters with Or. +* Performance improvements when querying for all archetypes and later filtering them.  + ## 0.1.0.0   * First version. Released on an unsuspecting world.
+ examples/rat-picker/Main.hs view
@@ -0,0 +1,124 @@+module Main where++import Control.Monad+import Material (RatColor, RatMaterial, colorDown, colorUp)+import Mischief.Animation+import Mischief.Assets+import Mischief.ECS+import Mischief.Input+import Mischief.Input.Keys qualified as Keys+import Mischief.Materials.Sprite (SpriteMaterial)+import Mischief.Math+import Mischief.Math.Transform qualified as Transform+import Mischief.Render+import Mischief.Render.Camera (CameraBackground (..), CameraZoom (..))++main :: IO ()+main = do+  app <- newApp+  setAssetSource "assets/" app+  addPlugin @RatPickerPlugin app+  runApp app++data RatPickerPlugin++instance Plugin RatPickerPlugin where+  init = do+    schedule @Startup $ systems setup++    schedule @RenderUpdate $+      systems+        ( renderMaterial @SpriteMaterial def {defaultSampler = sampler @PixelSampler},+          renderMaterial @RatMaterial def {defaultSampler = sampler @PixelSampler}+        )+    schedule @RenderPrepare $ systems (maintainBuffer @RatColor)++    schedule @Update $ systems (handleInput, animateArrows)++  deps = [dep @RenderPlugin, dep @InputPlugin, dep @AnimationPlugin]++data Rat = Rat++instance Component Rat where+  required = require @(RatColor CPU)++setup :: System ()+setup = do+  window <- spawn Window+  spawn_ (Camera, CameraZoom 15, CameraBackground $ 5 / 256, Rel OutputTo window)++  fur <- load @Image "ui/fur.png"+  size <- load @Image "ui/size.png"++  spawn_ (Rat, Animator RatAnimator, WithMaterial @RatMaterial)++  spawn_ (Sprite size, WithMaterial @SpriteMaterial, def {translation = V3 48 0 0})+  spawn_ (UpArrow, Animator ArrowAnimator, WithMaterial @SpriteMaterial, def {translation = V3 48 25 0})+  spawn_ (DownArrow, Animator ArrowAnimator, WithMaterial @SpriteMaterial, Transform.rotateZ pi def {translation = V3 48 (-25) 0})++  spawn_ (Sprite fur, WithMaterial @SpriteMaterial, def {translation = V3 0 (-26) 0})+  spawn_ (RightArrow, Animator ArrowAnimator, WithMaterial @SpriteMaterial, Transform.rotateZ (-(pi / 2)) def {translation = V3 16 (-26) 0})+  spawn_ (LeftArrow, Animator ArrowAnimator, WithMaterial @SpriteMaterial, Transform.rotateZ (pi / 2) def {translation = V3 (-16) (-26) 0})++data UpArrow = UpArrow deriving (Component)++data DownArrow = DownArrow deriving (Component)++data LeftArrow = LeftArrow deriving (Component)++data RightArrow = RightArrow deriving (Component)++data ArrowAnimator = ArrowAnimator++animateArrows :: System ()+animateArrows = do+  Just keys <- res @Keys++  single [q|E / With LeftArrow|] >>= input (const $ IsPressed $ Keys.pressed keys Keys.Left) . unwrap+  single [q|E / With RightArrow|] >>= input (const $ IsPressed $ Keys.pressed keys Keys.Right) . unwrap+  single [q|E / With UpArrow|] >>= input (const $ IsPressed $ Keys.pressed keys Keys.Up) . unwrap+  single [q|E / With DownArrow|] >>= input (const $ IsPressed $ Keys.pressed keys Keys.Down) . unwrap++instance AnimatorTag ArrowAnimator where+  data State ArrowAnimator = Pressed | NotPressed+  data Input ArrowAnimator = IsPressed Bool deriving (Generic, Default)+  data Shift ArrowAnimator = Shift++  animation Pressed = selAnim "ui/arrow-animations.kdl" "pressed"+  animation NotPressed = selAnim "ui/arrow-animations.kdl" "not_pressed"++  transition NotPressed (IsPressed True) _ _ = Just Pressed+  transition Pressed (IsPressed False) _ _ = Just NotPressed+  transition _ _ _ _ = Nothing++  start = NotPressed++handleInput :: System ()+handleInput = do+  Just (rat, color) <- single [q|E, (RatColor CPU) / With Rat|]+  Just keys <- res @Keys++  when (Keys.justPressed keys Keys.Up) $ shift ShiftUp rat+  when (Keys.justPressed keys Keys.Down) $ shift ShiftDown rat++  when (Keys.justPressed keys Keys.Left) $ insert (colorDown color) rat+  when (Keys.justPressed keys Keys.Right) $ insert (colorUp color) rat++data RatAnimator = RatAnimator++instance AnimatorTag RatAnimator where+  data State RatAnimator = Rat1 | Rat2 | Rat3+  data Input RatAnimator = Input deriving (Generic, Default)+  data Shift RatAnimator = ShiftUp | ShiftDown++  animation Rat1 = selAnim "rats/rat-animations.kdl" "rat1"+  animation Rat2 = selAnim "rats/rat-animations.kdl" "rat2"+  animation Rat3 = selAnim "rats/rat-animations.kdl" "rat3"++  transition Rat1 _ (Just ShiftUp) _ = Just Rat2+  transition Rat2 _ (Just ShiftUp) _ = Just Rat3+  transition Rat3 _ (Just ShiftDown) _ = Just Rat2+  transition Rat2 _ (Just ShiftDown) _ = Just Rat1+  transition _ _ _ _ = Nothing++  start = Rat1
+ examples/rat-picker/Material.hs view
@@ -0,0 +1,115 @@+module Material where++import Mischief.Materials+import Mischief.Materials.Sprite (SpriteMaterial)+import Mischief.Materials.Sprite qualified as Sprite+import Mischief.Materials.Time (TimeMaterial, getTime)+import Prelude (otherwise)+import Prelude qualified++data RatMaterial++newtype Bindings f = Bindings (Uniform f (RatColor f))+  deriving stock (Generic)+  deriving anyclass (Bindable)++newtype RatColor f = RatColor (Field f Int)+  deriving stock (Generic)+  deriving anyclass (Bufferable)++instance Component (RatColor CPU)++instance Default (RatColor CPU) where+  def = RatColor 0++minColor :: Prelude.Int+minColor = 0++maxColor :: Prelude.Int+maxColor = 4++colorDown :: RatColor CPU -> RatColor CPU+colorDown (RatColor 0) = RatColor maxColor+colorDown (RatColor x) = RatColor (x Prelude.- 1)++colorUp :: RatColor CPU -> RatColor CPU+colorUp (RatColor x)+  | x Prelude.== maxColor = RatColor minColor+  | otherwise = RatColor (x Prelude.+ 1)++instance Material RatMaterial where+  type B RatMaterial = Bindings+  type VI RatMaterial = Sprite.VertexInput+  type VO RatMaterial = Sprite.VertexOutput++  type Extends RatMaterial = '[SpriteMaterial, TimeMaterial]++  vertex = liftExt . vertex @SpriteMaterial+  fragment vo = do+    a <- liftExt $ Sprite.sampleSprite vo.uv+    select+      (all $ a.rgb == vec3 1 1 1)+      (pickColor vo.uv)+      (pure a)++  fetch =+    FetchBindings+      (const $ pure ())+      (\sprite -> [q|sprite. (Buffer RatColor)|])+      (const Bindings)++pickColor :: Vec2 Float -> Shader RatMaterial (Vec4 Float)+pickColor uv = do+  Bindings (RatColor c) <- bindings++  Sprite.Bindings {sprite = Sprite.SpriteData {size}} <- extension @SpriteMaterial+  uv <- var $ (floor (size * uv) ^+ 0.5) / size++  pick+    [ (c == 0, color0 uv),+      (c == 1, color1 uv),+      (c == 2, color2 uv),+      (c == 3, color3 uv),+      (c == 4, color4 uv)+    ]+    (color0 uv)++color0 :: Vec2 Float -> Shader RatMaterial (Vec4 Float)+color0 _ = pure $ vec4 82 25 181 256 ^/ 256++color1 :: Vec2 Float -> Shader RatMaterial (Vec4 Float)+color1 uv = pure $ mix (vec4 1 0 0 1) (vec4 0 1 1 1) uv.x++color2 :: Vec2 Float -> Shader RatMaterial (Vec4 Float)+color2 uv = do+  time <- liftExt getTime+  pure $ mix (vec4 0 0.5 1 1) (vec4 1 0 0.1 1) (uv.x / uv.y + sin time * 0.5)++color3 :: Vec2 Float -> Shader RatMaterial (Vec4 Float)+color3 uv = do+  time <- liftExt getTime+  i :: Int <- var $ cast (floor (uv.y * 10) + time) % 6+  color :: Vec3 Float <-+    pick+      [ (i == 0, pure $ vec3 228 3 3),+        (i == 1, pure $ vec3 255 140 0),+        (i == 2, pure $ vec3 255 237 0),+        (i == 3, pure $ vec3 0 128 38),+        (i == 4, pure $ vec3 0 76 255)+      ]+      (pure $ vec3 115 41 130)+  pure $ color ^/ 256 +: 1++color4 :: Vec2 Float -> Shader RatMaterial (Vec4 Float)+color4 uv = do+  time <- liftExt getTime+  i :: Int <- var $ cast (floor (uv.x / uv.y * 10) + time * 2) % 5+  color :: Vec3 Float <-+    pick+      [ (i == 0, pure $ vec3 91 207 250),+        (i == 1, pure $ vec3 245 171 185),+        (i == 2, pure $ vec3 255 255 255),+        (i == 3, pure $ vec3 245 171 185)+      ]+      (pure $ vec3 91 207 250)+  pure $ color ^/ 256 +: 1
mischief.cabal view
@@ -18,7 +18,7 @@ -- PVP summary:     +-+------- breaking API changes --                  | | +----- non-breaking API additions --                  | | | +--- code changes with no API change-version: 0.1.0.0+version: 0.1.1.0 -- A short (one-line) description of the package. -- synopsis: -- A longer description of the package.@@ -92,10 +92,10 @@   build-depends:     base >=4.21.0.0 && <4.22,     mischief-assets ^>=0.1,-    mischief-ecs ^>=0.2,+    mischief-ecs ^>=0.2.2,     mischief-input ^>=0.1,-    mischief-math ^>=0.1,-    mischief-render ^>=0.1,+    mischief-math ^>=0.1.1,+    mischief-render ^>=0.1.1,     mischief-sdl ^>=0.1,    -- Directories containing source files.@@ -107,3 +107,35 @@     GHC2024    ghc-options: -fno-omit-yields++executable example-rat-picker+  main-is: Main.hs+  hs-source-dirs: examples/rat-picker/+  other-modules:+    Material++  build-depends:+    base >=4.21.0.0 && <4.22,+    mischief-assets,+    mischief-ecs,+    mischief-input,+    mischief-math,+    mischief-render,+    text,++  default-language: GHC2024+  default-extensions:+    DefaultSignatures+    DeriveAnyClass+    DuplicateRecordFields+    FunctionalDependencies+    ImportQualifiedPost+    MagicHash+    NoFieldSelectors+    OverloadedRecordDot+    OverloadedStrings+    QuasiQuotes+    RequiredTypeArguments+    TemplateHaskell+    TypeFamilyDependencies+    UndecidableInstances
src/Mischief.hs view
@@ -1,3 +1,5 @@+{-# OPTIONS_GHC -Wno-unused-imports #-}+ module Mischief   ( -- * What is Mischief?     -- $whatis@@ -5,7 +7,7 @@     -- * Our Values     -- $values -    -- * "AI" Disclaimer+    -- * \"AI\" Disclaimer     -- $ai      -- * What do I Need to Know?@@ -28,6 +30,7 @@   ) where +import Mischief.ECS import Prelude ()  -- $whatis@@ -41,10 +44,10 @@ -- It is not an engine meant to fit everyone, to be the "best game engine ever" or to make money. It is -- a project born of passion for data-oriented design, functional programming, and the art of crafting video games. ----- You can read more about our values [here](https://codeberg.org/mischief/mischief/src/commit/321bfb3058b91f985c5149d88cb93618ae104c7f/Values.md).+-- You can read more about our values [here](https://codeberg.org/mischief/mischief/src/branch/main/Values.md).  -- $ai--- Mischief uses no "generative AI" (in code or otherwise) and strives to be a space that fosters creativity and wholesome human interaction.+-- Mischief uses no \"generative AI\" (in code or otherwise) and strives to be a space that fosters creativity and wholesome human interaction.  -- $know -- You should generally be able to read through the official books and understand most logic without needing to have written Haskell before.@@ -55,40 +58,24 @@  -- $setup -- In order to use Mischief, you'll first need to install @GHC@ and @cabal@. We recommend doing so through the [ghcup](https://www.haskell.org/ghcup/) installer.------ After you have set up a project, add @mischief-ecs@ under @build-depends@ in you @.cabal@ file.------ We recommend using @GHC2024@ as the language standard (set in your @.cabal@ file).+-- For this version of Mischief, we recommend installing @ghc-9.12.2@. ----- == Language Extensions--- We generally recommend using the following language extensions in a Mischief project:+-- For easier setup, we recommend cloning the template found [here](https://codeberg.org/mischief/template/src/branch/0.1.0.0). You can do so using: -- -- @--- DeriveAnyClass--- DuplicateRecordFields--- NoFieldSelectors--- DerivingStrategies--- OverloadedRecordDot--- MultiWayIf--- OverloadedStrings--- QuasiQuotes--- TypeFamilyDependencies--- ImportQualifiedPost+-- git clone -b 0.1.0.0 --single-branch https://codeberg.org/mischief/template.git -- @ ----- @QuasiQuotes@ and @OverloadedStrings@ are especially important because some Mischief features are not available without them (namely quasi-queries and logging).--- The rest of the extensions are optional.------ You can paste these extensions in the @default-extensions@ field of your @.cabal@ file.------ == Text--- Mischief uses @Text@ instead of @String@ where possible, including in its logging system.+-- The @.cabal@ and @.project@ files contain the recommended configuration: extensions, language standard, library versions. Only @mischief-ecs@ is+-- imported. The other core libraries are commented out. Make sure to change all occurrences of @my-mischief-app@ to your app's name, including the @.cabal@ file's name. ----- You'll often see functions from @Data.Text@ being used, such as @T.show@, so it's recommended to add @text@ as a dependency to your project and to qualify it when you want to use it:+-- @text@ is also included as a dependency, as Mischief uses it over @String@ whenever possible. You might see stuff like @T.show@ throughout the tutorials. When you want to use such functions, you need to qualify Text: -- -- @--- import "Data.Text" qualified as T+-- import "Data.Text" qualified as Text -- @+--+-- Alternatively, you can create a new project using @cabal init@ and manually copy the relevant parts of the @.cabal@ file into your own.  -- $howtouse -- Mischief is made up of a fully decentralized ecosystem of libraries built on top of [mischief-ecs]("Mischief.ECS").@@ -97,7 +84,7 @@ -- and one of the main focuses of the engine. So feel free to experiment with various libs to find what suits your game, or even make your own -- plugins to share with others! ----- To get started, check out the [startup guide]("Mischief.ECS.Startup") from the [Learn You and ECS for Great Mischief!]("Mischief.ECS") book. You can+-- To get started, check out the [startup guide]("Mischief.ECS.Tutorial.Introduction.Startup") from the [Learn You an ECS for Great Mischief!]("Mischief.ECS") book. You can -- also click on each of the links in the next section to see their documentation / book and learn to use them! You don't need to read any one book to completion before moving on, -- feel free to switch between them depending on what you wish to learn about. --@@ -109,14 +96,14 @@ -- $libs -- This is a list of the current libraries that exist within the ecosystem: ----- * [mischief-ecs]("Mischief.ECS"). The core ECS library.--- * [mischief-assets]("Mischief.Assets"). Load and play with assets.--- * [mischief-input]("Mischief.Input"). Read input from the player.--- * [mischief-math]("Mischief.Math"). Transforms and algebra.--- * [mischief-render]("Mischief.Render"). Render stuff and write shaders.+-- * [mischief-ecs ^>=0.2]("Mischief.ECS"). The core ECS library.+-- * [mischief-assets ^>=0.1]("Mischief.Assets"). Load and play with assets.+-- * [mischief-input ^>=0.1]("Mischief.Input"). Read input from the player.+-- * [mischief-math ^>=0.1]("Mischief.Math"). Transforms and algebra.+-- * [mischief-render ^>=0.1]("Mischief.Render"). Render stuff and write shaders. -- -- Feel free to open an issue or PR in the [official repo](https://codeberg.org/mischief/mischief) if you want your library to be added to this list.--- The ["AI" policy](https://codeberg.org/mischief/mischief/src/branch/main/AI_Policy.md) applies.+-- The [\"AI\" policy](https://codeberg.org/mischief/mischief/src/branch/main/AI_Policy.md) applies.  -- $community -- You can keep in touch with the maintainers and the rest of the community by joining the official [discord server](https://discord.gg/WcpPqrwpuk).