Files
esengine/packages/network/src/components/NetworkTransform.ts

101 lines
2.2 KiB
TypeScript
Raw Normal View History

import { Component, ECSComponent, Serialize, Serializable, Property } from '@esengine/ecs-framework';
/**
*
* Network transform component
*
*
* Syncs entity position and rotation with interpolation smoothing.
*/
@ECSComponent('NetworkTransform', { requires: ['NetworkIdentity'] })
@Serializable({ version: 1, typeId: 'NetworkTransform' })
export class NetworkTransform extends Component {
/**
* X
* Target position X
*/
public targetX: number = 0;
/**
* Y
* Target position Y
*/
public targetY: number = 0;
/**
*
* Target rotation
*/
public targetRotation: number = 0;
/**
* X
* Current position X
*/
public currentX: number = 0;
/**
* Y
* Current position Y
*/
public currentY: number = 0;
/**
*
* Current rotation
*/
public currentRotation: number = 0;
/**
*
* Interpolation speed
*/
@Serialize()
@Property({ type: 'number', label: 'Lerp Speed', min: 0.1, max: 50 })
public lerpSpeed: number = 10;
/**
*
* Enable interpolation
*/
@Serialize()
@Property({ type: 'boolean', label: 'Interpolate' })
public bInterpolate: boolean = true;
/**
* (ms)
* Sync interval in milliseconds
*/
@Serialize()
@Property({ type: 'number', label: 'Sync Interval', min: 16 })
public syncInterval: number = 50;
/**
*
* Last sync time
*/
public lastSyncTime: number = 0;
/**
*
* Set target position
*/
public setTarget(x: number, y: number, rotation?: number): void {
this.targetX = x;
this.targetY = y;
if (rotation !== undefined) {
this.targetRotation = rotation;
}
}
/**
*
* Snap to target position immediately
*/
public snap(): void {
this.currentX = this.targetX;
this.currentY = this.targetY;
this.currentRotation = this.targetRotation;
}
}