This repository has been archived by the owner on May 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.d
91 lines (84 loc) · 1.82 KB
/
player.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
module player;
import std.meta : Alias;
import cs = deimos.curses;
alias Window = Alias!(cs.WINDOW *);
class Player
{
char ch;
private {
int _y;
int _x;
Window _win;
}
this(Window win, short y, short x, char ch)
{
this._win = win;
this.ch = ch;
this._y = y;
this._x = x;
cs.mvwaddch(win, y, x, ch);
cs.wrefresh(_win);
}
int getY() const nothrow pure @nogc @safe
{
return _y;
}
void moveUp() nothrow @nogc
{
/* if the space is occupied, return */
if (cs.mvwinch(_win, _y - 1, _x) != ' ') {
return;
}
cs.mvwaddch(_win, _y, _x, ' '); /* remove character */
_y -= 1;
cs.mvwaddch(_win, _y, _x, ch); /* add character */
cs.wrefresh(_win);
}
void moveDown() nothrow @nogc
{
/* if the space is occupied, return */
if (cs.mvwinch(_win, _y + 1, _x) != ' ') {
return;
}
cs.mvwaddch(_win, _y, _x, ' '); /* remove character */
_y += 1;
cs.mvwaddch(_win, _y, _x, ch); /* add character */
cs.wrefresh(_win);
}
int getX() const nothrow pure @nogc @safe
{
return _x;
}
void moveLeft() nothrow @nogc
{
int newX;
newX = _x - 2;
/* if the space is occupied, return */
if (cs.mvwinch(_win, _y, newX) != ' ' ||
cs.mvwinch(_win, _y, (newX + _x) / 2) != ' ' ||
newX + 2 == cs.getmaxx(_win) ||
newX - 1 == 0) {
return;
}
cs.mvwaddch(_win, _y, _x, ' '); /* remove character */
_x = newX;
cs.mvwaddch(_win, _y, _x, ch); /* add character */
cs.wrefresh(_win);
}
void moveRight() nothrow @nogc
{
int newX;
newX = _x + 2;
/* if the space is occupied, return */
if (cs.mvwinch(_win, _y, newX) != ' ' ||
cs.mvwinch(_win, _y, (newX + _x) / 2) != ' ' ||
newX + 2 == cs.getmaxx(_win) ||
newX - 1 == 0) {
return;
}
cs.mvwaddch(_win, _y, _x, ' '); /* remove character */
_x = newX;
cs.mvwaddch(_win, _y, _x, ch); /* add character */
cs.wrefresh(_win);
}
}