6293256fec 2012-07-14 kinaba: import dfl.all; 6293256fec 2012-07-14 kinaba: import util; 6293256fec 2012-07-14 kinaba: import game; 6293256fec 2012-07-14 kinaba: import output; 6293256fec 2012-07-14 kinaba: //import solver; 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: class GUI : Form 6293256fec 2012-07-14 kinaba: { 6293256fec 2012-07-14 kinaba: private { 6293256fec 2012-07-14 kinaba: Game g; 6293256fec 2012-07-14 kinaba: int cell; 6293256fec 2012-07-14 kinaba: int turn = 0; 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: Font font; 6293256fec 2012-07-14 kinaba: Color[char] colors; 6293256fec 2012-07-14 kinaba: string[char] render; 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: this(Game g) 6293256fec 2012-07-14 kinaba: { 6293256fec 2012-07-14 kinaba: noMessageFilter(); 6293256fec 2012-07-14 kinaba: this.setStyle(ControlStyles.OPAQUE, true); 6293256fec 2012-07-14 kinaba: this.g = g; 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: this.paint ~= &my_paint; 6293256fec 2012-07-14 kinaba: this.keyDown ~= &my_keydown; 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: this.formBorderStyle = FormBorderStyle.FIXED_DIALOG; 6293256fec 2012-07-14 kinaba: this.maximizeBox = false; 6293256fec 2012-07-14 kinaba: this.minimizeBox = false; 6293256fec 2012-07-14 kinaba: this.cell = min(1024/g.map.W, 640/g.map.H); 6293256fec 2012-07-14 kinaba: this.clientSize = Size(g.map.W*cell, g.map.H*cell); 6293256fec 2012-07-14 kinaba: set_text(); 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: // Resources 6293256fec 2012-07-14 kinaba: this.font = new Font("MS Gothic", cell-2, GraphicsUnit.PIXEL); 6293256fec 2012-07-14 kinaba: this.backColor = Color(255,255,255); 6293256fec 2012-07-14 kinaba: this.colors['#'] = 6293256fec 2012-07-14 kinaba: this.colors['.'] = Color(255,191,127); 6293256fec 2012-07-14 kinaba: this.colors['*'] = Color(255,127,127); 6293256fec 2012-07-14 kinaba: this.colors['R'] = Color(128,128,0); 6293256fec 2012-07-14 kinaba: this.colors['D'] = Color(255,0,0); // Dead 6293256fec 2012-07-14 kinaba: this.colors['\\'] = 6293256fec 2012-07-14 kinaba: this.colors['L'] = 6293256fec 2012-07-14 kinaba: this.colors['O'] = Color(127,255,127); 6293256fec 2012-07-14 kinaba: this.colors['W'] = Color(204,229,255); // water 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: this.render['#'] = "■"; 6293256fec 2012-07-14 kinaba: this.render['*'] = "✹"; 6293256fec 2012-07-14 kinaba: this.render['.'] = "♒"; 6293256fec 2012-07-14 kinaba: this.render['\\'] = "λ"; 6293256fec 2012-07-14 kinaba: this.render['R'] = "☃"; 6293256fec 2012-07-14 kinaba: this.render['D'] = "☠"; 6293256fec 2012-07-14 kinaba: this.render['L'] = "☒"; 6293256fec 2012-07-14 kinaba: this.render['O'] = "☐"; 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: private: 6293256fec 2012-07-14 kinaba: void my_paint(Control, PaintEventArgs ev) 6293256fec 2012-07-14 kinaba: { 6293256fec 2012-07-14 kinaba: const scrH = this.clientSize.height; 6293256fec 2012-07-14 kinaba: const scrW = this.clientSize.width; 6293256fec 2012-07-14 kinaba: Graphics gr = new MemoryGraphics(scrW, scrH, ev.graphics); 6293256fec 2012-07-14 kinaba: scope(exit) { 6293256fec 2012-07-14 kinaba: gr.copyTo(ev.graphics, Rect(0,0,scrW,scrH)); 6293256fec 2012-07-14 kinaba: gr.dispose(); 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: // Fill bg. 6293256fec 2012-07-14 kinaba: gr.fillRectangle(this.backColor, Rect(0,0,scrW,scrH)); 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: // Fill water. 6293256fec 2012-07-14 kinaba: int w = g.water_level(); 6293256fec 2012-07-14 kinaba: gr.fillRectangle(this.colors['W'], Rect(0, scrH-cell*w-1, scrW, cell*w+1)); 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: // Paint map. 6293256fec 2012-07-14 kinaba: for(int y=1; y<=g.map.H; ++y) 6293256fec 2012-07-14 kinaba: for(int x=1; x<=g.map.W; ++x) { 6293256fec 2012-07-14 kinaba: Rect r = Rect(cell*(x-1), scrH-cell*y, cell, cell); 6293256fec 2012-07-14 kinaba: char c = g.map[y,x]; 6293256fec 2012-07-14 kinaba: if( c != ' ' ) { 6293256fec 2012-07-14 kinaba: if( c == 'R' && g.dead ) 6293256fec 2012-07-14 kinaba: c = 'D'; 6293256fec 2012-07-14 kinaba: gr.drawText(this.render[c], font, this.colors[c], r); 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: void my_keydown(Control c, KeyEventArgs ev) 6293256fec 2012-07-14 kinaba: { 6293256fec 2012-07-14 kinaba: switch(ev.keyCode) 6293256fec 2012-07-14 kinaba: { 6293256fec 2012-07-14 kinaba: case Keys.DOWN: g.command('D'); break; 6293256fec 2012-07-14 kinaba: case Keys.UP: g.command('U'); break; 6293256fec 2012-07-14 kinaba: case Keys.LEFT: g.command('L'); break; 6293256fec 2012-07-14 kinaba: case Keys.RIGHT: g.command('R'); break; 6293256fec 2012-07-14 kinaba: case Keys.W: g.command('W'); break; 6293256fec 2012-07-14 kinaba: case Keys.A: g.command('A'); break; 6293256fec 2012-07-14 kinaba: // case Keys.G: solver.act(g); break; 6293256fec 2012-07-14 kinaba: default: break; 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: if(g.cleared) 6293256fec 2012-07-14 kinaba: Application.exit(); 6293256fec 2012-07-14 kinaba: invalidate(); 6293256fec 2012-07-14 kinaba: set_text(); 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: void set_text() { 6293256fec 2012-07-14 kinaba: this.text = .text("Score: ", g.score, " Air: ", g.hp, " Tide: ", g.water_until_rise); 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: void main(string[] args) 6293256fec 2012-07-14 kinaba: { 6293256fec 2012-07-14 kinaba: auto g = Game.load(File(args[1])); 6293256fec 2012-07-14 kinaba: g.set_output(new GuardedOutput(g)); 6293256fec 2012-07-14 kinaba: auto myForm = new GUI(g); 6293256fec 2012-07-14 kinaba: Application.run(myForm); 6293256fec 2012-07-14 kinaba: }