-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cs
More file actions
112 lines (100 loc) · 3.65 KB
/
Parser.cs
File metadata and controls
112 lines (100 loc) · 3.65 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace World1
{
public class Parser
{
List<string> commands = new List<string>();
GameObject targetObject = null;
public Parser() {
commands.Add("look");
commands.Add("get");
commands.Add("attack");
commands.Add("inventory");
commands.Add("block");
commands.Add("drop");
commands.Add("stats");
commands.Add("showdb");
commands.Add("sheet");
commands.Add("quit");
commands.Add("q");
commands.Add("go");
commands.Add("teleport");
}
public string execute(Player player, Room room, String playerInput)
{
bool hasFoundCommand = false;
bool hasFoundObject = false;
targetObject = null;
string strippedInput;
string playerAction = "";
strippedInput = playerInput;
foreach (String c in commands)
{
if (playerInput.StartsWith(c, StringComparison.CurrentCultureIgnoreCase))
{
hasFoundCommand = true;
if (c.Equals("q")) {
playerAction = "quit";
} else {
playerAction = c;
}
if (playerInput.Length > c.Length)
{
strippedInput = playerInput.Substring(c.Length + 1);
}
break;
}
}
if (!hasFoundCommand || playerAction.Equals("go"))
{
Exit door = room.getExits().Find(
delegate(Exit e)
{
return e.getIdentifier().Equals(strippedInput, StringComparison.CurrentCultureIgnoreCase);
}
);
if (door != null)
{
targetObject = door;
hasFoundCommand = true;
hasFoundObject = true;
playerAction = "go";
}
}
if (!hasFoundObject)
{
GameObject localObject = DBHandler.Instance.GameDB.Find(
delegate(GameObject g)
{
return ((g.getLocation() == player.getLocation()) &&
(g.getIdentifier().Equals(strippedInput, StringComparison.CurrentCultureIgnoreCase)));
}
);
if (localObject != null) {
targetObject = localObject;
hasFoundObject = true;
}
}
if (!hasFoundObject) {
if (strippedInput.Equals("here", StringComparison.CurrentCultureIgnoreCase) || strippedInput.Equals("room", StringComparison.CurrentCultureIgnoreCase))
{
targetObject = room;
hasFoundObject = true;
}
else if (strippedInput.Equals("self", StringComparison.CurrentCultureIgnoreCase) || strippedInput.Equals("me", StringComparison.CurrentCultureIgnoreCase))
{
targetObject = player;
hasFoundObject = true;
}
}
return playerAction;
}
public GameObject getTarget()
{
return targetObject;
}
}
}