Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions example/pong-network/client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ export const main = async (options: IRunOptions) => {

graphics.stage.add(layer);

const terrain = registry.spawnEntity();
registry.addComponent(terrain, new Position(0, 0));
registry.addComponent(
terrain,
new RectangleComponent(new Rect({ fill: "rgb(58,99,39)", width: 1920, height: 1080 })),
);
const terrainLine = registry.spawnEntity();
registry.addComponent(terrainLine, new Position(955, 0));
registry.addComponent(
terrainLine,
new RectangleComponent(new Rect({ fill: "rgb(148,204,117)", width: 10, height: 1080 })),
);

const ball = registry.spawnEntity();
registry.addComponent(ball, new Velocity(0, 0));
registry.addComponent(ball, new Position(0, 0));
Expand Down
6 changes: 3 additions & 3 deletions example/pong-network/client/systems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ export function packetHandler(registry: Registry, ctx: Context) {
it.Velocity.y = packet.velocity.y;
} else if (type === "assignId") {
if (packet.assigned === "ball") {
registry.addComponent(registry.entityFromIndex(0), new NetworkId(packet.id));
} else if (packet.assigned === "paddle1") {
registry.addComponent(registry.entityFromIndex(2), new NetworkId(packet.id));
} else if (packet.assigned === "paddle1") {
registry.addComponent(registry.entityFromIndex(4), new NetworkId(packet.id));
} else if (packet.assigned === "paddle2") {
registry.addComponent(registry.entityFromIndex(3), new NetworkId(packet.id));
registry.addComponent(registry.entityFromIndex(5), new NetworkId(packet.id));
}
}
});
Expand Down
25 changes: 24 additions & 1 deletion example/pong-network/server/systems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,17 @@ function handleClientInput(clientId: number, key: string, network: NetworkServer

if (key === "up") {
paddle.Velocity.y = -paddleSpeed;
if (paddle.Position.y < 0) {
paddle.Position.y = 0;
paddle.Velocity.y = 0;
}
}
if (key === "down") {
paddle.Velocity.y = paddleSpeed;
if (paddle.Position.y > 780) {
paddle.Position.y = 780;
paddle.Velocity.y = 0;
}
}
if (key === "stop") {
paddle.Velocity.y = 0;
Expand Down Expand Up @@ -102,13 +110,28 @@ export function packetHandler(registry: Registry, ctx: Context) {
});
}

function checkOutOfTerrain(id: number, paddle: any, network: NetworkServerLibrary) {
if (paddle.Position.y < 0) {
paddle.Position.y = 0;
paddle.Velocity.y = 0;
sendMoveAll(id, paddle.Velocity, paddle.Position, network);
}
if (paddle.Position.y > 780) {
paddle.Position.y = 780;
paddle.Velocity.y = 0;
sendMoveAll(id, paddle.Velocity, paddle.Position, network);
}
}

export const bounce = (registry: Registry, ctx: Context) => {
const network = ctx.libs.getNetwork<NetworkServerLibrary>();
const zip = registry.getZipper([Position, Velocity]);
checkOutOfTerrain(1, zip[1], network);
checkOutOfTerrain(2, zip[2], network);
if (roundStart < 3000 && roundStart != -1) {
roundStart += ctx.app.delta;
return;
}
const zip = registry.getZipper([Position, Velocity]);
if (roundStart >= 3000) {
roundStart = -1;
zip[0].Velocity.x = 1;
Expand Down