PageRenderTime 30ms CodeModel.GetById 1ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/Snake/win.d

http://github.com/wilkie/djehuty
D | 173 lines | 136 code | 37 blank | 0 comment | 16 complexity | 37c4a15e604ff03de3da9597142f7b0c MD5 | raw file
  1import djehuty;
  2
  3import io.console;
  4
  5import synch.timer;
  6
  7import cui.window;
  8
  9import app;
 10import constants;
 11import game;
 12import posn;
 13import snake;
 14
 15class SnakeWindow : CuiWindow {
 16	this() {
 17		_frame_wait = FrameWait.Init;
 18
 19		_cod = DeathBy.NotDead;
 20
 21		_timer = new Timer();
 22		setSpeed();
 23		push(_timer);
 24
 25		_game = new SnakeGame(this);
 26	}
 27
 28	void setSpeed() {
 29		uint dirCoef = 1;
 30
 31		if (_game !is null) {
 32			Snake s = _game.snake;
 33			if (s !is null)
 34				dirCoef = s.isVertical ? SpeedCoef.V : SpeedCoef.H;
 35		}
 36
 37		_timer.setInterval(_frame_wait * dirCoef / FrameWait.Multiplier);
 38	}
 39
 40	void gameOver(DeathBy d) {
 41		_cod = d;
 42
 43		_end_time = Time.Now();
 44
 45		_timer.stop();
 46
 47		Console.setColor(fgColor.BrightRed);
 48		Console.putln("DEAD!");
 49	}
 50
 51
 52	override void onInitialize() {
 53		super.onInitialize();
 54
 55		Console.setColor(fgColor.White, bgColor.Black);
 56
 57		string[] instructions = ["move: arrows/wasd", "stop: q or die", "quit: any key"];
 58		foreach (uint y, string s; instructions) {
 59			Console.position(0, y);
 60			Console.put(s);
 61		}
 62		_timer.start();
 63
 64		_start_time = Time.Now();
 65	}
 66
 67	override void onKeyDown(Key k) {
 68		if (_cod != DeathBy.NotDead)
 69			finishUp();
 70		switch(k.code) {
 71			case Key.Up:
 72			case Key.W:
 73				_game.turn(Dir.Up);
 74				speedUp();
 75				break;
 76			case Key.Down:
 77			case Key.S:
 78				_game.turn(Dir.Down);
 79				speedUp();
 80				break;
 81			case Key.Left:
 82			case Key.A:
 83				_game.turn(Dir.Left);
 84				speedUp();
 85				break;
 86			case Key.Right:
 87			case Key.D:
 88				_game.turn(Dir.Right);
 89				speedUp();
 90				break;
 91			case Key.Escape:
 92			case Key.Q:
 93				if (_cod == DeathBy.NotDead) {
 94					gameOver(DeathBy.Quit);
 95					finishUp();
 96				}
 97				break;
 98			default:
 99				break;
100		}
101		super.onKeyDown(k);
102	}
103
104	bool onSignal(Dispatcher dsp, uint signal) {
105		if (dsp is _timer) {
106			if (_game !is null)
107				_game.frame();
108		}
109		return true;
110	}
111
112private:
113	void speedUp() {
114		_frame_wait -= FrameWait.Step;
115		if (_frame_wait < FrameWait.Min)
116			_frame_wait = FrameWait.Min;
117	}
118
119	void finishUp() {
120		(cast(SnakeApp)(this.application)).shutdown();
121		Console.clear();
122
123		string msg;
124
125		uint time = cast(uint)(_end_time - _start_time).seconds;
126		uint fps = cast(uint)(1000.0 / _frame_wait * FrameWait.Multiplier * 2 / (SpeedCoef.H + SpeedCoef.V));
127
128		if (time > 0 && this.width > 0 && this.height > 0 && _game !is null) {
129			string cod_msg;
130			switch (_cod) {
131				case DeathBy.CollisionSelf:
132					cod_msg = "hit yourself";
133					break;
134				case DeathBy.CollisionPortal:
135					cod_msg = "crashed into a portal";
136					break;
137				case DeathBy.CollisionReverse:
138					cod_msg = "reversed into yourself";
139					break;
140				case DeathBy.Quit:
141					cod_msg = "quit";
142					break;
143				default:
144					cod_msg = "died mysteriously";
145					break;
146			}
147
148			Snake s = _game.snake;
149
150			uint len = 0;
151			if (s !is null)
152				len = s.length;
153
154			msg =  (cod_msg ~ " with {d} segments in {d} seconds on {d} lines and {d} columns at {d} frames per second").format(len, time, this.height, this.width, fps);
155		} else {
156			msg = "You didn't even play!";
157		}
158
159		Console.putln(msg);
160
161		(cast(SnakeApp)(this.application)).end(0);
162	}
163
164	uint _frame_wait;
165
166	DeathBy _cod;
167
168	Time _start_time, _end_time;
169
170	Timer _timer;
171	SnakeGame _game;
172}
173