-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathPlacementState.cs
93 lines (79 loc) · 3.02 KB
/
PlacementState.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static Unity.VisualScripting.Member;
public class PlacementState : IBuildingState
{
private int selectedObjectIndex = -1;
int ID;
Grid grid;
PreviewSystem previewSystem;
ObjectsDatabaseSO database;
GridData floorData;
GridData furnitureData;
ObjectPlacer objectPlacer;
SoundFeedback soundFeedback;
public PlacementState(int iD,
Grid grid,
PreviewSystem previewSystem,
ObjectsDatabaseSO database,
GridData floorData,
GridData furnitureData,
ObjectPlacer objectPlacer,
SoundFeedback soundFeedback)
{
ID = iD;
this.grid = grid;
this.previewSystem = previewSystem;
this.database = database;
this.floorData = floorData;
this.furnitureData = furnitureData;
this.objectPlacer = objectPlacer;
this.soundFeedback = soundFeedback;
selectedObjectIndex = database.objectsData.FindIndex(data => data.ID == ID);
if (selectedObjectIndex > -1)
{
previewSystem.StartShowingPlacementPreview(
database.objectsData[selectedObjectIndex].Prefab,
database.objectsData[selectedObjectIndex].Size);
}
else
throw new System.Exception($"No object with ID {iD}");
}
public void EndState()
{
previewSystem.StopShowingPreview();
}
public void OnAction(Vector3Int gridPosition)
{
bool placementValidity = CheckPlacementValidity(gridPosition, selectedObjectIndex);
if (placementValidity == false)
{
soundFeedback.PlaySound(SoundType.wrongPlacement);
return;
}
soundFeedback.PlaySound(SoundType.Place);
int index = objectPlacer.PlaceObject(database.objectsData[selectedObjectIndex].Prefab,
grid.CellToWorld(gridPosition));
GridData selectedData = database.objectsData[selectedObjectIndex].ID == 0 ?
floorData :
furnitureData;
selectedData.AddObjectAt(gridPosition,
database.objectsData[selectedObjectIndex].Size,
database.objectsData[selectedObjectIndex].ID,
index);
previewSystem.UpdatePosition(grid.CellToWorld(gridPosition), false);
}
private bool CheckPlacementValidity(Vector3Int gridPosition, int selectedObjectIndex)
{
GridData selectedData = database.objectsData[selectedObjectIndex].ID == 0 ?
floorData :
furnitureData;
return selectedData.CanPlaceObejctAt(gridPosition, database.objectsData[selectedObjectIndex].Size);
}
public void UpdateState(Vector3Int gridPosition)
{
bool placementValidity = CheckPlacementValidity(gridPosition, selectedObjectIndex);
previewSystem.UpdatePosition(grid.CellToWorld(gridPosition), placementValidity);
}
}