-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocation.cs
166 lines (151 loc) · 3.83 KB
/
Location.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
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
using System;
using System.Xml;
using NLua;
namespace Game
{
/// <summary>
/// Location is a square in a map. Contains coordinates X, Y, indicator, whether is visible, and block, which stores object inheriting interface IPlace.
/// </summary>
public class Location :IPlace
{
public int X {get; set;}
public int Y {get; set;}
public IPlace Block {get; set;}
public bool Visible {get; set;}
public char symbol;
// script, if available. default is null
public string Script { get; set;}
public Location (int x, int y, IPlace o)
{
X = x;
Y = y;
Block = o;
Visible = false;
Script = null;
symbol = Block.Symbol();
}
public void SetScript(string scriptName)
{
this.Script = scriptName;
}
/// <summary>
/// Pass to the symbol.
/// </summary>
public char Symbol()
{
return symbol;
}
public virtual void SetSymbol(string x)
{
this.Block.SetSymbol(x);
UpdateSymbol();
}
public void UpdateSymbol()
{
this.symbol = this.Block.Symbol();
}
public bool CanMoveTo()
{
return this.Block.CanMoveTo();
}
public virtual bool CanDropItemOnto()
{
return this.Block.CanDropItemOnto();
}
public virtual void DropItemOnLocation(Item i)
{
this.Block = this.Block.DropItemOnto(i);
}
public virtual IPlace DropItemOnto(Item i)
{
return this;
}
/// <summary>
/// Passes method to block.
/// </summary>
/// <returns>
/// The action.
/// </returns>
/// <param name='p'>
/// If set to <c>true</c> p.
/// </param>
/// <param name='l'>
/// If set to <c>true</c> l.
/// </param>
/// <param name='l2'>
/// If set to <c>true</c> l2.
/// </param>
public IPlace AutomaticAction(Player p)
{
if (Script == null)
this.Block = Block.AutomaticAction(p);
else
{
ThisGame.lua["map"] = ThisGame.dungeon;
ThisGame.lua["block"] = Block;
ThisGame.lua.DoFile(ThisGame.filePath + "luascripts/dungeons/" + ThisGame.player.currentDungeon + "/" + Script);
Block = (IPlace)ThisGame.lua["out"];
bool keepScript = (bool)ThisGame.lua["keepscript"];
string message = (string)ThisGame.lua["message"];
if (message != "null")
ThisGame.messageLog.Enqueue(message);
if (!keepScript)
{
if ((string)ThisGame.lua["newscript"] == "null")
Script = null;
else
Script = (string)ThisGame.lua["newscript"];
}
UpdateSymbol();
ThisGame.lua["block"] = null;
ThisGame.lua["out"] = null;
ThisGame.lua["keepscript"] = null;
ThisGame.lua["newscript"] = null;
ThisGame.lua["message"] = null;
}
return this;
}
public void VoluntaryAction(Player p)
{
Block.VoluntaryAction(p);
if (Block is Chest)
if (((Chest)Block).CanBeRemoved())
if (((Chest)Block).IsEmpty())
Block = new BasicObject();
this.UpdateSymbol();
}
/// <summary>
/// Writes location to Xml.
/// </summary>
/// <returns>
/// The xml.
/// </returns>
/// <param name='doc'>
/// Document.
/// </param>
/// <param name='elementName'>
/// Element name.
/// </param>
public virtual XmlElement ToXml(XmlDocument doc, string elementName)
{
XmlElement loc = doc.CreateElement(elementName);
// attributes of location
loc.SetAttribute("x", X.ToString());
loc.SetAttribute("y", Y.ToString());
//loc.SetAttribute("symbol", Symbol().ToString());
loc.SetAttribute("visible", Visible.ToString());
if (Script != null)
loc.SetAttribute("script", Script);
// create block with the name of the inner class
XmlElement block = doc.CreateElement("block");
string type = Block.GetType().ToString();
block.SetAttribute("type", type);
// append content of block - pass to inner object
XmlElement innerObject = Block.ToXml(doc, type.Split('.')[1]);
block.AppendChild(innerObject);
// append block
loc.AppendChild(block);
return loc;
}
}
}