PageRenderTime 140ms CodeModel.GetById 129ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/Snake/posn.d

http://github.com/wilkie/djehuty
D | 65 lines | 53 code | 12 blank | 0 comment | 11 complexity | 4291334a327bd3cbd8df417e2ee0cffb MD5 | raw file
 1enum Dir : int {
 2	Up    = -1,
 3	Down  = +1,
 4	Left  = -2,
 5	Right = +2,
 6}
 7
 8class Posn {
 9	this(int x_n, int y_n) {
10		x = x_n;
11		y = y_n;
12	}
13
14	Posn dup() {
15		return new Posn(_x, _y);
16	}
17
18	static void SetBounds(uint max_x_n, uint max_y_n) {
19		_max_x = max_x_n;
20		_max_y = max_y_n;
21	}
22
23	void x(int x_n) {
24		if (x_n < 0) {
25			_x = _max_x;
26		} else if (x_n > _max_x) {
27			_x = 0;
28		} else {
29			_x = x_n;
30		}
31	}
32
33	uint x() {
34		return _x;
35	}
36
37	void y(int y_n) {
38		if (y_n < 0) {
39			_y = _max_y;
40		} else if (y_n > _max_y) {
41			_y = 0;
42		} else {
43			_y = y_n;
44		}
45	}
46
47	uint y() {
48		return _y;
49	}
50
51	void moveTo(Posn p) {
52		x = p.x;
53		y = p.y;
54	}
55
56	int opEquals(Posn p) {
57		return cast(int)(x == p.x && y == p.y);
58	}
59
60private:
61	static uint _max_x, _max_y;
62
63	uint _x, _y;
64}
65