Skip to content

Commit 537631b

Browse files
Tchips46Exeloo
authored andcommitted
fix(examples): server network wrong bounce
1 parent 684541b commit 537631b

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

example/pong-network/server/systems.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ export const bounce = (registry: Registry, ctx: Context) => {
115115
roundStart = -1;
116116
zip[0].Velocity.x = 1;
117117
sendMoveAll(0, zip[0].Velocity, zip[0].Position, network);
118-
sendMoveAll(0, zip[0].Velocity, zip[0].Position, network);
119118
return;
120119
}
121120
let bounced = false;
@@ -128,27 +127,34 @@ export const bounce = (registry: Registry, ctx: Context) => {
128127
zip[1].Position.y - 15 <= zip[0].Position.y &&
129128
zip[0].Position.y - 15 <= zip[1].Position.y + 300) ||
130129
(zip[0].Position.x >= 1835 &&
131-
zip[1].Position.y - 15 <= zip[0].Position.y &&
132-
zip[0].Position.y - 15 <= zip[1].Position.y + 300)
130+
zip[2].Position.y - 15 <= zip[0].Position.y &&
131+
zip[0].Position.y - 15 <= zip[2].Position.y + 300)
133132
) {
133+
let paddleTouched = 1;
134+
if (zip[0].Position.x >= 1835) {
135+
paddleTouched = 2;
136+
}
134137
const prevSpeed = Math.sqrt(zip[0].Velocity.x ** 2 + zip[0].Velocity.y ** 2);
135138
const newSpeed = prevSpeed * 1.1;
136139
const posOnPaddle =
137-
(zip[0].Position.y - (zip[0].Position.x < 960 ? zip[1].Position.y : zip[1].Position.y) + 15) /
140+
(zip[0].Position.y -
141+
(zip[0].Position.x < 960 ? zip[paddleTouched].Position.y : zip[paddleTouched].Position.y) +
142+
15) /
138143
330;
139144
const baseAngle = posOnPaddle * ((6 * Math.PI) / 8) + Math.PI / 8 - Math.PI / 2;
140145
const outAngle = zip[0].Position.x < 960 ? baseAngle : -baseAngle + Math.PI;
141146
zip[0].Velocity.x = Math.cos(outAngle) * newSpeed;
142147
zip[0].Velocity.y = Math.sin(outAngle) * newSpeed;
143148
bounced = true;
144149
} else if (zip[0].Position.x <= 15 || zip[0].Position.x >= 1905) {
145-
zip[0].Position = { x: 960, y: 540 };
146-
zip[0].Velocity = { x: 0, y: 0 };
150+
zip[0].Position.x = 960;
151+
zip[0].Position.y = 540;
152+
zip[0].Velocity.x = 0;
153+
zip[0].Velocity.y = 0;
147154
roundStart = 0;
148155
bounced = true;
149156
}
150157
if (bounced) {
151158
sendMoveAll(0, zip[0].Velocity, zip[0].Position, network);
152-
sendMoveAll(0, zip[0].Velocity, zip[0].Position, network);
153159
}
154160
};

0 commit comments

Comments
 (0)