Skip to content

Commit

Permalink
feat: add render position
Browse files Browse the repository at this point in the history
  • Loading branch information
hxtree committed Dec 14, 2024
1 parent 6b4e3bc commit 0e71df9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 32 deletions.
2 changes: 1 addition & 1 deletion clients/player-client/src/core/GameEngine/GameEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const GameEngine: React.FC<GameEngineProps> = props => {
data.actors[actorIndex].movement.isInMotion = true;
data.actors[actorIndex].movement.currentPosition =
data.actors[actorIndex].movement.targetPosition;
data.actors[actorIndex].movement.targetPositionReached = false;
data.actors[actorIndex].movement.startTimestamp = DateTime.now();
data.actors[actorIndex].movement.movementDuration = Duration.fromObject({
seconds: 10,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ export class IsometricRender {
private findActorsByPosition(position: Coordinate3D): Actor[] {
return this._actors.filter(
actor =>
actor.position?.x === position.x &&
actor.position?.y === position.y &&
actor.position?.z === position.z,
actor.movement.renderPosition?.x === position.x &&
actor.movement.renderPosition?.y === position.y &&
actor.movement.renderPosition?.z === position.z,
);
}

Expand Down
29 changes: 12 additions & 17 deletions clients/player-client/src/dtos/Actor/Actor.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,19 @@ export class Actor {

lastUpdated?: DateTime; // Timestamp for when the actor's data was last updated

/**
* Returns the current position of the actor where they should be rendered from
*/
get position() {
const now = DateTime.now();
if (this.movement.endTimestamp && now > this.movement.endTimestamp) {
// Determine if the target position is closer to the camera than the current position
if (
this.movement.targetPosition.x > this.movement.currentPosition.x ||
this.movement.targetPosition.y > this.movement.currentPosition.y
) {
return this.movement.targetPosition;
}

if (this.movement.targetPositionReached) {
return this.movement.targetPosition;
}
return this.movement.currentPosition;
Expand All @@ -54,19 +64,4 @@ export class Actor {
}
return true;
}

get targetPositionReached() {
const now = DateTime.now();
if (this.endTimestamp && now > this.endTimestamp) {
return true;
}
return false;
}

get endTimestamp() {
if (!this.movement.movementDuration) {
return null;
}
return this.movement.startTimestamp.plus(this.movement.movementDuration);
}
}
8 changes: 6 additions & 2 deletions clients/player-client/src/dtos/Actor/ActorAnimation.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ export class ActorAnimation {
@IsDateString()
startTimestamp?: DateTime; // Time when the animation started

@IsDateString()
endTimestamp?: DateTime; // Time when the animation should end
get endTimestamp(): DateTime | null {
if (!this.startTimestamp || !this.duration) {
return null;
}
return this.startTimestamp.plus(this.duration);
}

get duration(): Duration {
return this.frameDuration.mapUnits(unit => unit * this.totalFrames);
Expand Down
48 changes: 39 additions & 9 deletions clients/player-client/src/dtos/Actor/ActorMovement.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IsBoolean, ValidateNested, IsDateString } from 'class-validator';
import { Type } from 'class-transformer';
import { Transform, Type } from 'class-transformer';
import { Duration, DateTime } from 'luxon';
import { Coordinate3d } from '../Coordinate3d.dto';

Expand All @@ -15,16 +15,46 @@ export class ActorMovement {
@Type(() => Coordinate3d)
targetPosition: Coordinate3d;

// TODO type for duration

movementDuration?: Duration; // Duration of the movement (in seconds) ???

@IsBoolean()
targetPositionReached: boolean;
@Transform(({ value }) => Duration.fromObject(value), { toClassOnly: true })
@Transform(({ value }) => value.toObject(), { toPlainOnly: true })
movementDuration?: Duration;

@IsDateString()
startTimestamp: DateTime;

@IsDateString()
endTimestamp?: DateTime;
/**
* Returns the current position of the actor where they should be rendered from
*/
get renderPosition(): Coordinate3d {
// Determine if the target position is closer to the camera than the current position
if (
this.targetPosition.x > this.currentPosition.x ||
this.targetPosition.y > this.currentPosition.y
) {
return this.targetPosition;
}
return this.position;
}

get position(): Coordinate3d {
if (this.targetPositionReached) {
return this.targetPosition;
}
return this.currentPosition;
}

get endTimestamp(): DateTime | null {
if (!this.startTimestamp || !this.movementDuration) {
return null;
}
return this.startTimestamp.plus(this.movementDuration);
}

get targetPositionReached(): boolean {
const now = DateTime.now();
if (this.endTimestamp && now > this.endTimestamp) {
return true;
}
return false;
}
}

0 comments on commit 0e71df9

Please sign in to comment.