This repository has been archived by the owner on May 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuilder.cs
196 lines (153 loc) · 6.99 KB
/
Builder.cs
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using Cube.Gameplay;
using Cube.Networking;
using System;
using UnityEngine;
using UnityEngine.Assertions;
namespace Cube.ModularBuildings
{
[AddComponentMenu("Cube.ModularBuildings/Builder")]
public class Builder : EquippableItem
{
const float PART_SNAP_DISTANCE = 0.75f;
[Serializable]
struct KeyPartBinding {
public KeyCode key;
public BuildingPartType partType;
}
public Material blueprintMaterial = null;
public Material occupiedBlueprintMaterial = null;
[SerializeField]
BuildingPartType _currentPartType;
GameObject _blueprint;
Building _currentBuildingTheBlueprintIsSnappedTo;
bool _currentBuildingSlotOccupied;
BuildingSlot _currentBuildingClosestSlot;
Vector3 _currentBuildingBuildPosition;
Quaternion _currentBuildingBuildRotation;
[SerializeField]
KeyPartBinding[] _bindings;
BuilderItemTypeExtension _builderTypeExtension;
Pawn _pawn;
float _nextShotTime;
public override void Equip(ItemType itemType, Replica owner) {
_builderTypeExtension = itemType.GetExtension<BuilderItemTypeExtension>();
Assert.IsNotNull(_builderTypeExtension);
_pawn = GetComponentInParent<Pawn>();
RebuildBlueprint();
}
public override void Use() {
if (!isClient)
return;
if (Time.time < _nextShotTime)
return;
_nextShotTime = Time.time + 0.2f;
if (_currentBuildingTheBlueprintIsSnappedTo == null && _currentPartType.canCreateNewBuilding)
BuildNew();
else if (_currentBuildingClosestSlot != null && !_currentBuildingSlotOccupied)
Build();
}
void BuildNew() {
RpcBuildNew(_currentBuildingBuildPosition, _currentBuildingBuildRotation, _currentPartType);
}
void Build() {
var buildingReplica = _currentBuildingTheBlueprintIsSnappedTo.GetComponent<Replica>();
RpcBuild(buildingReplica, _currentPartType, _currentBuildingClosestSlot.type, _currentBuildingBuildPosition);
_currentBuildingTheBlueprintIsSnappedTo.AddPart(_currentPartType, _currentBuildingClosestSlot);
_currentBuildingTheBlueprintIsSnappedTo.Rebuild();
}
[ReplicaRpc(RpcTarget.Server)]
void RpcBuildNew(Vector3 position, Quaternion rotation, BuildingPartType partType)
{
#if SERVER
var buildingManager = SystemProvider.GetSystem<IBuildingSystem>(gameObject);
var newBuilding = buildingManager.CreateBuilding(_builderTypeExtension.buildingType, position, rotation);
newBuilding.AddPart(partType, null);
newBuilding.Rebuild();
#endif
}
[ReplicaRpc(RpcTarget.Server)]
void RpcBuild(Replica buildingReplica, BuildingPartType partType, BuildingSlotType buildingSlotType, Vector3 slotPosition) {
var building = buildingReplica.GetComponent<Building>();
float distance = 0;
var slot = building.GetClosestSlot(slotPosition, buildingSlotType, true, out distance);
//#TODO check distance
Assert.IsNotNull(slot);
building.AddPart(partType, slot);
building.Rebuild();
}
void Update() {
UpdatePartType();
UpdateBlueprint();
}
void OnDestroy() {
DestroyBlueprint();
}
void UpdatePartType() {
foreach (var binding in _bindings) {
if (Input.GetKeyDown(binding.key)) {
_currentPartType = binding.partType;
RebuildBlueprint();
}
}
}
void RebuildBlueprint() {
DestroyBlueprint();
if (!isClient || _currentPartType == null || _pawn.controller == null)
return;
var prefab = _builderTypeExtension.buildingType.GetPrefabForPartType(_currentPartType);
_blueprint = Instantiate(prefab);
_blueprint.GetComponent<Renderer>().sharedMaterial = blueprintMaterial;
foreach (var collider in _blueprint.GetComponents<Collider>())
{
collider.enabled = false;
}
}
void DestroyBlueprint() {
if (_blueprint == null)
return;
Destroy(_blueprint);
_blueprint = null;
}
void UpdateBlueprint() {
if (_blueprint == null)
return;
var buildingManager = gameObject.GetSystem<IBuildingSystem>();
var buildPosition = Camera.main.transform.position + Camera.main.transform.rotation * Vector3.forward * 3f;
var buildRotation = Quaternion.AngleAxis(Camera.main.transform.rotation.eulerAngles.y, Vector3.up);
DebugDraw.DrawMarker(buildPosition, 0.25f, Color.blue);
_blueprint.transform.position = buildPosition;
_blueprint.transform.rotation = buildRotation;
BuildingSlot closestSlot = null;
var occupied = false;
var building = buildingManager.GetBuildingInRange(buildPosition, PART_SNAP_DISTANCE * 2f);
if (building != null) {
float closestDistance = float.MaxValue;
var sockets = _blueprint.GetComponentsInChildren<BuildingSocket>();
foreach (var socket in sockets) {
float distance;
var slot = building.GetClosestSlot(socket.transform.position, socket.slotType, true, out distance);
if (slot == null)
continue;
if (distance <= PART_SNAP_DISTANCE && distance < closestDistance) {
closestDistance = distance;
closestSlot = slot;
}
}
//Debug.Log(closestSlot);
if (closestSlot != null) {
buildPosition = closestSlot.transform.position;
buildRotation = closestSlot.transform.rotation;
occupied = !building.IsSlotFree(closestSlot);
}
}
_blueprint.transform.position = buildPosition + Vector3.up * 0.025f;
_blueprint.transform.rotation = buildRotation;
_blueprint.GetComponent<Renderer>().sharedMaterial = !occupied ? blueprintMaterial : occupiedBlueprintMaterial;
_currentBuildingTheBlueprintIsSnappedTo = building;
_currentBuildingSlotOccupied = occupied;
_currentBuildingClosestSlot = closestSlot;
_currentBuildingBuildPosition = buildPosition;
_currentBuildingBuildRotation = buildRotation;
}
}
}