1 import dfl.all;
2 import util;
3 import game;
4 import output;
5 //import solver;
6
7 class GUI : Form
8 {
9 private {
10 Game g;
11 int cell;
12 int turn = 0;
13
14 Font font;
15 Color[char] colors;
16 string[char] render;
17 }
18
19 this(Game g)
20 {
21 noMessageFilter();
22 this.setStyle(ControlStyles.OPAQUE, true);
23 this.g = g;
24
25 this.paint ~= &my_paint;
26 this.keyDown ~= &my_keydown;
27
28 this.formBorderStyle = FormBorderStyle.FIXED_DIALOG;
29 this.maximizeBox = false;
30 this.minimizeBox = false;
31 this.cell = min(1024/g.map.W, 640/g.map.H);
32 this.clientSize = Size(g.map.W*cell, g.map.H*cell);
33 set_text();
34
35 // Resources
36 this.font = new Font("MS Gothic", cell-2, GraphicsUnit.PIXEL);
37 this.backColor = Color(255,255,255);
38 this.colors['#'] =
39 this.colors['.'] = Color(255,191,127);
40 this.colors['*'] = Color(255,127,127);
41 this.colors['R'] = Color(128,128,0);
42 this.colors['D'] = Color(255,0,0); // Dead
43 this.colors['\\'] =
44 this.colors['L'] =
45 this.colors['O'] = Color(127,255,127);
46 this.colors['W'] = Color(204,229,255); // water
47
48 this.render['#'] = "■";
49 this.render['*'] = "✹";
50 this.render['.'] = "♒";
51 this.render['\\'] = "λ";
52 this.render['R'] = "☃";
53 this.render['D'] = "☠";
54 this.render['L'] = "☒";
55 this.render['O'] = "☐";
56 }
57
58 private:
59 void my_paint(Control, PaintEventArgs ev)
60 {
61 const scrH = this.clientSize.height;
62 const scrW = this.clientSize.width;
63 Graphics gr = new MemoryGraphics(scrW, scrH, ev.graphics);
64 scope(exit) {
65 gr.copyTo(ev.graphics, Rect(0,0,scrW,scrH));
66 gr.dispose();
67 }
68
69 // Fill bg.
70 gr.fillRectangle(this.backColor, Rect(0,0,scrW,scrH));
71
72 // Fill water.
73 int w = g.water_level();
74 gr.fillRectangle(this.colors['W'], Rect(0, scrH-cell*w-1, scrW, cell*w+1));
75
76 // Paint map.
77 for(int y=1; y<=g.map.H; ++y)
78 for(int x=1; x<=g.map.W; ++x) {
79 Rect r = Rect(cell*(x-1), scrH-cell*y, cell, cell);
80 char c = g.map[y,x];
81 if( c != ' ' ) {
82 if( c == 'R' && g.dead )
83 c = 'D';
84 gr.drawText(this.render[c], font, this.colors[c], r);
85 }
86 }
87 }
88
89 void my_keydown(Control c, KeyEventArgs ev)
90 {
91 switch(ev.keyCode)
92 {
93 case Keys.DOWN: g.command('D'); break;
94 case Keys.UP: g.command('U'); break;
95 case Keys.LEFT: g.command('L'); break;
96 case Keys.RIGHT: g.command('R'); break;
97 case Keys.W: g.command('W'); break;
98 case Keys.A: g.command('A'); break;
99 // case Keys.G: solver.act(g); break;
100 default: break;
101 }
102 if(g.cleared)
103 Application.exit();
104 invalidate();
105 set_text();
106 }
107
108 void set_text() {
109 this.text = .text("Score: ", g.score, " Air: ", g.hp, " Tide: ", g.water_until_rise);
110 }
111 }
112
113 void main(string[] args)
114 {
115 auto g = Game.load(File(args[1]));
116 g.set_output(new GuardedOutput(g));
117 auto myForm = new GUI(g);
118 Application.run(myForm);
119 }