nitro-renderer/src/nitro/room/object/logic/MovingObjectLogic.ts

148 lines
3.9 KiB
TypeScript
Raw Normal View History

2022-11-02 20:32:38 +01:00
import { IRoomObjectController, IRoomObjectUpdateMessage, IVector3D, RoomObjectVariable, Vector3d } from '../../../../api';
import { RoomObjectLogicBase } from '../../../../room';
2022-11-02 21:08:56 +01:00
import { ObjectMoveUpdateMessage } from '../../messages';
2021-03-17 03:02:09 +01:00
export class MovingObjectLogic extends RoomObjectLogicBase
{
2022-03-18 05:59:22 +01:00
public static DEFAULT_UPDATE_INTERVAL: number = 500;
2021-03-17 03:02:09 +01:00
private static TEMP_VECTOR: Vector3d = new Vector3d();
private _liftAmount: number;
private _location: Vector3d;
private _locationDelta: Vector3d;
private _lastUpdateTime: number;
private _changeTime: number;
2022-03-18 05:59:22 +01:00
private _updateInterval: number;
2021-03-17 03:02:09 +01:00
constructor()
{
super();
2022-03-03 01:21:30 +01:00
this._liftAmount = 0;
2021-03-17 03:02:09 +01:00
2022-03-03 01:21:30 +01:00
this._location = new Vector3d();
this._locationDelta = new Vector3d();
this._lastUpdateTime = 0;
this._changeTime = 0;
2022-03-18 05:59:22 +01:00
this._updateInterval = MovingObjectLogic.DEFAULT_UPDATE_INTERVAL;
2021-03-17 03:02:09 +01:00
}
2021-08-11 04:28:21 +02:00
protected onDispose(): void
2021-03-17 03:02:09 +01:00
{
this._liftAmount = 0;
2021-08-11 04:28:21 +02:00
super.onDispose();
2021-03-17 03:02:09 +01:00
}
public update(time: number): void
{
super.update(time);
2022-03-03 01:21:30 +01:00
const locationOffset = this.getLocationOffset();
const model = this.object && this.object.model;
2021-03-17 03:02:09 +01:00
2022-11-02 23:44:30 +01:00
if(model)
2021-03-17 03:02:09 +01:00
{
2022-11-02 23:44:30 +01:00
if(locationOffset)
2021-03-17 03:02:09 +01:00
{
2022-11-02 23:44:30 +01:00
if(this._liftAmount !== locationOffset.z)
2021-03-17 03:02:09 +01:00
{
this._liftAmount = locationOffset.z;
model.setValue(RoomObjectVariable.FURNITURE_LIFT_AMOUNT, this._liftAmount);
}
}
else
{
2022-11-02 23:44:30 +01:00
if(this._liftAmount !== 0)
2021-03-17 03:02:09 +01:00
{
this._liftAmount = 0;
model.setValue(RoomObjectVariable.FURNITURE_LIFT_AMOUNT, this._liftAmount);
}
}
}
2022-11-02 23:44:30 +01:00
if((this._locationDelta.length > 0) || locationOffset)
2021-03-17 03:02:09 +01:00
{
const vector = MovingObjectLogic.TEMP_VECTOR;
let difference = (this.time - this._changeTime);
2022-11-02 23:44:30 +01:00
if(difference === (this._updateInterval >> 1)) difference++;
2021-03-17 03:02:09 +01:00
2022-11-02 23:44:30 +01:00
if(difference > this._updateInterval) difference = this._updateInterval;
2021-03-17 03:02:09 +01:00
2022-11-02 23:44:30 +01:00
if(this._locationDelta.length > 0)
2021-03-17 03:02:09 +01:00
{
vector.assign(this._locationDelta);
2022-03-18 05:59:22 +01:00
vector.multiply((difference / this._updateInterval));
2021-03-17 03:02:09 +01:00
vector.add(this._location);
}
else
{
vector.assign(this._location);
}
2022-11-02 23:44:30 +01:00
if(locationOffset) vector.add(locationOffset);
2021-03-17 03:02:09 +01:00
this.object.setLocation(vector);
2022-11-02 23:44:30 +01:00
if(difference === this._updateInterval)
2021-03-17 03:02:09 +01:00
{
this._locationDelta.x = 0;
this._locationDelta.y = 0;
this._locationDelta.z = 0;
}
}
this._lastUpdateTime = this.time;
}
public setObject(object: IRoomObjectController): void
{
super.setObject(object);
2022-11-02 23:44:30 +01:00
if(object) this._location.assign(object.getLocation());
2021-03-17 03:02:09 +01:00
}
2022-10-30 07:08:37 +01:00
public processUpdateMessage(message: IRoomObjectUpdateMessage): void
2021-03-17 03:02:09 +01:00
{
2022-11-02 23:44:30 +01:00
if(!message) return;
2021-03-17 03:02:09 +01:00
super.processUpdateMessage(message);
2022-11-02 23:44:30 +01:00
if(message.location) this._location.assign(message.location);
2021-03-17 03:02:09 +01:00
2022-11-02 23:44:30 +01:00
if(message instanceof ObjectMoveUpdateMessage) return this.processMoveMessage(message);
2021-03-17 03:02:09 +01:00
}
private processMoveMessage(message: ObjectMoveUpdateMessage): void
{
2022-11-02 23:44:30 +01:00
if(!message || !this.object || !message.location) return;
2021-03-17 03:02:09 +01:00
this._changeTime = this._lastUpdateTime;
this._locationDelta.assign(message.targetLocation);
this._locationDelta.subtract(this._location);
}
protected getLocationOffset(): IVector3D
{
return null;
}
protected get lastUpdateTime(): number
{
return this._lastUpdateTime;
}
2022-03-18 05:59:22 +01:00
protected set updateInterval(interval: number)
{
2022-11-02 23:44:30 +01:00
if(interval <= 0) interval = 1;
2022-03-18 05:59:22 +01:00
this._updateInterval = interval;
}
2021-08-11 04:28:21 +02:00
}