-
Notifications
You must be signed in to change notification settings - Fork 21
/
PlacementSystem.cs
120 lines (99 loc) · 3.39 KB
/
PlacementSystem.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
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class PlacementSystem : MonoBehaviour
{
[SerializeField]
private InputManager inputManager;
[SerializeField]
private Grid grid;
[SerializeField]
private ObjectsDatabaseSO database;
[SerializeField]
private GameObject gridVisualization;
[SerializeField]
private AudioClip correctPlacementClip, wrongPlacementClip;
[SerializeField]
private AudioSource source;
private GridData floorData, furnitureData;
[SerializeField]
private PreviewSystem preview;
private Vector3Int lastDetectedPosition = Vector3Int.zero;
[SerializeField]
private ObjectPlacer objectPlacer;
IBuildingState buildingState;
[SerializeField]
private SoundFeedback soundFeedback;
private void Start()
{
gridVisualization.SetActive(false);
floorData = new();
furnitureData = new();
}
public void StartPlacement(int ID)
{
StopPlacement();
gridVisualization.SetActive(true);
buildingState = new PlacementState(ID,
grid,
preview,
database,
floorData,
furnitureData,
objectPlacer,
soundFeedback);
inputManager.OnClicked += PlaceStructure;
inputManager.OnExit += StopPlacement;
}
public void StartRemoving()
{
StopPlacement();
gridVisualization.SetActive(true) ;
buildingState = new RemovingState(grid, preview, floorData, furnitureData, objectPlacer, soundFeedback);
inputManager.OnClicked += PlaceStructure;
inputManager.OnExit += StopPlacement;
}
private void PlaceStructure()
{
if(inputManager.IsPointerOverUI())
{
return;
}
Vector3 mousePosition = inputManager.GetSelectedMapPosition();
Vector3Int gridPosition = grid.WorldToCell(mousePosition);
buildingState.OnAction(gridPosition);
}
//private bool CheckPlacementValidity(Vector3Int gridPosition, int selectedObjectIndex)
//{
// GridData selectedData = database.objectsData[selectedObjectIndex].ID == 0 ?
// floorData :
// furnitureData;
// return selectedData.CanPlaceObejctAt(gridPosition, database.objectsData[selectedObjectIndex].Size);
//}
private void StopPlacement()
{
soundFeedback.PlaySound(SoundType.Click);
if (buildingState == null)
return;
gridVisualization.SetActive(false);
buildingState.EndState();
inputManager.OnClicked -= PlaceStructure;
inputManager.OnExit -= StopPlacement;
lastDetectedPosition = Vector3Int.zero;
buildingState = null;
}
private void Update()
{
if (buildingState == null)
return;
Vector3 mousePosition = inputManager.GetSelectedMapPosition();
Vector3Int gridPosition = grid.WorldToCell(mousePosition);
if(lastDetectedPosition != gridPosition)
{
buildingState.UpdateState(gridPosition);
lastDetectedPosition = gridPosition;
}
}
}