-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mover.jack
102 lines (95 loc) · 2.25 KB
/
Mover.jack
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
92
93
94
95
96
97
98
99
100
101
102
class Mover {
field Grid grid;
field int pos;
field bool draw, listen, done;
constructor Mover new(Grid g) {
let grid = g;
let pos = grid.center();
do grid.setPos(pos);
let draw = false;
let listen = true;
return this;
}
//in draw mode, will leave trail of active grid squares
//out of draw mode, will erase trails
method void mode() {
let draw = ~draw;
return;
}
method void moveLeft() {
if (~draw) {
do grid.unSetPos(pos);
do grid.drawPos(pos);
}
let pos = grid.left(pos);
do grid.setPos(pos);
return;
}
method void moveRight() {
if (~draw) {
do grid.unSetPos(pos);
do grid.drawPos(pos);
}
let pos = grid.right(pos);
do grid.setPos(pos);
return;
}
method void moveUp() {
if (~draw) {
do grid.unSetPos(pos);
do grid.drawPos(pos);
}
let pos = grid.up(pos);
do grid.setPos(pos);
return;
}
method void moveDown() {
if (~draw) {
do grid.unSetPos(pos);
do grid.drawPos(pos);
}
let pos = grid.down(pos);
do grid.setPos(pos);
return;
}
method void step() {
var char key;
let key = Keyboard.keyPressed();
if (key = 32) { //spacebar to toggle draw mode
let draw = true;
// do Sys.wait(500); //so we don't double count spacebar hits
}
if (key = 130) {
do moveLeft();
let draw = false;
}
if (key = 131) {
do moveUp();
let draw = false;
}
if (key = 132) {
do moveRight();
let draw = false;
}
if (key = 133) {
do moveDown();
let draw = false;
}
if (key = 140) {
let done = true;
}
do grid.drawPos(pos);
return;
}
method Grid state() {
return grid;
}
method bool done() {
return done;
}
method void dispose() {
do grid.dispose();
do Memory.deAlloc(this);
return;
}
}