Diff
Not logged in

Differences From Artifact [8bca70d55515c665]:

To Artifact [e76b6b97ba56256b]:


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