This repository has been archived by the owner on Aug 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test3.pjs
146 lines (126 loc) · 3.1 KB
/
test3.pjs
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
int bx, by, bz;
float dx, dy, dz;
int paddledepth = 160, paddlesize = 100, walldepth = -1000, cameradepth = 200, score, speed = 20;
void setup() {
size(window.innerWidth * .95, window.innerHeight * .95, OPENGL);
websock = new WebSocket("ws://localhost:8080");
websock.onopen = function(evt) {
console.log("opened websocket")
websock.send("Hello Web Socket!");
};
websock.onmessage = function(evt) {
console.log("received message " + evt.data);
};
websock.onclose = function(evt) {
console.log("I really should try to connect again");
};
newgame();
}
void newgame() {
bx = .4*width;
by = .4*height;
bz = walldepth;
dx = 0;
dy = 0;
dz = speed;
score = 0;
}
void draw() {
background(0);
lights();
noStroke();
// gravity
dy += 0.10;
bx += dx;
by += dy;
bz += dz;
if(bz > paddledepth) {
camera(mouseX,mouseY,500, bx, by, bz, 0, 1, 0);
} else if(mousePressed) {
//camera(width/2,height/2,500,width/2,height/2,-200, 0, 1, 0);
camera(width,height, walldepth/2, bx, by, bz, 0, 1, 0);
} else {
camera(mouseX,mouseY,500, width/2, height/2, -200, 0, 1, 0);
}
boolean paddling = bx > mouseX - paddlesize/2 && bx < mouseX + paddlesize/2 && by > mouseY - paddlesize/2 && by < mouseY + paddlesize/2;
// collisions
if(bz >= paddledepth && bz <= paddledepth + speed) {
if(mousePressed) {
// cheating: never miss, random deflection
dx = random(speed/10)-speed/20;
dy = random(speed/3);
dz = -speed;
} else if(bz == paddledepth && paddling) {
// my paddle
dz = -dz;
dx = (speed/4)*(bx - mouseX)/paddlesize;
dy = (speed)*(by - mouseY)/paddlesize;
score++;
websock.send("scored!")
}
} else if(bz < walldepth) {
// opponent
dz = -dz;
} else if(by > height) {
// floor
dy = -dy;
by -= 1;
}
if(bz >= 1000) {
websock.send("lost!");
newgame();
}
// sphere has a light source
pointLight(255,255,255,bx,by,bz);
// bouncing sphere
pushMatrix();
translate(bx,by,bz);
if(by > height*9/10 && bz < paddledepth) {
scale(1, 5.5-5*by/height, 1);
}
fill(255,88,0);
sphere(30);
popMatrix();
// distracting cube
pushMatrix();
translate(width*3/4, height/2, -650);
rotateX(PI/4);
rotateZ(PI/3);
fill(255,255,255,128);
box(90, 60, 150);
popMatrix();
// floor
pushMatrix();
translate(width/2, height, walldepth/2);
fill(99,55,22,192);
box(700,10,walldepth+15);
popMatrix();
// computer's paddle
pushMatrix();
translate(bx, by, walldepth);
fill(255,55,22,192);
box(paddlesize,paddlesize,10);
popMatrix();
// gameboard divider ("net")
pushMatrix();
translate(width/2, height/2, walldepth/2);
fill(99,55,22,32);
box(500,700,10);
popMatrix();
// distracting sphere
pushMatrix();
translate(width/4, height/3, -90);
fill(22,55,99);
sphere(90);
popMatrix();
// my paddle (last so alpha works)
pushMatrix();
if(mousePressed) {
translate(bx, by, paddledepth);
} else {
translate(mouseX, mouseY, paddledepth);
}
fill(255,255,0,32);
box(paddlesize,paddlesize,10);
popMatrix();
}