From 1f57aab3b2bbf864c185a413110923eebb26dfb7 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Sun, 25 Apr 2021 00:30:34 -0600 Subject: [PATCH] [asciilib] White cell in Death Trap --- .../death-trap/source/DeathTrapLogic.hx | 2 ++ .../death-trap/source/DeathTrapLogic.kiss | 9 ++++++-- .../examples/death-trap/source/PlayState.hx | 2 +- projects/asciilib2/src/asciilib/Colors.kiss | 20 ++++++++++++++++ projects/asciilib2/src/asciilib/Graphics.hx | 2 +- projects/asciilib2/src/asciilib/Graphics.kiss | 4 ++-- projects/asciilib2/src/asciilib/Letters.kiss | 5 +++- projects/asciilib2/src/asciilib/Surface.hx | 5 ++++ projects/asciilib2/src/asciilib/Surface.kiss | 11 ++++++++- .../backends/flixel/FlxGraphicsBackend.hx | 4 ++++ .../backends/flixel/FlxGraphicsBackend.kiss | 23 ++++++++++++++++--- 11 files changed, 76 insertions(+), 11 deletions(-) diff --git a/projects/asciilib2/examples/death-trap/source/DeathTrapLogic.hx b/projects/asciilib2/examples/death-trap/source/DeathTrapLogic.hx index e441e29f..cb139ebd 100644 --- a/projects/asciilib2/examples/death-trap/source/DeathTrapLogic.hx +++ b/projects/asciilib2/examples/death-trap/source/DeathTrapLogic.hx @@ -2,6 +2,8 @@ package; import asciilib.GameLogic; import asciilib.Graphics; +import asciilib.Colors; +import kiss.Prelude; @:build(kiss.Kiss.build()) class DeathTrapLogic implements GameLogic {} diff --git a/projects/asciilib2/examples/death-trap/source/DeathTrapLogic.kiss b/projects/asciilib2/examples/death-trap/source/DeathTrapLogic.kiss index 4d498d9d..55350682 100644 --- a/projects/asciilib2/examples/death-trap/source/DeathTrapLogic.kiss +++ b/projects/asciilib2/examples/death-trap/source/DeathTrapLogic.kiss @@ -1,4 +1,9 @@ -(defnew [] [:Int a 5]) +(defprop &mut firstDraw true) + +(defmethod new [] 0) (defmethod :Void update [:Float deltaSeconds] 0) -(defmethod :Void draw [:Void->Graphics graphics] 0) \ No newline at end of file +(defmethod :Void draw [:Void->Graphics graphics] + (when firstDraw + (set firstDraw false) + (.setBackgroundColor (graphics) 5 5 Colors.White))) \ No newline at end of file diff --git a/projects/asciilib2/examples/death-trap/source/PlayState.hx b/projects/asciilib2/examples/death-trap/source/PlayState.hx index 32d15852..da77ec57 100644 --- a/projects/asciilib2/examples/death-trap/source/PlayState.hx +++ b/projects/asciilib2/examples/death-trap/source/PlayState.hx @@ -11,7 +11,7 @@ class PlayState extends FlxState override public function create() { super.create(); - game = new Game("Beware Yon Death Trap", 100, 40, 8, 12, new DeathTrapLogic(), new FlxGraphicsBackend()); + game = new Game("Beware Yon Death Trap", 40, 24, 8, 12, new DeathTrapLogic(), new FlxGraphicsBackend(this)); } override public function update(elapsed:Float) diff --git a/projects/asciilib2/src/asciilib/Colors.kiss b/projects/asciilib2/src/asciilib/Colors.kiss index 50751074..760da771 100644 --- a/projects/asciilib2/src/asciilib/Colors.kiss +++ b/projects/asciilib2/src/asciilib/Colors.kiss @@ -13,5 +13,25 @@ (green.fill 0 area color.g) (blue.fill 0 area color.b)) +(defmethod _index [x y] + (+ x (* y width))) + +(defmacro withIndex [idxName xName yName &rest body] + `(let [,idxName (_index ,xName ,yName)] + ,@body)) + +(defmethod getPixel [x y] + (withIndex idx x y + (object r (red.get idx) g (green.get idx) b (blue.get idx)))) + +(defmethod setPixel [x y color] + (withIndex idx x y + (red.set idx color.r) + (green.set idx color.g) + (blue.set idx color.b))) + +(defun equal [c1 c2] + (and (= c1.r c2.r) (= c1.g c2.g) (= c1.b c2.b))) + (defvar Black (object r 0 g 0 b 0)) (defvar White (object r 255 g 255 b 255)) \ No newline at end of file diff --git a/projects/asciilib2/src/asciilib/Graphics.hx b/projects/asciilib2/src/asciilib/Graphics.hx index 8d7c4108..b4709172 100644 --- a/projects/asciilib2/src/asciilib/Graphics.hx +++ b/projects/asciilib2/src/asciilib/Graphics.hx @@ -1,4 +1,4 @@ package asciilib; @:build(kiss.Kiss.build()) -class Graphics {} +class Graphics extends Surface {} diff --git a/projects/asciilib2/src/asciilib/Graphics.kiss b/projects/asciilib2/src/asciilib/Graphics.kiss index dc8d26f3..4b19129b 100644 --- a/projects/asciilib2/src/asciilib/Graphics.kiss +++ b/projects/asciilib2/src/asciilib/Graphics.kiss @@ -1,2 +1,2 @@ -(defnew [width height] - [:Surface surface (new Surface width height)]) \ No newline at end of file +(defmethod new [width height] + (super width height)) \ No newline at end of file diff --git a/projects/asciilib2/src/asciilib/Letters.kiss b/projects/asciilib2/src/asciilib/Letters.kiss index c87f4e2f..b2c78626 100644 --- a/projects/asciilib2/src/asciilib/Letters.kiss +++ b/projects/asciilib2/src/asciilib/Letters.kiss @@ -1,4 +1,7 @@ (defnew [_width _height &opt :String letter] [:Int width _width :Int height _height - :Array rows (for _ (range height) (* (or letter " ") width))]) \ No newline at end of file + :Array rows (for _ (range height) (* (or letter " ") width))]) + +(defmethod getChar [x y] + (.charAt (nth rows y) x)) \ No newline at end of file diff --git a/projects/asciilib2/src/asciilib/Surface.hx b/projects/asciilib2/src/asciilib/Surface.hx index e6c5486b..7a0f1866 100644 --- a/projects/asciilib2/src/asciilib/Surface.hx +++ b/projects/asciilib2/src/asciilib/Surface.hx @@ -2,5 +2,10 @@ package asciilib; import asciilib.Colors; +typedef Letter = { + char:String, + color:Color +}; + @:build(kiss.Kiss.build()) class Surface {} diff --git a/projects/asciilib2/src/asciilib/Surface.kiss b/projects/asciilib2/src/asciilib/Surface.kiss index 739f1d58..9885343d 100644 --- a/projects/asciilib2/src/asciilib/Surface.kiss +++ b/projects/asciilib2/src/asciilib/Surface.kiss @@ -5,4 +5,13 @@ :Letters letters (new Letters width height (or letter " ")) :Colors letterColors (new Colors width height (or letterColor Colors.White)) :Grid opacity (new Grid width height true) - :Grid specialInfo (new Grid width height "")]) \ No newline at end of file + :Grid specialInfo (new Grid width height "")]) + +(defmethod getBackgroundColor [x y] + (backgroundColors.getPixel x y)) + +(defmethod setBackgroundColor [x y color] + (backgroundColors.setPixel x y color)) + +(defmethod getLetter [x y] + (object char (letters.getChar x y) color (letterColors.getPixel x y))) \ No newline at end of file diff --git a/projects/asciilib2/src/asciilib/backends/flixel/FlxGraphicsBackend.hx b/projects/asciilib2/src/asciilib/backends/flixel/FlxGraphicsBackend.hx index 93749d83..1f0ea66a 100644 --- a/projects/asciilib2/src/asciilib/backends/flixel/FlxGraphicsBackend.hx +++ b/projects/asciilib2/src/asciilib/backends/flixel/FlxGraphicsBackend.hx @@ -2,6 +2,10 @@ package asciilib.backends.flixel; import asciilib.GraphicsBackend; import asciilib.Graphics; +import flixel.FlxState; +import flixel.group.FlxGroup; +import flixel.FlxSprite; +import flixel.util.FlxColor; @:build(kiss.Kiss.build()) class FlxGraphicsBackend implements GraphicsBackend {} diff --git a/projects/asciilib2/src/asciilib/backends/flixel/FlxGraphicsBackend.kiss b/projects/asciilib2/src/asciilib/backends/flixel/FlxGraphicsBackend.kiss index 741625e8..fdf69d93 100644 --- a/projects/asciilib2/src/asciilib/backends/flixel/FlxGraphicsBackend.kiss +++ b/projects/asciilib2/src/asciilib/backends/flixel/FlxGraphicsBackend.kiss @@ -1,3 +1,20 @@ -(defnew [] [:Int a 5]) -(defmethod :Void initialize [:String title :Int width :Int height :Int letterWidth :Int letterHeight] 0) -(defmethod :Void draw [:Graphics graphics] 0) \ No newline at end of file +(defprop &mut :FlxGroup backgroundColors null) +(defprop &mut :Int letterWidth 0) +(defprop &mut :Int letterHeight 0) +(defnew [_state] + [:FlxState state _state]) + +(defmethod :Void initialize [:String title :Int width :Int height :Int _letterWidth :Int _letterHeight] + (set letterWidth _letterWidth) + (set letterHeight _letterHeight)) + +(defmethod :Void draw [:Graphics graphics] + (when backgroundColors (backgroundColors.kill)) + (set backgroundColors (new FlxGroup)) + (for x (range graphics.width) + (for y (range graphics.height) + (let [bgc (graphics.getBackgroundColor x y)] + (unless (Colors.equal bgc Colors.Black) + (let [sprite (new FlxSprite (* letterWidth x) (* letterHeight y))] + (backgroundColors.add (sprite.makeGraphic letterWidth letterHeight (FlxColor.fromRGB bgc.r bgc.g bgc.b)))))))) + (state.add backgroundColors)) \ No newline at end of file