From b8372420f006807b05d7d7f3b3b0d8658870063e Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Sat, 18 Sep 2021 16:42:28 -0600 Subject: [PATCH] Make sprites for NAT entries with images --- .../src/nat/components/Positions.hx | 6 ++++ .../source/EntrySprite.hx | 14 +++++++++ .../source/EntrySprite.kiss | 11 +++++++ .../source/EntrySpriteSystem.hx | 10 +++++++ .../source/EntrySpriteSystem.kiss | 16 ++++++++++ .../source/PlayState.kiss | 30 ++++++++++++++++--- 6 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 projects/nat-archive-tool/src/nat/components/Positions.hx create mode 100644 projects/nat-flixel-playground/source/EntrySprite.hx create mode 100644 projects/nat-flixel-playground/source/EntrySprite.kiss create mode 100644 projects/nat-flixel-playground/source/EntrySpriteSystem.hx create mode 100644 projects/nat-flixel-playground/source/EntrySpriteSystem.kiss diff --git a/projects/nat-archive-tool/src/nat/components/Positions.hx b/projects/nat-archive-tool/src/nat/components/Positions.hx new file mode 100644 index 00000000..1984a96b --- /dev/null +++ b/projects/nat-archive-tool/src/nat/components/Positions.hx @@ -0,0 +1,6 @@ +package nat.components; + +/** + * Entries can have multiple positions in different UIs/folders/whatever. + */ +typedef Positions = Map; diff --git a/projects/nat-flixel-playground/source/EntrySprite.hx b/projects/nat-flixel-playground/source/EntrySprite.hx new file mode 100644 index 00000000..3ec3795b --- /dev/null +++ b/projects/nat-flixel-playground/source/EntrySprite.hx @@ -0,0 +1,14 @@ +package; + +import kiss.Prelude; +import kiss.List; +import flash.display.BitmapData; +import flixel.FlxSprite; +import nat.Entry; +import nat.Archive; +import nat.BoolExpInterp; +import nat.components.Images; +import nat.components.Positions; + +@:build(kiss.Kiss.build()) +class EntrySprite extends FlxSprite {} diff --git a/projects/nat-flixel-playground/source/EntrySprite.kiss b/projects/nat-flixel-playground/source/EntrySprite.kiss new file mode 100644 index 00000000..ec0c428a --- /dev/null +++ b/projects/nat-flixel-playground/source/EntrySprite.kiss @@ -0,0 +1,11 @@ +(loadFrom "nat-archive-tool" "src/nat/Lib.kiss") + +(defNew [:String positionKey + &prop :Archive archive + &prop :Entry e] + (let [images (readComponent archive e Images)] + (case (dictGet (readComponent archive e Positions) positionKey) + ((object x x y y z z) + (super x y))) + (.onComplete (BitmapData.loadFromFile (joinPath archive.archiveDir "files" (nth images.imageFiles images.pinnedImageIndex))) + ->bitmapData (loadGraphic bitmapData)))) \ No newline at end of file diff --git a/projects/nat-flixel-playground/source/EntrySpriteSystem.hx b/projects/nat-flixel-playground/source/EntrySpriteSystem.hx new file mode 100644 index 00000000..0045546d --- /dev/null +++ b/projects/nat-flixel-playground/source/EntrySpriteSystem.hx @@ -0,0 +1,10 @@ +package; + +import kiss.Prelude; +import kiss.List; +import nat.System; +import nat.BoolExpInterp; +import nat.components.Positions; + +@:build(kiss.Kiss.build()) +class EntrySpriteSystem extends System {} diff --git a/projects/nat-flixel-playground/source/EntrySpriteSystem.kiss b/projects/nat-flixel-playground/source/EntrySpriteSystem.kiss new file mode 100644 index 00000000..f3b6a73d --- /dev/null +++ b/projects/nat-flixel-playground/source/EntrySpriteSystem.kiss @@ -0,0 +1,16 @@ +(loadFrom "nat-archive-tool" "src/nat/Lib.kiss") + +(defNew [:String tagFilterString :String positionKey :PlayState playState] + (super + ->[archive e] + (and (tagsMatch archive e tagFilterString) (hasComponent e Images)) + ->[archive e] + { + (when !(hasComponent e Positions) + (withWritableEntry archive e + (addComponent archive e Positions (new Map)))) + (withWritableComponents archive e [positions Positions] + (when !(positions.exists positionKey) + (dictSet positions positionKey (object x 0.0 y 0.0 z 0.0)))) + (playState.entryGroup.add (new EntrySprite positionKey archive e)) + })) \ No newline at end of file diff --git a/projects/nat-flixel-playground/source/PlayState.kiss b/projects/nat-flixel-playground/source/PlayState.kiss index 5ea65f94..1b986160 100644 --- a/projects/nat-flixel-playground/source/PlayState.kiss +++ b/projects/nat-flixel-playground/source/PlayState.kiss @@ -1,16 +1,28 @@ (loadFrom "nat-archive-tool" "src/nat/Lib.kiss") +// TODO store a map of Entry IDs -> EntrySprites. +// TODO handleChanges() will need to kill every changed Entry's sprite and make a new one +// TODO make the EntrySprite constructor assign the entry a serialized position component + // maybe by writing a Map positions component so there can be multiple? + (method &override :Void create [] (super.create) // TODO find a better way to pass the archiveDir to a HaxeFlixel game - (let [archiveDir (or (Sys.getEnv "NAT_DIR") (throw "NAT_DIR environment variable must be set"))] + (let [archiveDir + (or (Sys.getEnv "NAT_DIR") (throw "NAT_DIR environment variable must be set")) + archive + (new Archive archiveDir)] + (set this.archive archive) (new ArchiveController - (new Archive archiveDir) + archive this)) (prop &mut :FlxGroup uiGroup (new FlxGroup)) (add uiGroup) + (prop :FlxGroup entryGroup (new FlxGroup)) + (add entryGroup) + (prop uiCamera (new FlxCamera 0 0 FlxG.width FlxG.height)) (set uiCamera.bgColor FlxColor.TRANSPARENT) (FlxG.cameras.add uiCamera false) @@ -18,7 +30,15 @@ (set uiGroup.cameras [uiCamera]) // TODO make a button that can be clicked to run typeCommand() - // TODO make sprites for entries that have images + + // make interactible sprites for entries that have images + + // TODO allow configuring the tags at runtime and erasing/re-creating sprites later + // TODO allow using other position keys and erasing/re-creating sprites later + (let [entrySpriteSystem (new EntrySpriteSystem "!done" "Playground-MAIN" this)] + (archive.addSystem entrySpriteSystem) + (entrySpriteSystem.process archive)) + ) (method &override :Void update [:Float elapsed] @@ -75,8 +95,10 @@ Math.POSITIVE_INFINITY)) (prop &mut :ArchiveController controller) +(prop &mut :Archive archive) -(method :Void setController [controller] (set this.controller controller)) +(method :Void setController [controller] + (set this.controller controller)) (prop &mut :FlxText textInputLabel null) (prop &mut :FlxInputText textInput null)