-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpaddlestate.js
More file actions
112 lines (104 loc) · 3.3 KB
/
paddlestate.js
File metadata and controls
112 lines (104 loc) · 3.3 KB
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
103
104
105
106
107
108
109
110
111
112
(function () {
let nsPaddle, nsBall, nsBackground;
let isPaddle, isBallInPaddleBuffer, isPaddleBuffer;
// Don't access color directly; it may be out of date.
// This originally just dealt with paddles. You can now create a PaddleState
// from a PaddleBuffer, with or without a ball in it, so beware the underlying
// ball bits and type when dealing with motion.
// Note that in setDY we assume a position range of 0-56, since that's what
// our current 3-bit-shifted-3 dest can encode.
class PaddleState {
static init(_nsPaddle_, _nsBall_, _nsBackground_,
_isPaddle_, _isBallInPaddleBuffer_, _isPaddleBuffer_) {
nsPaddle = _nsPaddle_;
nsBall = _nsBall_;
nsBackground = _nsBackground_;
isPaddle = _isPaddle_;
isBallInPaddleBuffer = _isBallInPaddleBuffer_;
isPaddleBuffer = _isPaddleBuffer_;
}
constructor(color) {
assert(nsPaddle);
assert(isPaddle(color) || isBallInPaddleBuffer(color) ||
isPaddleBuffer(color));
this.color = color;
this.delay = 0;
if (isPaddle(color)) {
this.ns = nsPaddle;
} else if (isBallInPaddleBuffer(color)) {
this.ns = nsBall;
} else {
assert(isPaddleBuffer(color));
this.ns = nsBackground;
this.delay = nsBackground.PADDLE_MOVE_DELAY_COUNTER.get(color);
}
this.position = this.ns.PADDLE_POSITION.get(color);
this.dest = this.ns.PADDLE_DEST.get(color);
this.decimator = this.ns.DECIMATOR.get(color);
// This is the single raw bit, not the decoded, useful value.
this.paddlePixelBit = this.ns.PADDLE_PIXEL.get(color);
}
isMotionCycle() {
return !this.decimator;
}
getDY(isLeft) {
assert(isLeft !== undefined);
let useUserInput = isLeft ? leftPlayerHuman : rightPlayerHuman;
if (useUserInput) {
if (isLeft) {
if (keyIsPressed('w') && this.position > 0) {
return -1;
} else if (keyIsPressed('s') && this.position < 56) {
return 1;
} else {
return 0;
}
} else {
if (keyIsPressed('arrowup') && this.position > 0) {
return -1;
} else if (keyIsPressed('arrowdown') && this.position < 56) {
return 1;
} else {
return 0;
}
}
}
if (this.delay) {
return 0;
}
let destPos = this.dest << 3;
if (this.position > destPos) {
return -1;
} else if (this.position < destPos) {
return 1;
} else {
return 0;
}
}
setDest(dest) {
assert(!(dest & ~0x07))
this.dest = dest;
}
getColor() {
assert(this.ns);
let color = this.color;
color = this.ns.PADDLE_DEST.set(color, this.dest);
return color;
}
nextColor(isLeft) {
let color = this.getColor();
if (this.isMotionCycle()) {
color = this.ns.PADDLE_POSITION.set(color,
this.position + this.getDY(isLeft));
}
color = this.ns.DECIMATOR.set(color, !this.decimator);
return color;
}
// This is the namespace of the source color, and thus of getColor and
// nextColor.
getNamespace() {
return this.ns;
}
}
window.PaddleState = PaddleState;
})();