Skip to content

Commit

Permalink
Merge pull request #43 from benjaminbours/doors-activation
Browse files Browse the repository at this point in the history
Doors activation
  • Loading branch information
benjaminbours authored Oct 7, 2023
2 parents 69ba353 + f81d35f commit 016c345
Show file tree
Hide file tree
Showing 29 changed files with 888 additions and 2,596 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
- docker-compose
- Makefile

### Configuration

You need to setup a env variable `NPM_TOKEN` in order to install github packages

Add this to your `.bashrc` / `.zshrc`:

`export NPM_TOKEN=github_access_token`

### How to run the application

Start all the containers
Expand Down
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
- Orchestrate efficiently the transition between the menu, the game, the state on the server, the connection, etc
- Fix responsive menu this last changes
- Ensure all click action on menu are on buttons element to maximize browser compatibility
- Fix bug where camera of other player have the same behavior as the first one while on interactive element

## Nice to have

Expand All @@ -24,3 +23,4 @@
- Rethink the way the level are built, everything could be imported from Blender or other tool to generate level
- Check to optimize / improve the shadow player shader
- Lint front
- Game state can be optimized about door management
27 changes: 13 additions & 14 deletions back/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,35 @@
"@benjaminbours/composite-core": "^0.1.0-next.0",
"@injectit/threejs-nodejs-exporters": "^0.0.2",
"@nestjs/cache-manager": "^2.1.0",
"@nestjs/common": "^10.2.5",
"@nestjs/core": "^10.2.5",
"@nestjs/platform-express": "^10.2.5",
"@nestjs/platform-socket.io": "^10.2.5",
"@nestjs/websockets": "^10.2.5",
"@nestjs/common": "^10.2.7",
"@nestjs/core": "^10.2.7",
"@nestjs/platform-express": "^10.2.7",
"@nestjs/platform-socket.io": "^10.2.7",
"@nestjs/websockets": "^10.2.7",
"@prisma/client": "^5.3.1",
"@socket.io/redis-adapter": "^8.2.1",
"@socket.io/redis-streams-adapter": "^0.1.0",
"cache-manager": "^5.2.3",
"cache-manager": "^5.2.4",
"cache-manager-redis-store": "^3.0.1",
"env-cmd": "^10.1.0",
"redis": "^4.6.9",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.2.0",
"socket.io": "^4.7.2",
"three": "^0.156.1"
"three": "^0.157.0"
},
"devDependencies": {
"@nestjs/cli": "^10.1.17",
"@nestjs/schematics": "^10.0.2",
"@nestjs/testing": "^10.2.5",
"@nestjs/testing": "^10.2.7",
"@types/express": "^4.17.13",
"@types/jest": "^29.5.5",
"@types/node": "^20.7.0",
"@types/supertest": "^2.0.11",
"@types/node": "^20.8.3",
"@types/supertest": "^2.0.14",
"@types/three": "^0.156.0",
"@typescript-eslint/eslint-plugin": "^6.7.0",
"@typescript-eslint/parser": "^6.7.0",
"eslint": "^8.50.0",
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
"eslint": "^8.51.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"jest": "^29.7.0",
Expand All @@ -61,7 +61,6 @@
"source-map-support": "^0.5.20",
"supertest": "^6.1.3",
"ts-jest": "^29.1.1",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"tsconfig-paths": "4.2.0",
"typescript": "^5.0.0"
Expand Down
8 changes: 5 additions & 3 deletions back/src/socket/socket.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
RedisGameState,
TimeSyncPayload,
PhysicLoop,
applyInputs,
applyInputList,
} from '@benjaminbours/composite-core';
// local
import { PrismaService } from '../prisma.service';
Expand Down Expand Up @@ -298,16 +298,18 @@ export class SocketGateway {
.then((redisState) => GameState.parseRedisGameState(redisState)),
]).then(([inputsQueue, gameState]) => {
// console.log('inputs queue before', inputsQueue.length);
physicLoop.run(() => {
physicLoop.run((delta) => {
gameState.game_time++;
const inputsForTick = inputsQueue.filter(
({ sequence }) => sequence == gameState.game_time,
);
applyInputs(
applyInputList(
delta,
lastPlayersInput,
inputsForTick,
collidingScene.children,
gameState,
'server',
// true,
);
// then we remove it from the list
Expand Down
57 changes: 48 additions & 9 deletions front/app/Game/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import {
FLOOR,
Side,
SocketEventType,
updateGameState,
applySingleInput,
PhysicLoop,
applyInputs,
applyInputList,
ElementName,
} from '@benjaminbours/composite-core';
// local
// import SkyShader from './SkyShader';
Expand Down Expand Up @@ -129,10 +130,10 @@ export default class App {

this.socketController.getCurrentGameState = this.getCurrentGameState;
this.socketController.onGameStateUpdate = (gameState: GameState) => {
const gameTimeDelta =
this.currentState.game_time - gameState.game_time;
console.log('received update', gameState);
console.log('time delta', gameTimeDelta);
// console.log('received update', gameState);
// const gameTimeDelta =
// this.currentState.game_time - gameState.game_time;
// console.log('time delta', gameTimeDelta);
this.shouldReconciliateState = true;
this.serverGameState = gameState;
// this.checkServerState();
Expand Down Expand Up @@ -233,7 +234,7 @@ export default class App {
const item = object.children[i] as any;
if (item.update) {
if (item instanceof DoorOpener) {
item.update(this.delta, this.camera);
// do nothing
} else if (item instanceof Player) {
// do nothing
} else {
Expand Down Expand Up @@ -328,7 +329,8 @@ export default class App {
({ sequence }) =>
sequence == nextStateAtInterpolationTime.game_time,
);
applyInputs(
applyInputList(
this.physicLoop.delta,
lastPlayersInput,
inputsForTick,
[
Expand All @@ -338,6 +340,7 @@ export default class App {
]!.collidingElements,
],
nextStateAtInterpolationTime,
'client',
// true,
);
for (let i = 0; i < inputsForTick.length; i++) {
Expand Down Expand Up @@ -373,6 +376,9 @@ export default class App {
// other players interpolation
this.interpolation.shouldUpdate = true;
this.interpolation.ratio = 0;

// erase level state
this.currentState.level = this.serverGameState.level;
};

// private checkServerState = () => {
Expand Down Expand Up @@ -438,7 +444,7 @@ export default class App {
}
this.physicLoop.run((delta) => {
this.processInputs();
updateGameState(
applySingleInput(
delta,
this.playersConfig[0],
this.inputsManager.inputsActive,
Expand All @@ -449,6 +455,7 @@ export default class App {
]!.collidingElements,
],
this.currentState,
'client',
);
// this.gameStateHistory.push(
// JSON.parse(JSON.stringify(this.currentState)),
Expand Down Expand Up @@ -488,7 +495,39 @@ export default class App {
}
};

private updateWorldPhysic = () => {
for (const key in this.currentState.level.doors) {
const { activators } = this.currentState.level.doors[key];

const doorOpener = this.levelController.levels[
this.levelController.currentLevel
]!.collidingElements.find(
(object) => object.name === ElementName.AREA_DOOR_OPENER(key),
)?.children.find(
(object) => object.name === ElementName.DOOR_OPENER(key),
) as DoorOpener | undefined;

if (doorOpener) {
const isActivate =
activators.length > 0 && !doorOpener.shouldActivate;
const isDeactivating =
activators.length === 0 && doorOpener.shouldActivate;
if (isActivate) {
doorOpener.shouldActivate = true;
}

if (isDeactivating) {
doorOpener.shouldActivate = false;
}
}

const withFocusCamera = activators.includes(this.playersConfig[0]);
doorOpener?.update(this.delta, this.camera, withFocusCamera);
}
};

public updateWorld = () => {
this.updateWorldPhysic();
this.updateChildren(this.scene);
// update the floor to follow the player to be infinite
// this.floor.position.set(this.players[0].position.x, 0, 0);
Expand Down
1 change: 1 addition & 0 deletions front/app/Game/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ function Game({
useEffect(() => {
if (!tabIsHidden && isSynchronizingTime && socketController) {
socketController.synchronizeTime().then(() => {
appRef.current?.inputsManager.registerEventListeners();
setIsSynchronizingTime(false);
});
}
Expand Down
6 changes: 3 additions & 3 deletions front/app/Game/Player/InputsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ export default class InputsManager {
jump: false,
};

constructor() {
// TODO: Implements a destroy method
// TODO: Implements a destroy method
public registerEventListeners = () => {
window.addEventListener('keydown', this.handleKeydown.bind(this));
window.addEventListener('keyup', this.handleKeyup.bind(this));
}
};

public reset() {
this.inputsActive.left = false;
Expand Down
26 changes: 19 additions & 7 deletions front/app/Game/elements/DoorOpener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,15 @@ export class DoorOpener extends Object3D implements InteractiveComponent {

// TODO: Think about using a door opening system to manage this logic
// I don't like the fact its the element door opener who set the camera target
public update = (delta: number, camera: CustomCamera) => {
this.detectActivation(this.activate(camera), this.deactivate(camera));
public update = (
delta: number,
camera: CustomCamera,
withFocusCamera: boolean,
) => {
this.detectActivation(
this.activate(camera, withFocusCamera),
this.deactivate(camera),
);
this.updateShader(delta);
};

Expand Down Expand Up @@ -170,16 +177,21 @@ export class DoorOpener extends Object3D implements InteractiveComponent {
});
};

activate = (camera: CustomCamera) => () => {
camera.focusTarget(
this.doorInfo.cameraPosition,
new Vector3(0, 0.2, 0),
);
activate = (camera: CustomCamera, withFocusCamera: boolean) => () => {
console.log('deactivate');
if (withFocusCamera) {
camera.focusTarget(
this.doorInfo.cameraPosition,
new Vector3(0, 0.2, 0),
);
}
this.activateVFX();
this.openTheDoor();
};

deactivate = (camera: CustomCamera) => () => {
console.log('deactivate');

camera.unfocus();
this.deactivateVFX();
this.closeTheDoor();
Expand Down
4 changes: 2 additions & 2 deletions front/app/Game/levels/PositionLevelWithGraphic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ export class PositionLevelWithGraphic extends PositionLevel {

const doors = [
{
id: 'GROUND',
id: 'ground',
color: new Color('black'),
cameraPosition: new Vector3(50, 200),
},
{
id: 'ROOF',
id: 'roof',
color: new Color('white'),
cameraPosition: new Vector3(0, 100),
},
Expand Down
12 changes: 0 additions & 12 deletions front/jest.config.js

This file was deleted.

6 changes: 5 additions & 1 deletion front/next.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
});

/** @type {import('next').NextConfig} */
const nextConfig = {
webpack: (config, options) => {
Expand All @@ -10,4 +14,4 @@ const nextConfig = {
reactStrictMode: true,
};

module.exports = nextConfig;
module.exports = withBundleAnalyzer(nextConfig);
Loading

0 comments on commit 016c345

Please sign in to comment.