-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
BuilderBotDragOperation.ts
133 lines (118 loc) · 3.93 KB
/
BuilderBotDragOperation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { Intersection, Vector3, Vector2 } from 'three';
import { Physics } from '../../../shared/scene/Physics';
import { Bot, BotTags } from '@casual-simulation/aux-common/bots';
import {
BotCalculationContext,
isMinimized,
} from '@casual-simulation/aux-common';
import { BuilderGroup3D } from '../../../shared/scene/BuilderGroup3D';
import { BuilderInteractionManager } from '../BuilderInteractionManager';
import { BaseBuilderBotDragOperation } from './BaseBuilderBotDragOperation';
import { Simulation3D } from '../../../shared/scene/Simulation3D';
import { VRController3D } from '../../../shared/scene/vr/VRController3D';
import { BuilderModDragOperation } from './BuilderModDragOperation';
/**
* Bot Drag Operation handles dragging of bots for mouse and touch input.
*/
export class BuilderBotDragOperation extends BaseBuilderBotDragOperation {
// This overrides the base class BaseInteractionManager
protected _interaction: BuilderInteractionManager;
private _workspace: BuilderGroup3D;
private _workspaceDelta: Vector3;
/**
* Create a new drag rules.
*/
constructor(
simulation3D: Simulation3D,
interaction: BuilderInteractionManager,
hit: Intersection,
bots: Bot[],
workspace: BuilderGroup3D,
context: string,
vrController: VRController3D | null,
fromCoord: Vector2,
skipOnDragEvents: boolean = false
) {
super(
simulation3D,
interaction,
bots,
context,
vrController,
fromCoord,
skipOnDragEvents
);
this._workspace = workspace;
if (this._workspace) {
this.game.setWorldGridVisible(true);
// calculate the delta needed to be applied to the pointer
// positions to have the pointer drag around the originally tapped point
// instead of where the anchor is.
this._workspaceDelta = new Vector3()
.copy(this._workspace.position)
.sub(hit.point);
this._workspaceDelta.setY(0);
}
}
protected _createBotDragOperation(bot: Bot) {
if (this._workspace) {
return null;
} else if (this._bots.indexOf(bot) >= 0) {
return null;
}
return new BuilderBotDragOperation(
this._simulation3D,
this._interaction,
null,
[bot],
null,
this._context,
this._vrController,
this._fromCoord,
true
);
}
protected _createModDragOperation(mod: BotTags) {
if (this._workspace) {
return null;
}
return new BuilderModDragOperation(
this._simulation3D,
this._interaction,
mod,
this._vrController
);
}
protected _disposeCore() {
if (this._workspace) {
this.game.setWorldGridVisible(false);
}
super._disposeCore();
}
protected _onDrag(calc: BotCalculationContext) {
if (this._workspace) {
if (isMinimized(calc, this._workspace.bot)) {
this._onDragWorkspace(calc);
}
} else {
super._onDrag(calc);
}
}
protected _onDragWorkspace(calc: BotCalculationContext) {
const mouseDir = Physics.screenPosToRay(
this.game.getInput().getMouseScreenPos(),
this.game.getMainCameraRig().mainCamera
);
const point = Physics.pointOnPlane(mouseDir, Physics.GroundPlane);
if (point) {
// move the center of the workspace to the point
let final = new Vector3().copy(point);
this.simulation.helper.updateBot(this._workspace.bot, {
tags: {
[`aux.context.x`]: final.x,
[`aux.context.y`]: final.z,
},
});
}
}
}