[asciilib] White cell in Death Trap

This commit is contained in:
2021-04-25 00:30:34 -06:00
parent bd2b36bf1f
commit 1f57aab3b2
11 changed files with 76 additions and 11 deletions

View File

@@ -2,6 +2,8 @@ package;
import asciilib.GameLogic; import asciilib.GameLogic;
import asciilib.Graphics; import asciilib.Graphics;
import asciilib.Colors;
import kiss.Prelude;
@:build(kiss.Kiss.build()) @:build(kiss.Kiss.build())
class DeathTrapLogic implements GameLogic {} class DeathTrapLogic implements GameLogic {}

View File

@@ -1,4 +1,9 @@
(defnew [] [:Int a 5]) (defprop &mut firstDraw true)
(defmethod new [] 0)
(defmethod :Void update [:Float deltaSeconds] 0) (defmethod :Void update [:Float deltaSeconds] 0)
(defmethod :Void draw [:Void->Graphics graphics] 0) (defmethod :Void draw [:Void->Graphics graphics]
(when firstDraw
(set firstDraw false)
(.setBackgroundColor (graphics) 5 5 Colors.White)))

View File

@@ -11,7 +11,7 @@ class PlayState extends FlxState
override public function create() override public function create()
{ {
super.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) override public function update(elapsed:Float)

View File

@@ -13,5 +13,25 @@
(green.fill 0 area color.g) (green.fill 0 area color.g)
(blue.fill 0 area color.b)) (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 Black (object r 0 g 0 b 0))
(defvar White (object r 255 g 255 b 255)) (defvar White (object r 255 g 255 b 255))

View File

@@ -1,4 +1,4 @@
package asciilib; package asciilib;
@:build(kiss.Kiss.build()) @:build(kiss.Kiss.build())
class Graphics {} class Graphics extends Surface {}

View File

@@ -1,2 +1,2 @@
(defnew [width height] (defmethod new [width height]
[:Surface surface (new Surface width height)]) (super width height))

View File

@@ -1,4 +1,7 @@
(defnew [_width _height &opt :String letter] (defnew [_width _height &opt :String letter]
[:Int width _width [:Int width _width
:Int height _height :Int height _height
:Array<String> rows (for _ (range height) (* (or letter " ") width))]) :Array<String> rows (for _ (range height) (* (or letter " ") width))])
(defmethod getChar [x y]
(.charAt (nth rows y) x))

View File

@@ -2,5 +2,10 @@ package asciilib;
import asciilib.Colors; import asciilib.Colors;
typedef Letter = {
char:String,
color:Color
};
@:build(kiss.Kiss.build()) @:build(kiss.Kiss.build())
class Surface {} class Surface {}

View File

@@ -5,4 +5,13 @@
:Letters letters (new Letters width height (or letter " ")) :Letters letters (new Letters width height (or letter " "))
:Colors letterColors (new Colors width height (or letterColor Colors.White)) :Colors letterColors (new Colors width height (or letterColor Colors.White))
:Grid<Bool> opacity (new Grid width height true) :Grid<Bool> opacity (new Grid width height true)
:Grid<String> specialInfo (new Grid width height "")]) :Grid<String> 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)))

View File

@@ -2,6 +2,10 @@ package asciilib.backends.flixel;
import asciilib.GraphicsBackend; import asciilib.GraphicsBackend;
import asciilib.Graphics; import asciilib.Graphics;
import flixel.FlxState;
import flixel.group.FlxGroup;
import flixel.FlxSprite;
import flixel.util.FlxColor;
@:build(kiss.Kiss.build()) @:build(kiss.Kiss.build())
class FlxGraphicsBackend implements GraphicsBackend {} class FlxGraphicsBackend implements GraphicsBackend {}

View File

@@ -1,3 +1,20 @@
(defnew [] [:Int a 5]) (defprop &mut :FlxGroup backgroundColors null)
(defmethod :Void initialize [:String title :Int width :Int height :Int letterWidth :Int letterHeight] 0) (defprop &mut :Int letterWidth 0)
(defmethod :Void draw [:Graphics graphics] 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))