nitro-renderer/packages/room/src/object/logic/PetLogic.ts

239 lines
8.0 KiB
TypeScript
Raw Normal View History

2024-03-20 02:53:17 +01:00
import { IAssetData, IRoomGeometry, IRoomObjectModel, IVector3D, MouseEventType, PetFigureData, PetType, RoomObjectVariable } from '@nitrots/api';
import { RoomObjectMouseEvent, RoomObjectMoveEvent, RoomSpriteMouseEvent } from '@nitrots/events';
import { Vector3d } from '@nitrots/utils';
import { ObjectAvatarChatUpdateMessage, ObjectAvatarExperienceUpdateMessage, ObjectAvatarFigureUpdateMessage, ObjectAvatarPetGestureUpdateMessage, ObjectAvatarPostureUpdateMessage, ObjectAvatarSelectedMessage, ObjectAvatarSleepUpdateMessage, ObjectAvatarUpdateMessage, RoomObjectUpdateMessage } from '../../messages';
import { MovingObjectLogic } from './MovingObjectLogic';
2021-03-17 03:02:09 +01:00
export class PetLogic extends MovingObjectLogic
{
private _selected: boolean;
2024-03-20 02:53:17 +01:00
private _reportedLocation: IVector3D;
2021-03-17 03:02:09 +01:00
private _postureIndex: number;
private _gestureIndex: number;
private _headDirectionDelta: number;
private _directions: number[];
private _talkingEndTimestamp: number;
private _gestureEndTimestamp: number;
private _expressionEndTimestamp: number;
constructor()
{
super();
2022-03-03 01:21:30 +01:00
this._selected = false;
this._reportedLocation = null;
this._postureIndex = 0;
this._gestureIndex = 0;
this._headDirectionDelta = 0;
this._directions = [];
this._talkingEndTimestamp = 0;
this._gestureEndTimestamp = 0;
this._expressionEndTimestamp = 0;
2021-03-17 03:02:09 +01:00
}
public getEventTypes(): string[]
{
2022-10-30 02:32:38 +01:00
const types = [RoomObjectMouseEvent.CLICK, RoomObjectMoveEvent.POSITION_CHANGED];
2021-03-17 03:02:09 +01:00
return this.mergeTypes(super.getEventTypes(), types);
}
public initialize(asset: IAssetData): void
{
2022-11-10 17:00:28 +01:00
if(!asset) return;
2021-03-17 03:02:09 +01:00
const model = this.object && this.object.model;
2022-11-10 17:00:28 +01:00
if(!model) return;
2021-03-17 03:02:09 +01:00
2022-11-10 17:00:28 +01:00
if(asset.logic)
2021-03-17 03:02:09 +01:00
{
2022-11-10 17:00:28 +01:00
if(asset.logic.model)
2021-03-17 03:02:09 +01:00
{
2021-08-19 09:33:05 +02:00
const directions = asset.logic.model.directions;
2022-11-10 17:00:28 +01:00
if(directions && directions.length)
2021-08-19 09:33:05 +02:00
{
2022-11-10 17:00:28 +01:00
for(const direction of directions) this._directions.push(direction);
2021-08-19 09:33:05 +02:00
this._directions.sort();
}
}
2021-03-17 03:02:09 +01:00
}
model.setValue(RoomObjectVariable.PET_ALLOWED_DIRECTIONS, this._directions);
}
public dispose(): void
{
2022-11-10 17:00:28 +01:00
if(this._selected && this.object)
2021-03-17 03:02:09 +01:00
{
2022-11-10 17:00:28 +01:00
if(this.eventDispatcher) this.eventDispatcher.dispatchEvent(new RoomObjectMoveEvent(RoomObjectMoveEvent.OBJECT_REMOVED, this.object));
2021-03-17 03:02:09 +01:00
}
2022-03-03 01:21:30 +01:00
this._directions = null;
this._reportedLocation = null;
2021-03-17 03:02:09 +01:00
}
public update(totalTimeRunning: number): void
{
super.update(totalTimeRunning);
2022-11-10 17:00:28 +01:00
if(this._selected && this.object)
2021-03-17 03:02:09 +01:00
{
2022-11-10 17:00:28 +01:00
if(this.eventDispatcher)
2021-03-17 03:02:09 +01:00
{
const location = this.object.getLocation();
2022-11-10 17:00:28 +01:00
if(((!this._reportedLocation || (this._reportedLocation.x !== location.x)) || (this._reportedLocation.y !== location.y)) || (this._reportedLocation.z !== location.z))
2021-03-17 03:02:09 +01:00
{
2022-11-10 17:00:28 +01:00
if(!this._reportedLocation) this._reportedLocation = new Vector3d();
2021-03-17 03:02:09 +01:00
this._reportedLocation.assign(location);
this.eventDispatcher.dispatchEvent(new RoomObjectMoveEvent(RoomObjectMoveEvent.POSITION_CHANGED, this.object));
}
}
}
2022-11-10 17:00:28 +01:00
if(this.object && this.object.model) this.updateModel(totalTimeRunning, this.object.model);
2021-03-17 03:02:09 +01:00
}
private updateModel(time: number, model: IRoomObjectModel): void
{
2022-11-10 17:00:28 +01:00
if((this._gestureEndTimestamp > 0) && (time > this._gestureEndTimestamp))
2021-03-17 03:02:09 +01:00
{
model.setValue(RoomObjectVariable.FIGURE_GESTURE, null);
this._gestureEndTimestamp = 0;
}
2022-11-10 17:00:28 +01:00
if(this._talkingEndTimestamp > 0)
2021-03-17 03:02:09 +01:00
{
2022-11-10 17:00:28 +01:00
if(time > this._talkingEndTimestamp)
2021-03-17 03:02:09 +01:00
{
model.setValue(RoomObjectVariable.FIGURE_TALK, 0);
this._talkingEndTimestamp = 0;
}
}
2022-11-10 17:00:28 +01:00
if((this._expressionEndTimestamp > 0) && (time > this._expressionEndTimestamp))
2021-03-17 03:02:09 +01:00
{
model.setValue(RoomObjectVariable.FIGURE_EXPRESSION, 0);
this._expressionEndTimestamp = 0;
}
}
public processUpdateMessage(message: RoomObjectUpdateMessage): void
{
2022-11-10 17:00:28 +01:00
if(!message || !this.object) return;
2021-03-17 03:02:09 +01:00
super.processUpdateMessage(message);
const model = this.object && this.object.model;
2022-11-10 17:00:28 +01:00
if(!model) return;
2021-03-17 03:02:09 +01:00
2022-11-10 17:00:28 +01:00
if(message instanceof ObjectAvatarUpdateMessage)
2021-03-17 03:02:09 +01:00
{
model.setValue(RoomObjectVariable.HEAD_DIRECTION, message.headDirection);
return;
}
2022-11-10 17:00:28 +01:00
if(message instanceof ObjectAvatarFigureUpdateMessage)
2021-03-17 03:02:09 +01:00
{
const petFigureData = new PetFigureData(message.figure);
model.setValue(RoomObjectVariable.FIGURE, message.figure);
model.setValue(RoomObjectVariable.RACE, message.subType);
model.setValue(RoomObjectVariable.PET_PALETTE_INDEX, petFigureData.paletteId);
model.setValue(RoomObjectVariable.PET_COLOR, petFigureData.color);
model.setValue(RoomObjectVariable.PET_TYPE, petFigureData.typeId);
model.setValue(RoomObjectVariable.PET_CUSTOM_LAYER_IDS, petFigureData.customLayerIds);
model.setValue(RoomObjectVariable.PET_CUSTOM_PARTS_IDS, petFigureData.customPartIds);
model.setValue(RoomObjectVariable.PET_CUSTOM_PALETTE_IDS, petFigureData.customPaletteIds);
model.setValue(RoomObjectVariable.PET_IS_RIDING, (message.isRiding ? 1 : 0));
return;
}
2022-11-10 17:00:28 +01:00
if(message instanceof ObjectAvatarPostureUpdateMessage)
2021-03-17 03:02:09 +01:00
{
model.setValue(RoomObjectVariable.FIGURE_POSTURE, message.postureType);
return;
}
2022-11-10 17:00:28 +01:00
if(message instanceof ObjectAvatarChatUpdateMessage)
2021-03-17 03:02:09 +01:00
{
model.setValue(RoomObjectVariable.FIGURE_TALK, 1);
this._talkingEndTimestamp = this.time + (message.numberOfWords * 1000);
return;
}
2022-11-10 17:00:28 +01:00
if(message instanceof ObjectAvatarSleepUpdateMessage)
2021-03-17 03:02:09 +01:00
{
model.setValue(RoomObjectVariable.FIGURE_SLEEP, message.isSleeping ? 1 : 0);
return;
}
2022-11-10 17:00:28 +01:00
if(message instanceof ObjectAvatarPetGestureUpdateMessage)
2021-03-17 03:02:09 +01:00
{
model.setValue(RoomObjectVariable.FIGURE_GESTURE, message.gesture);
this._gestureEndTimestamp = this.time + 3000;
return;
}
2022-11-10 17:00:28 +01:00
if(message instanceof ObjectAvatarSelectedMessage)
2021-03-17 03:02:09 +01:00
{
2022-03-03 01:21:30 +01:00
this._selected = message.selected;
this._reportedLocation = null;
2021-03-17 03:02:09 +01:00
return;
}
2021-07-14 09:10:54 +02:00
2022-11-10 17:00:28 +01:00
if(message instanceof ObjectAvatarExperienceUpdateMessage)
2021-07-14 09:10:54 +02:00
{
model.setValue(RoomObjectVariable.FIGURE_EXPERIENCE_TIMESTAMP, this.time);
model.setValue(RoomObjectVariable.FIGURE_GAINED_EXPERIENCE, message.gainedExperience);
return;
}
2021-03-17 03:02:09 +01:00
}
public mouseEvent(event: RoomSpriteMouseEvent, geometry: IRoomGeometry): void
{
let eventType: string = null;
2022-11-10 17:00:28 +01:00
switch(event.type)
2021-03-17 03:02:09 +01:00
{
case MouseEventType.MOUSE_CLICK:
eventType = RoomObjectMouseEvent.CLICK;
break;
case MouseEventType.DOUBLE_CLICK:
break;
case MouseEventType.MOUSE_DOWN: {
const petType = this.object.model.getValue<number>(RoomObjectVariable.PET_TYPE);
2022-11-10 17:00:28 +01:00
if(petType === PetType.MONSTERPLANT)
2021-03-17 03:02:09 +01:00
{
2022-11-10 17:00:28 +01:00
if(this.eventDispatcher) this.eventDispatcher.dispatchEvent(new RoomObjectMouseEvent(RoomObjectMouseEvent.MOUSE_DOWN, this.object, event.eventId, event.altKey, event.ctrlKey, event.shiftKey, event.buttonDown));
2021-03-17 03:02:09 +01:00
}
break;
}
}
2022-11-10 17:00:28 +01:00
if(eventType && this.eventDispatcher) this.eventDispatcher.dispatchEvent(new RoomObjectMouseEvent(eventType, this.object, event.eventId, event.altKey, event.ctrlKey, event.shiftKey, event.buttonDown));
2021-03-17 03:02:09 +01:00
}
2021-06-07 10:28:52 +02:00
}