-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMouseController.cs
More file actions
166 lines (145 loc) · 5.33 KB
/
MouseController.cs
File metadata and controls
166 lines (145 loc) · 5.33 KB
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
using UnityEngine;
using System;
using UnityEngine.EventSystems;
using System.Collections;
public class MouseController : MonoBehaviour {
public static MouseController Instance { get; protected set; }
public GhostScript ghost;
public TileMarkerScript marker;
Vector2 currTileUnderMouse;
Vector2 lastTileUnderMouse;
bool buildHouseMode = false;
bool buildTileMode = false;
bool deleteJob = false;
private int groundLM;
private int selectableLM;
private RaycastHit hit;
Action<Vector2> TileChanged;
// Use this for initialization
void Start () {
if (Instance == null) {
Instance = this;
}
else {
Debug.LogError("Only one instance of MouseControl can be created");
}
currTileUnderMouse = new Vector2(-1, -1);
lastTileUnderMouse = currTileUnderMouse;
groundLM = LayerMask.GetMask("Ground");
selectableLM = LayerMask.GetMask("Selectable");
}
// Update is called once per frame
void Update () {
// All click management for ghost
if (buildHouseMode)
{
if (Input.GetMouseButtonUp(2))
ghost.Rotate();
if (Input.GetMouseButtonUp(0) && ghost.CanBuild())
{
ghost.AddJob();
// On default we use road after building a house
BuildMode.Instance.OnTileButtonClick(0);
}
if (Input.GetMouseButtonUp(1))
{
// Go out from build mode
BuildMode.Instance.OnBuildButtonClick();
}
}
// All clicks for tiles
if (buildTileMode)
{
// Here we decide what we want to do, add jobs or remove them.
if (Input.GetMouseButtonDown(0) && currTileUnderMouse.x >= 0)
{
BuildJob job = BuildJobController.Instance.GetJob(currTileUnderMouse);
deleteJob = job != null &&
job.GetType().Equals(typeof(TileBuildJob)) &&
job.type == "Road";
}
if (Input.GetMouseButton(0) && currTileUnderMouse.x >= 0)
{
if (!deleteJob)
{ // Add job
if (marker.CanBuild())
{
BuildJobController.Instance.AddTileJob(currTileUnderMouse, "Road");
}
}
else // Remove job
{
BuildJob job = BuildJobController.Instance.GetJob(currTileUnderMouse);
if (job != null &&
job.GetType().Equals(typeof(TileBuildJob)) &&
job.type == "Road")
{
BuildJobController.Instance.GetJob(currTileUnderMouse).OnCancell();
}
}
}
else
{
if (Input.GetMouseButtonUp(1))
{
BuildMode.Instance.OnBuildButtonClick();
}
}
}
// All modes are off. Selecting objects and orders.
if (!buildHouseMode && !buildTileMode)
{
if (Input.GetMouseButtonUp(0))
{
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity, selectableLM) &&
!EventSystem.current.IsPointerOverGameObject())
{
//Debug.Log("hit: "+ hit.transform.name);
SelectedPanel.Instance.ChangeSelection(hit.transform.GetComponent<ISelectable>());
//Selecting.Instance.ChangeSelection(hit.transform.GetComponent<ISelectable>());
}
}
}
}
void FixedUpdate() {
if (TileChanged != null) {
lastTileUnderMouse = currTileUnderMouse;
currTileUnderMouse = TileUnderMouse();
//Debug.Log("currTileUnderMouse.x" + currTileUnderMouse.x);
if (currTileUnderMouse != lastTileUnderMouse )
{
TileChanged(currTileUnderMouse);
}
}
}
// Determine coordinates of tile, pointed by mouse.
public Vector2 TileUnderMouse()
{
Vector2 tileUnderMouse = new Vector2(-1, -1);
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity, groundLM) && !EventSystem.current.IsPointerOverGameObject())
{
tileUnderMouse.x = (int)(hit.point.x / MapController.TILE_SIZE);
tileUnderMouse.y = (int)(hit.point.z / MapController.TILE_SIZE);
}
//Debug.Log("x" + tileUnderMouse.x + " y=" + tileUnderMouse.y);
return tileUnderMouse;
}
public void RegisterTileChanged(Action<Vector2> callBack) {
TileChanged += callBack;
}
public void UnregisterTileChanged(Action<Vector2> callBack) {
TileChanged -= callBack;
}
public void TurnOnHouseMode() {
buildHouseMode = true;
buildTileMode = false;
}
public void TurnOnTileMode() {
buildHouseMode = false;
buildTileMode = true;
}
public void TurnOffModes() {
buildHouseMode = false;
buildTileMode = false;
}
}