Artifact Content
Not logged in

Artifact e76b6b97ba56256b286133502f8bc901342ade43


import dfl.all;
import util;
import game;
import output;
import driver;
//import solver;
pragma(lib, "dfl.lib");

class GUI : Form, GameObserver
{
	bool on_game_changed(char c, const(Game) g, bool finished) {
		draw(gr, g);
		invalidate();
		return false;
	}

	private {
		int cell;
		int turn = 0;

		Font font;
		Color[char] colors;
		string[char] render;
		void delegate(char c) fn;
	}

	this(const(Game) g)
	{
		noMessageFilter();
		this.setStyle(ControlStyles.OPAQUE, true);
		this.fn = fn;

		this.paint ~= &my_paint;
		this.keyDown ~= &my_keydown;

		this.formBorderStyle = FormBorderStyle.FIXED_DIALOG;
		this.maximizeBox = false;
		this.minimizeBox = false;
		this.cell = min(1024/g.map.W, 640/g.map.H);
		this.clientSize = Size(g.map.W*cell, g.map.H*cell);

		const scrH = this.clientSize.height;
		const scrW = this.clientSize.width;
		this.gr = new MemoryGraphics(scrW, scrH);

		// Resources
		this.font = new Font("MS Gothic", cell-2, GraphicsUnit.PIXEL);
		this.backColor = Color(255,255,255);
		this.colors['#'] =
		this.colors['.'] = Color(255,191,127);
		this.colors['*'] = Color(255,127,127);
		this.colors['R'] = Color(128,128,0);
		this.colors['D'] = Color(255,0,0); // Dead
		this.colors['\\'] =
		this.colors['L'] =
		this.colors['O'] = Color(127,255,127);
		this.colors['W'] = Color(204,229,255); // water

		this.render['#'] = "■";
		this.render['*'] = "✹";
		this.render['.'] = "♒";
		this.render['\\'] = "λ";
		this.render['R'] = "☃";
		this.render['D'] = "☠";
		this.render['L'] = "☒";
		this.render['O'] = "☐";
		draw(gr, g);
	}

	void set_fn(F)(F f) { this.fn = f; }

	void run() {
		Application.run(this);
	}

private:
	Graphics gr;

	void my_paint(Control, PaintEventArgs ev)
	{
		gr.copyTo(ev.graphics, Rect(0,0,this.clientSize.width,this.clientSize.height));
	}

	void draw(Graphics gr, const(Game) g)
	{
		int scrW = this.clientSize.width;
		int scrH = this.clientSize.height;
		// Fill bg.
		gr.fillRectangle(this.backColor, Rect(0,0,scrW,scrH));

		// Fill water.
		int w = g.water_level();
		gr.fillRectangle(this.colors['W'], Rect(0, scrH-cell*w-1, scrW, cell*w+1));

		// Paint map.
		for(int y=1; y<=g.map.H; ++y)
		for(int x=1; x<=g.map.W; ++x) {
			Rect r = Rect(cell*(x-1), scrH-cell*y, cell, cell);
			char c = g.map[y,x];
			if( c != ' ' ) {
				if( c == 'R' && g.dead )
					c = 'D';
				gr.drawText(this.render[c], font, this.colors[c], r);
			}
		}

		set_text(g);
	}

	void my_keydown(Control c, KeyEventArgs ev)
	{
		switch(ev.keyCode)
		{
		case Keys.DOWN:  fn('D'); break;
		case Keys.UP:    fn('U'); break;
		case Keys.LEFT:  fn('L'); break;
		case Keys.RIGHT: fn('R'); break;
		case Keys.W:     fn('W'); break;
		case Keys.A:     fn('A'); break;
		default:         break;
		}
	}

	void set_text(const(Game) g) {
		this.text = .text("Score: ", g.score, " Air: ", g.hp, " Tide: ", g.water_until_rise);
	}
}

void main(string[] args)
{
	auto d = new Driver(File(args[1]));
	d.addObserver!(GuardedOutput)();
	GUI g = d.addObserver!(GUI)();
	g.set_fn(&d.command);
	g.run();
}