Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,6 @@ export class AuthGoogleExchangeService {
};
}


private async initPlayerStats(user: ExchangeResult['user']): Promise<void> {
const payload: Record<string, unknown> = {
userId: user.id,
Expand Down
25 changes: 13 additions & 12 deletions apps/frontend/lib/babylon/data/achievments.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@

export class Achievements {

private achievements: any =
{
public achievements: Record<string, boolean> = {
"marathon-mayem": false,
"the early bird catches the worm!": false,
"first blood!": false,
"jump, JUMP, JUMP!!!": false,
"eagle eye": false,
"chip-in!": false
}
"chip-in!": false,
};

private distanceCounter: number = 0;
private marathonDistance: number = 20;
private distanceCounter = 0;
private marathonDistance = 20;
private userId: string;

constructor() {}
constructor(userId: string) {
this.userId = userId;
console.log(this.userId);
}

addToDistance(amount: number){
async addToDistance(amount: number) {
this.distanceCounter += amount;
if (this.distanceCounter >= this.marathonDistance && !this.achievements["marathon-mayem"])
{

if (this.distanceCounter >= this.marathonDistance && !this.achievements["marathon-mayem"]) {
this.achievements["marathon-mayem"] = true;
console.log("achievement unlocked!: marathon-mayem");
}
Expand Down
17 changes: 16 additions & 1 deletion apps/frontend/lib/babylon/gui/GuiHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import type { msgToServerType } from '@/lib/packets/msgToServerType';
import { stateUi } from '../state/state_ui/stateUi';
import { GameNotifications } from "../gui/GameNotifications";
import { SocketStatus } from "./SocketStatus";
import { StateMachine } from "../state/StateMachine";
import { Achievement } from "next-auth/providers/42-school";
import { Achievements } from "../data/achievments";
import { distance } from "framer-motion";

export class GuiHelper {
public socketStatus: SocketStatus;
Expand All @@ -15,12 +19,13 @@ export class GuiHelper {
public notifications: GameNotifications;
private resizeFunctions: Array<() => void> = [];
constructor(
machine: StateMachine,
scene: Scene,
canvas: HTMLCanvasElement,
clientId: string,
msgToServer: msgToServerType
) {
const count = 0;
let count = 0;
// Text hitboxes may overlap with buttons and take over control

// GUI for non-interactable text
Expand All @@ -47,6 +52,16 @@ export class GuiHelper {
msgToServer<CS_DEV_StartEndscreen>(CS_Type.CS_DEV_StartEndscreen, {
won: false,
winnerId: clientId,
payload: {
userId: machine.userId,
type: "marathon-mayem" ,
name: "Marathon Mayem",
description: "Travel 20 meters total",
achieved: machine.achievements.achievements["marathon-mayem"],
progress: 10,
progressTarget: 50,
meta: { distance: 8 }
},
});
});
this.resizeFunctions.push(() => {
Expand Down
1 change: 1 addition & 0 deletions apps/frontend/lib/babylon/state/4_turn_start/Turn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { StateMachine } from "../StateMachine";
import { ImportMesh } from "../1_loading/ImportMesh";
import { aimingMeshes } from "../1_loading/loadGame";
import { DotTail } from "../1_loading/DotTail";
import { Projectile } from "../8_turn_end/Projectile";

const pi2 = Math.PI * 2;

Expand Down
12 changes: 9 additions & 3 deletions apps/frontend/lib/babylon/state/6_movement/movementTick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,19 @@ function forAllWorms(

// Remove gravity if on ground
if (worm.onGround)
{
// console.log("My Gravity is 0!");
worm.aggregate.body.setGravityFactor(0);
}
else
{
// console.log("My Gravity is 1!");
worm.aggregate.body.setGravityFactor(1);

// When player is moving, send packet to update clients and server with that players position
if (isActive && isMovement && worm.aggregate.body.getLinearVelocity().length() > 0.01) {
broadcastPosition(worm.id, worm.collider.position.x, worm.collider.position.y);
// When player is moving, send packet to update clients and server with that players position
if (isActive && isMovement && worm.aggregate.body.getLinearVelocity().length() > 0.01) {
broadcastPosition(worm.id, worm.collider.position.x, worm.collider.position.y);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions apps/frontend/lib/babylon/state/7_aiming/AimingState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function sendAimingDone(machine: StateMachine) {
(machine.loaded.turn.chosenWeapon?.getProjectileSpawnPos()?.y ??
machine.loaded.turn.chosenWorm.collider.position.y);
machine.msgToServer<CS_EndAimState>(CS_Type.CS_EndAimState, {
id: machine.loaded.turn.chosenWeapon?.weaponId ?? 0,
wormAngle: data.wormAngle,
position: {
x: pos_x,
Expand Down
53 changes: 53 additions & 0 deletions apps/frontend/lib/babylon/state/8_turn_end/Projectile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Mesh, MeshBuilder, PhysicsAggregate, PhysicsMotionType, PhysicsShape, PhysicsShapeType, Scene, StateCondition, Vector3 } from "@babylonjs/core";
import { StateMachine } from "../StateMachine";
import { ThinSSRBlurCombinerPostProcess } from "@babylonjs/core/PostProcesses/thinSSRBlurCombinerPostProcess";

export class Projectile {

private speed: number = 10000000;

public type: number = 0;
public launchPos: Vector3 = Vector3.Zero();
public launchAngle: number = 0;
public endPos: Vector3 = Vector3.Zero();

// public mesh: Mesh;
// public aggregate: PhysicsAggregate;

constructor() {}

launchProjectile(scene: Scene)
{
const mesh = MeshBuilder.CreateSphere
(
"projectile",
{ diameter: 0.5 },
scene
);
mesh.position = this.launchPos;

const aggregate = new PhysicsAggregate
(
mesh,
PhysicsShapeType.SPHERE,
{ mass: 1, restitution: 0, friction: 50 },
scene
);
aggregate.body.setMotionType(PhysicsMotionType.DYNAMIC);
mesh.isVisible = true;

console.log(`Received angle: ${this.launchAngle / Math.PI * 180}, turned into ${mesh.rotation.z / Math.PI * 180}`);

// this.mesh.isVisible = true;
// this.aggregate.body.setCollisionCallbackEnabled(true);

const direction2 = new Vector3(Math.sin(this.launchAngle), Math.cos(this.launchAngle), 0);
console.log("We go in", direction2);
aggregate.body.applyImpulse(direction2.scale(this.speed), mesh.getAbsolutePosition());

console.log("Launch position: ", this.launchPos);
console.log("Mesh position: ", mesh.position);
console.log("Launch angle: ", mesh.rotation.z);
console.log("Of course i did xd!");
}
}
8 changes: 7 additions & 1 deletion apps/frontend/lib/babylon/state/8_turn_end/TurnEndState.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SC_TurnEnds, SC_Type } from '@/shared/packets/ServerClientPackets';
import { IState } from '../IState'
import { StateMachine } from '../StateMachine';
import { IAction, Vector3 } from '@babylonjs/core'
import { ActionManager, ExecuteCodeAction, IAction, MeshBuilder, PhysicsAggregate, PhysicsMotionType, PhysicsShapeType, Vector3 } from '@babylonjs/core'
import { CS_ClientFinishedTurn, CS_Type } from '@/shared/packets/ClientServerPackets';

/**
Expand All @@ -27,7 +27,13 @@ export class TurnEndState implements IState {
// Setup
turnMessage(this.machine);

// if (!this.machine.loaded)
// return;

// const projectile = this.machine.loaded.turn.projectile;

// Actions
// projectile.launchProjectile(this.machine.scene);
}

tick() {
Expand Down
10 changes: 10 additions & 0 deletions apps/frontend/lib/babylon/state/9_game_end/GameEndState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ export class GameEndState implements IState {
this.machine.msgToServer<CS_DEV_StartEndscreen>(CS_Type.CS_DEV_StartEndscreen, {
won: false,
winnerId: "",
payload: {
userId: this.machine.userId,
type: "marathon-mayem" ,
name: "Marathon Mayem",
description: "Travel 20 meters total",
achieved: this.machine.achievements.achievements["marathon-mayem"],
progress: 10,
progressTarget: 50,
meta: { distance: 8 }
}
});
}
}
Expand Down
6 changes: 4 additions & 2 deletions apps/frontend/lib/babylon/state/StateMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { aimingMeshes } from './1_loading/loadGame';
import { Achievements } from '../data/achievments';
import { movementTick } from './6_movement/movementTick';
import { ExplosionParticles } from './8_turn_end/ExplosionParticles';
import { useDeprecatedInvertedScale } from 'framer-motion';

// Stores the part of the statemachine that are created in the loading step
export interface loaded {
Expand Down Expand Up @@ -89,7 +90,7 @@ export class StateMachine {
this.guiHelper = undefined;
this.loaded = undefined;
this.activePlayerId = "";
this.achievements = new Achievements();
this.achievements = new Achievements(this.userId);
}

// Called only once per canvas, when sockets have been set up
Expand Down Expand Up @@ -154,7 +155,8 @@ export class StateMachine {
// Set up a fresh Game
this.log("Setting up new Game");

this.guiHelper = new GuiHelper(this.scene, this.canvas, this.userId, this.msgToServer);

this.guiHelper = new GuiHelper(this, this.scene, this.canvas, this.userId, this.msgToServer);
// Need to prompt socket to update the UI if its connected
this.queue?.updateSocketUi();
//this.msgToServer<CS_GetGameState>(CS_Type.CS_GetGameState, {});
Expand Down
2 changes: 1 addition & 1 deletion apps/frontend/lib/packets/handlePacket.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SC_Type, SC_GenericPacket, frontendServerPackets, SC_ExplosionOccurs } from "@/shared/packets/ServerClientPackets"
import { StateMachine } from '../babylon/state/StateMachine';
import { GameState } from '@/shared/state/GameState';
import { Nullable } from "@babylonjs/core";
import { Nullable, Vector3 } from "@babylonjs/core";
import { Control, TextBlock } from "@babylonjs/gui";
import { Player } from "../babylon/player/Player";
import { Worm } from '../babylon/player/Worm';
Expand Down
Loading
Loading