-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathInputHandler.cs
More file actions
108 lines (94 loc) · 2.63 KB
/
InputHandler.cs
File metadata and controls
108 lines (94 loc) · 2.63 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
using UnityEngine;
using System.Collections.Generic;
public class InputHandler : MonoBehaviour
{
public float moveDistance = 10f;
public GameObject objectToMove;
private MoveCommandReceiver moveCommandReciever;
private List<MoveCommand> commands = new List<MoveCommand>();
private int currentCommandNum = 0;
void Start()
{
moveCommandReciever = new MoveCommandReceiver();
if (objectToMove == null)
{
Debug.LogError("objectToMove must be assigned via inspector");
this.enabled = false;
}
}
public void Undo()
{
if (currentCommandNum > 0)
{
currentCommandNum--;
MoveCommand moveCommand = commands[currentCommandNum];
moveCommand.UnExecute();
}
}
public void Redo()
{
if (currentCommandNum < commands.Count)
{
MoveCommand moveCommand = commands[currentCommandNum];
currentCommandNum++;
moveCommand.Execute();
}
}
private void Move(MoveDirection direction)
{
MoveCommand moveCommand = new MoveCommand(moveCommandReciever, direction, moveDistance, objectToMove);
moveCommand.Execute();
commands.Add(moveCommand);
currentCommandNum++;
}
//Simple move commands to attach to UI buttons
public void MoveUp() { Move(MoveDirection.up); }
public void MoveDown() { Move(MoveDirection.down); }
public void MoveLeft() { Move(MoveDirection.left); }
public void MoveRight() { Move(MoveDirection.right); }
//Shows what's going on in the command list
void OnGUI()
{
string label = " start";
if (currentCommandNum == 0)
{
label = ">" + label;
}
label += "\n";
for (int i = 0; i < commands.Count; i++)
{
if (i == currentCommandNum - 1)
label += "> " + commands[i].ToString() + "\n";
else
label += " " + commands[i].ToString() + "\n";
}
GUI.Label(new Rect(0, 0, 400, 800), label);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
MoveUp();
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
MoveDown();
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
MoveLeft();
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
MoveRight();
}
if (Input.GetKeyDown(KeyCode.R))
{
Redo();
}
if (Input.GetKeyDown(KeyCode.U))
{
Undo();
}
}
}