-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI.cs
More file actions
283 lines (240 loc) · 9.46 KB
/
API.cs
File metadata and controls
283 lines (240 loc) · 9.46 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using System;
using System.IO;
using MoonSharp.Interpreter;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace Dungeontest
{
public class API
{
/// <summary>
/// Claims a location in the game's sprite list
/// </summary>
/// <param name="name">Name of the sprite</param>
/// <param name="src">File location</param>
/// <returns>The sprite's ID</returns>
public static int ClaimID(string name, string src)
{
int id = CoreGame.sprites.Count;
Console.WriteLine("\n[dungeontest] creating entity '{0}' under id '{1}'\n", name, id);
try
{
//add name to sprite list
CoreGame.spriteNames.Add(name);
//add sprite to sprite list
CoreGame.sprites.Add(new TextureData(src));
Console.WriteLine("\n[dungeontest] created entity '{0}' under id '{1}'\n", name, id);
}
catch (Exception ex)
{
Console.WriteLine("\n[dungeontest] error occured when spawning entity '{0}' under id '{1}'. error: {2}\n", name, id, ex.Message);
}
return id;
}
public static void LoadSound(string name, string src)
{
Console.WriteLine("\n[dungeontest] creating sound '{0}'\n", name);
try
{
Sounds soundEffect = new Sounds(src);
// add name to sound list
CoreGame.soundNames.Add(name);
CoreGame.soundEffects.Add(soundEffect);
Console.WriteLine("\n[dungeontest] created sound '{0}'\n", name);
}
catch (Exception ex)
{
Console.WriteLine("\n[dungeontest] error occured when creating sound '{0}' from file '{1}'. error: {2}\n", name, src, ex.Message);
}
}
public static void PlaySound(string name, float volume, float pan)
{
int soundIndex = CoreGame.soundNames.IndexOf(name);
Console.WriteLine(soundIndex);
if (soundIndex >= 0)
{
Console.WriteLine("\n[dungeontest] playing sound '{0}'\n", name);
Sounds soundEffect = CoreGame.soundEffects[soundIndex];
soundEffect.Play(volume, pan);
}
else
Console.WriteLine("\n[dungeontest] sound \'{0}\' does not exist", name);
}
/// <summary>
/// Spawns an an entity at (x, y)
/// </summary>
/// <param name="id">The ID of the sprite to use</param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public static void SpawnEntity(int id, float x, float y)
{
Entity myEntity = new Entity(id, x, y);
Console.WriteLine("\n[dungeontest] spawning entity '{0}'\n", id);
try
{
Dungeon.entities.Add(myEntity);
Console.WriteLine("\n[dungeontest] entity '{0}' spawned\n", id);
}
catch (Exception ex)
{
Console.WriteLine("\n[dungeontest] error occured when spawning '{0}'. error: {1}\n", id, ex.Message);
}
}
/// <summary>
/// Checks if the block is a wall
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public static bool IsBlockSolid(int x, int y)
{
return Dungeon.IsBlocking(x, y);
}
/// <summary>
/// Returns the block at (x, y)
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public static object GetBlock(int x, int y)
{
return Dungeon.GetBlockAt(x, y);
}
/// <summary>
/// Sets the block at (x, y) to specified ID
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="id">The ID of the sprite to use</param>
/// <param name="solid">Making the block a wall or floor</param>
/// <returns></returns>
public static void SetBlock(int x, int y, byte id, bool solid)
{
Dungeon.SetBlockAt(x, y, new Block(id, solid));
}
/// <summary>
/// Gets the closest player to this entity
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public static Entity GetNearestPlayer(Entity entity)
{
float closestDistance = float.MaxValue;
Entity closestPlayer = CoreGame.players[0];
foreach (Entity player in CoreGame.players)
{
float distance = Vector2.Distance(player.pos, entity.pos);
//if this is closer than the closest found so far then this is the closest player
if (distance < closestDistance)
{
//set the closest distance
closestDistance = distance;
//set the closest player
closestPlayer = player;
}
}
return closestPlayer;
}
/// <summary>
/// Changes the name of the task in the loading screen
/// </summary>
/// <param name="name"></param>
public static void ChangeTask(string name)
{
Dungeon.task = name;
}
public static void SetDungeonSize(int width, int height)
{
}
}
public class ModHandler
{
const string MODS_FOLDER = "mods/";
public static List<Script> mods = new List<Script>();
/// <summary>
/// Load the mods in the mods folder
/// </summary>
public static void LoadMods()
{
Console.WriteLine("\n[dungeontest] loading mods\n");
// Set script loader
//Script.DefaultOptions.ScriptLoader = new EmbeddedResourcesScriptLoader();
// Register classes
UserData.RegisterType<API>();
UserData.RegisterType<Input>();
UserData.RegisterType<Block>();
UserData.RegisterType<Entity>();
UserData.RegisterType<Keys>();
UserData.RegisterType<Vector2>();
// Load static classes
DynValue api = UserData.Create(new API());
DynValue input = UserData.Create(new Input());
DynValue keys = UserData.Create(new Keys());
// Script Loader Base
//((ScriptLoaderBase)script.Options.ScriptLoader).ModulePaths = new string[] { "mods/?", "mods/?.lua" };
//((ScriptLoaderBase)script.Options.ScriptLoader).IgnoreLuaPathGlobal = true;
foreach (string file in Directory.EnumerateFiles(MODS_FOLDER, "*.lua"))
{
Console.Write("\n[dungeontest] initializing mod '{0}'\n", file);
try
{
Script script = new Script();
script.Globals.Set("API", api);
script.Globals.Set("Input", input);
script.Globals.Set("Keys", keys);
// Load the file
script.DoFile(file);
mods.Add(script);
// Log completion
Console.WriteLine("\n[dungeontest] mod '{0}' loaded!\n", file);
}
catch (ScriptRuntimeException ex)
{
// Alerting the issue loading a mod
Console.WriteLine("\n[dungeontest] error occured in loading '{0}' mod: {1}\n", file, ex.Message);
Console.WriteLine("\n[dungeontest] could not load '{0}' mod!\n", file);
}
// Print how many mods were loaded
Console.WriteLine("\n[dungeontest] {0} mods have been loaded\n", mods.Count);
}
}
// API Error Handling (this does nothing currently)
private static void DoError()
{
throw new ScriptRuntimeException("\n[dungeontest] fatal error occured!\n");
}
// implemented events:
//
// PreGenerate() - Runs before dungeon is generated
// PostGenerate() - Runs after dungeon is generated
// ServerUpdate(float deltaTime) - Runs every time the server updates
// EntityUpdate(Entity entity) - Runs when the entity needs an update
/// <summary>
/// Runs event handler on every mod
/// </summary>
/// <param name="eventName">The event to handle</param>
/// <param name="args">Parameters for the event</param>
public static void HandleEvent(string eventName, params object[] args)
{
try
{
// Run the handler for the event on every mod
foreach (Script mod in mods)
{
// Get the method
object method = mod.Globals[eventName];
// See if it exists then call it
if (method != null)
mod.Call(method, args);
}
}
catch (ScriptRuntimeException e)
{
// Error warning
Console.WriteLine("\n[dungeontest] \"" + eventName + "\" event error: " + e.DecoratedMessage + "\n");
}
}
}
}