Skip to content

Commit

Permalink
tidying some of the client code
Browse files Browse the repository at this point in the history
  • Loading branch information
orion3dgames committed Jul 3, 2024
1 parent 4e4d7b1 commit c90fd0a
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 198 deletions.
2 changes: 1 addition & 1 deletion src/client/Controllers/UI/HotBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class HotBar {
});

headlineRect.onPointerClickObservable.add(() => {
if (!this._currentPlayer.isCasting) {
if (!this._currentPlayer.abilityController.isCasting) {
this._game.sendMessage(ServerMsg.PLAYER_HOTBAR_ACTIVATED, {
senderId: this._room.sessionId,
targetId: this._game?.selectedEntity?.sessionId ?? false,
Expand Down
4 changes: 4 additions & 0 deletions src/client/Controllers/UI/Panels/Panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ export class Panel {
}
}

public isOpen(): boolean {
return this._panel.isVisible;
}

// update panel
public update() {}

Expand Down
88 changes: 17 additions & 71 deletions src/client/Entities/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,93 +203,39 @@ export class Entity extends TransformNode {
}

public update(delta): any {
////////////////////////////////////
// what to do when an entity dies
if (this.health < 1 && !this.isDead) {
this.isDead = true;

// remove from player selection
this._game.selectedEntity = null;

// remove selected mesh
if(this.meshController){
this.meshController.selectedMesh.isVisible = false;

// remove any action managers
if (this.meshController.mesh.actionManager) {
this.meshController.mesh.actionManager.dispose();
this.meshController.mesh.actionManager = null;
}
}

// hide interactable button
if (this.interactableButtons) {
this.interactableButtons.isVisible = false;
}

// hide any dialog this entity could be linked too
if (this._ui.panelDialog.currentEntity && this._ui.panelDialog.currentEntity.sessionId === this.sessionId) {
this._ui.panelDialog.clear();
this._ui.panelDialog.close();
}
}

////////////////////////////////////
if (this.health > 0) {
this.isDead = false;
}

////////////////////////////////////
// animate player continuously
// choose entity animation state
if (this.animatorController) {
this.animatorController.animate(this);
}

////////////////////////////////////
// only do the below if entity is not dead
if (!this.isDead) {
// if entity is selected, show
if (this.selectedMesh && this.selectedMesh.visibility) {
if (this._game.selectedEntity && this._game.selectedEntity.sessionId === this.sessionId) {
this.selectedMesh.isVisible = true;
this.selectedMesh.rotate(new Vector3(0, 0.1, 0), 0.01);
} else {
this.selectedMesh.isVisible = false;
}
}

// if entity has aggro
if (this.ai_state === AI_STATE.SEEKING || this.ai_state === AI_STATE.ATTACKING) {
if (this.debugMesh) {
this.debugMesh.material = this.debugMaterialActive;
}
}

// if entity lose aggro
if (this.ai_state === AI_STATE.WANDER) {
if (this.debugMesh) {
this.debugMesh.material = this.debugMaterialNeutral;
}
}

// tween entity
if (this && this.moveController) {
this.moveController.tween();
}
// move update
if (this && this.moveController) {
this.moveController.update();
}

// animate player continuously
if (this.animatorController) {
// animate player continuously
// refresh animation ratio
this.animatorController.refreshAnimationRatio();

// animate player continuously
this.animatorController.play(this);
}

//
// update nameplate
if (this.nameplateController) {
this.nameplateController.update();
}

// if entity is selected, show
if (this.selectedMesh && this.selectedMesh.visibility) {
if (this._game.selectedEntity && this._game.selectedEntity.sessionId === this.sessionId) {
this.selectedMesh.isVisible = true;
this.selectedMesh.rotate(new Vector3(0, 0.1, 0), 0.01);
} else {
this.selectedMesh.isVisible = false;
}
}
}

public updateServerRate(delta) {}
Expand Down
21 changes: 9 additions & 12 deletions src/client/Entities/Entity/EntityMove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,20 @@ export class EntityMove {
this.playerInputs.push(latestInput);
}

// move transform node
public tween(tween: number = 0.1): void {
// update loop
public update(tween: number = 0.1): void {
// continuously lerp between current position and next position
this._node.position = Vector3.Lerp(this._node.position, this.nextPosition, tween);

// move camera at the same time
if (this._node.isCurrentPlayer) {
// this._node.cameraController._camRoot.position = Vector3.Lerp(this._node.cameraController._camRoot.position, this.nextPosition, tween);
// continuously lerp between current rotation and next rotation
const gap = Math.abs(this._node.rotation.y - this.nextRotation.y);
if (gap > Math.PI) {
this._node.rotation.y = this.nextRotation.y;
} else {
this._node.rotation = Vector3.Lerp(this._node.rotation, this.nextRotation, 0.45);
}

// rotation
// TODO DAYD : make it better
// maybe look into Scalar.LerpAngle ??? https://doc.babylonjs.com/typedoc/classes/BABYLON.Scalar#LerpAngle
const gap = Math.abs(this._node.rotation.y - this.nextRotation.y);
if (gap > Math.PI) this._node.rotation.y = this.nextRotation.y;
else this._node.rotation = Vector3.Lerp(this._node.rotation, this.nextRotation, 0.45);
// camera
// rotate camera to copy player rotation
if (this._node.isCurrentPlayer) {
this._node.cameraController._camRoot.rotation.y = -this._node.rotation.y + this._game.deltaCamY;
}
Expand Down
128 changes: 14 additions & 114 deletions src/client/Entities/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@ import { Entity } from "./Entity";
import State from "../../client/Screens/Screens";
import { Ability, ServerMsg } from "../../shared/types";
import { GameScene } from "../Screens/GameScene";
import { SoundController } from "../Controllers/SoundController";
import { PlayerAbility } from "./Player/PlayerAbility";

export class Player extends Entity {
public game;
public entities;
public interval;

public isCasting: boolean = false;
public castingDigit: number = 0;
public castingTimer;
public castingElapsed: number = 0;
public castingTarget: number = 0;
public ability_in_cooldown;
public abilityController: PlayerAbility;

public onPointerObservable;

Expand All @@ -40,8 +34,6 @@ export class Player extends Entity {

this._input = gamescene._input;

this.ability_in_cooldown = [false, false, false, false, false, false, false, false, false, false, false];

this.type = "player";

this.spawnPlayer();
Expand All @@ -52,6 +44,7 @@ export class Player extends Entity {
this.cameraController = this._gamescene._camera;
this.cameraController.attach(this);
this.actionsController = new EntityActions(this._scene, this._game._loadedAssets, this.entities, this._gamescene);
this.abilityController = new PlayerAbility(this);

// register player server messages
this.registerServerMessages();
Expand Down Expand Up @@ -86,9 +79,6 @@ export class Player extends Entity {
}
}
});

// add chat label
//this.characterChatLabel = this._ui.createEntityChatLabel(this);
}

getMeshMetadata(pointerInfo) {
Expand Down Expand Up @@ -216,15 +206,11 @@ export class Player extends Entity {
// run super function first
super.update(delta);

// action controller
if (this.actionsController) {
this.actionsController.update();
}

if (this && this.moveController) {
// tween entity
this.moveController.tween();
}

// update camera
this.cameraController.update();
}
Expand All @@ -239,68 +225,28 @@ export class Player extends Entity {
this.moveController.processMove();
}

///////////// ABILITY & CASTING EVENTS ///////////////////////////
// if digit pressed
if (this._input.digit_pressed > 0 && !this.isCasting) {
// get all necessary vars
let digit = this._input.digit_pressed;
let target = this._game.selectedEntity;

// send to server
this._game.sendMessage(ServerMsg.PLAYER_HOTBAR_ACTIVATED, {
senderId: this._room.sessionId,
targetId: target ? target.sessionId : false,
digit: digit,
});

// clear digit
this._input.digit_pressed = 0;
}

// check if casting
if (this.isCasting === true) {
// increment casting timer
this._ui._CastingBar.open();
this.castingElapsed += delta; // increment casting timer by server delta
let widthInPercentage = ((this.castingElapsed / this.castingTarget) * 100) / 100; // percentage between 0 and 1
let text = this.castingElapsed + "/" + this.castingTarget;
let width = widthInPercentage;
this._ui._CastingBar.update(text, width);
if (this.abilityController) {
this.abilityController.update(delta);
}

// check for cooldowns (estethic only as server really controls cooldowns)
this.ability_in_cooldown.forEach((cooldown, digit) => {
if (cooldown > 0) {
let cooldownUI = this._ui.MAIN_ADT.getControlByName("ability_" + digit + "_cooldown");
let ability = this.getAbilityByDigit(digit) as Ability;
if (ability && cooldownUI) {
this.ability_in_cooldown[digit] -= delta;
let percentage = ((this.ability_in_cooldown[digit] / ability.cooldown) * 100) / 100;
cooldownUI.height = percentage;
}
}
});

///////////// RESSURECT EVENTS ///////////////////////////
// if dead
// if dead, show ressurect panel
if (!this.isDeadUI && this.health < 1) {
this._ui._RessurectBox.open();
this.cameraController.vfx_black_and_white_on();
this.isDeadUI = true;
}

// if ressurect
// if ressurected, hide panel
if (this.isDeadUI && this.health > 0) {
this.cameraController.vfx_black_and_white_off();
this._ui._RessurectBox.close();
this.isDeadUI = false;
}

// sounds
if (this.isMoving && this.footstepCurrent > this.footstepInterval) {
this._gamescene._sound.play("SOUND_player_walking");
this.footstepCurrent = 0;
}

this.footstepCurrent += delta;
}

Expand Down Expand Up @@ -364,84 +310,38 @@ export class Player extends Entity {
});
}

public getAbilityByDigit(digit): Ability | boolean {
let found = false;
this.player_data.hotbar.forEach((element) => {
if (element.digit === digit) {
found = this._game.getGameData("ability", element.key);
}
});
return found;
}

// player is casting
public startCasting(data) {
let digit = data.digit;
let ability = this.getAbilityByDigit(digit) as Ability;
if (ability) {
this.isCasting = true;
this.castingElapsed = 0;
this.castingTarget = ability.castTime;
this.castingDigit = digit;
this._ui._CastingBar.open();
}
}

// player cancel casting
public stopCasting(data) {
this.isCasting = false;
this.castingElapsed = 0;
this.castingTarget = 0;
this.castingDigit = 0;
this._ui._CastingBar.close();
}

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// server message handler

public registerServerMessages() {
this._room.onMessage(ServerMsg.SERVER_MESSAGE, (data) => {
console.log("ServerMsg.SERVER_MESSAGE", data);
this._ui._ChatBox.addNotificationMessage(data.type, data.message, data.message);
});

// on teleport confirmation
this._room.onMessage(ServerMsg.PLAYER_TELEPORT, (location) => {
console.log(location);
console.log("ServerMsg.PLAYER_TELEPORT", location);
this.teleport(location);
});

// server confirm player can start casting
this._room.onMessage(ServerMsg.PLAYER_CASTING_START, (data) => {
console.log("ServerMsg.PLAYER_CASTING_START", data);
this.startCasting(data);
this.abilityController.startCasting(data);
});

this._room.onMessage(ServerMsg.PLAYER_CASTING_CANCEL, (data) => {
console.log("ServerMsg.PLAYER_CASTING_CANCEL", data);
this.stopCasting(data);
this.abilityController.stopCasting(data);
});

// server confirms ability can be cast
this._room.onMessage(ServerMsg.PLAYER_ABILITY_CAST, (data) => {
console.log("ServerMsg.PLAYER_ABILITY_CAST", data);
let digit = data.digit;
let ability = this.getAbilityByDigit(digit) as Ability;
if (ability) {
// if you are sender, cancel casting and strat cooldown on client
if (data.fromId === this.sessionId) {
// cancel casting
this.castingElapsed = 0;
this.castingTarget = 0;
this.isCasting = false;
this._ui._CastingBar.close();
this.ability_in_cooldown[digit] = ability.cooldown; // set cooldown
}

// action ability
this.actionsController.process(this, data, ability);
}
this.abilityController.processServerCasting(data);
});
}

Expand Down
Loading

0 comments on commit c90fd0a

Please sign in to comment.