-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerControls.cs
55 lines (49 loc) · 1.99 KB
/
PlayerControls.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
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Assertions;
namespace StrategicGame.Client {
public class PlayerControls : MonoBehaviour {
const int LMB = 0;
const int RMB = 1;
public UnitSelector UnitSelector;
public WorldRaycaster WorldRaycaster;
Vector2 _mouseDownPosition;
bool _mousePressed;
UnitSelector.IFrameSelecting _frameSelecting;
public event Action<IEnumerable<Unit>, Vector3> OnSendUnits;
void Awake() {
Assert.IsNotNull(UnitSelector);
Assert.IsNotNull(WorldRaycaster);
}
void Update() {
var mousePosition = (Vector2)Input.mousePosition;
if (Input.GetMouseButtonDown(LMB)) {
_mouseDownPosition = mousePosition;
_mousePressed = true;
} else if (Input.GetMouseButtonUp(LMB)) {
if (_frameSelecting != null) {
_frameSelecting.End();
_frameSelecting = null;
} else
UnitSelector.SelectOne(mousePosition);
_mousePressed = false;
} else if (Input.GetMouseButtonDown(RMB)) {
var selection = UnitSelector.CurrentSelection;
if (selection.Any()) {
var units = selection.Select(s => s.GetComponent<Unit>()).Where(s => s != null);
if (units.Any()) {
var groundPoint = WorldRaycaster.RaycastGround(mousePosition);
OnSendUnits.InvokeSafe(units, groundPoint);
}
}
} else if (_mousePressed) {
if (_frameSelecting != null)
_frameSelecting.Update(mousePosition);
else if ((mousePosition - _mouseDownPosition).magnitude > 5)
_frameSelecting = UnitSelector.BeginFrameSelection(mousePosition);
}
}
}
}