-
Notifications
You must be signed in to change notification settings - Fork 37
/
DynamicDungeon.cs
270 lines (240 loc) · 12.7 KB
/
DynamicDungeon.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
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
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StardewValley;
using StardewValley.Menus;
using StardewValley.Monsters;
using StardewValley.TerrainFeatures;
using xTile.Dimensions;
using xTile.ObjectModel;
using SObject = StardewValley.Object;
using XnaRectangle = Microsoft.Xna.Framework.Rectangle;
using xTileRectangle = xTile.Dimensions.Rectangle;
namespace Entoarox.DynamicDungeons
{
internal class DynamicDungeon : GameLocation
{
/*********
** Properties
*********/
private double _Difficulty;
private readonly Texture2D Minimap;
private static readonly Random Random = new Random();
private readonly int Seed;
#pragma warning disable IDE0044 // Add readonly modifier
#pragma warning disable CS0649
private int Time;
#pragma warning restore IDE0044 // Add readonly modifier
#pragma warning restore CS0649
/*********
** Accessors
*********/
public int Floor;
public Point EntryPoint;
public bool DrawInfo = false;
public double Difficulty
{
get => this._Difficulty;
set => this._Difficulty = Math.Max(0, Math.Min(10, value));
}
public List<ResourceClump> ResourceClumps = new List<ResourceClump>();
/*********
** Public methods
*********/
public DynamicDungeon(double difficulty = 0, int? seed = null)
{
#pragma warning disable AvoidNetField // Avoid Netcode types when possible
this.name.Value = "DynamicDungeon";
#pragma warning restore AvoidNetField // Avoid Netcode types when possible
this.Seed = seed ?? DynamicDungeon.Random.Next();
this.Difficulty = difficulty;
DungeonBuilder builder = new DungeonBuilder(difficulty, 1);
this.Floor = 1;
this.Minimap = builder.GetMiniMap();
this.map = builder.GetMap();
this.EntryPoint = builder.GetFloorPoint();
this.forceViewportPlayerFollow = true;
// Embed StardewValley.GameLocation.ctor()
this.waterTiles = new bool[this.map.Layers[0].LayerWidth, this.map.Layers[0].LayerHeight];
bool flag = false;
for (int i = 0; i < this.map.Layers[0].LayerWidth; i++)
{
for (int j = 0; j < this.map.Layers[0].LayerHeight; j++)
{
if (this.doesTileHaveProperty(i, j, "Water", "Back") != null)
{
flag = true;
this.waterTiles[i, j] = true;
}
}
}
if (!flag)
this.waterTiles = null;
}
public override void drawAboveAlwaysFrontLayer(SpriteBatch b)
{
ModEntry.SMonitor.Log("DynamicDungeon#drawAboveAlwaysFrontLayer", StardewModdingAPI.LogLevel.Trace);
if (this.DrawInfo)
this.Map.GetLayer("MapInfo").Draw(Game1.mapDisplayDevice, Game1.viewport, Location.Origin, false, Game1.pixelZoom);
foreach (NPC current in this.characters)
{
if (current is Monster monster)
monster.drawAboveAllLayers(b);
}
base.drawAboveAlwaysFrontLayer(b);
// Floor
string floor = this.Floor.ToString();
Vector2 size = Game1.smallFont.MeasureString(floor);
size.X += 26;
if (size.X > 150)
{
floor = "?????";
size = Game1.smallFont.MeasureString(floor);
size.X += 26;
}
IClickableMenu.drawTextureBox(Game1.spriteBatch, 158, 48, (int)Math.Ceiling(size.X), 52, Color.White);
Utility.drawTextWithShadow(Game1.spriteBatch, floor, Game1.smallFont, new Vector2(158 + 12, 48 + 12), Game1.textColor);
// Difficulty
IClickableMenu.drawTextureBox(Game1.spriteBatch, 158, 4, 125, 40, Color.White);
if (this.Difficulty >= 1)
Game1.spriteBatch.Draw(Game1.staminaRect, new XnaRectangle(158 + 13, 17, 9, 14), Color.Green);
if (this.Difficulty >= 2)
Game1.spriteBatch.Draw(Game1.staminaRect, new XnaRectangle(158 + 13 + 10, 17, 9, 14), Color.Green);
if (this.Difficulty >= 3)
Game1.spriteBatch.Draw(Game1.staminaRect, new XnaRectangle(158 + 13 + 20, 17, 9, 14), Color.Green);
if (this.Difficulty >= 4)
Game1.spriteBatch.Draw(Game1.staminaRect, new XnaRectangle(158 + 13 + 30, 17, 9, 14), Color.Green);
if (this.Difficulty >= 5)
Game1.spriteBatch.Draw(Game1.staminaRect, new XnaRectangle(158 + 13 + 40, 17, 9, 14), Color.DarkOrange);
if (this.Difficulty >= 6)
Game1.spriteBatch.Draw(Game1.staminaRect, new XnaRectangle(158 + 13 + 50, 17, 9, 14), Color.DarkOrange);
if (this.Difficulty >= 7)
Game1.spriteBatch.Draw(Game1.staminaRect, new XnaRectangle(158 + 13 + 60, 17, 9, 14), Color.DarkOrange);
if (this.Difficulty >= 8)
Game1.spriteBatch.Draw(Game1.staminaRect, new XnaRectangle(158 + 13 + 70, 17, 9, 14), Color.Red);
if (this.Difficulty >= 9)
Game1.spriteBatch.Draw(Game1.staminaRect, new XnaRectangle(158 + 13 + 80, 17, 9, 14), Color.Red);
if (this.Difficulty == 10)
Game1.spriteBatch.Draw(Game1.staminaRect, new XnaRectangle(158 + 13 + 90, 17, 9, 14), Color.DarkRed);
// Minimap
IClickableMenu.drawTextureBox(Game1.spriteBatch, 4, 4, 150, 150, Color.White);
Game1.spriteBatch.Draw(this.Minimap, new Vector2(20, 20), Color.White);
Point p = Game1.player.getTileLocationPoint();
Game1.spriteBatch.Draw(Game1.staminaRect, new XnaRectangle(p.X + 19, p.Y + 19, 1, 1), Color.Red * 0.33f);
Game1.spriteBatch.Draw(Game1.staminaRect, new XnaRectangle(p.X + 19, p.Y + 21, 1, 1), Color.Red * 0.33f);
Game1.spriteBatch.Draw(Game1.staminaRect, new XnaRectangle(p.X + 21, p.Y + 19, 1, 1), Color.Red * 0.33f);
Game1.spriteBatch.Draw(Game1.staminaRect, new XnaRectangle(p.X + 21, p.Y + 21, 1, 1), Color.Red * 0.33f);
Game1.spriteBatch.Draw(Game1.staminaRect, new XnaRectangle(p.X + 20, p.Y + 19, 1, 1), Color.Red * 0.66f);
Game1.spriteBatch.Draw(Game1.staminaRect, new XnaRectangle(p.X + 19, p.Y + 20, 1, 1), Color.Red * 0.66f);
Game1.spriteBatch.Draw(Game1.staminaRect, new XnaRectangle(p.X + 21, p.Y + 20, 1, 1), Color.Red * 0.66f);
Game1.spriteBatch.Draw(Game1.staminaRect, new XnaRectangle(p.X + 20, p.Y + 21, 1, 1), Color.Red * 0.66f);
Game1.spriteBatch.Draw(Game1.staminaRect, new XnaRectangle(p.X + 20, p.Y + 20, 1, 1), Color.Red * 0.99f);
}
public override void checkForMusic(GameTime time)
{
ModEntry.SMonitor.Log("DynamicDungeon#checkForMusic", StardewModdingAPI.LogLevel.Trace);
if (Game1.currentSong == null || !Game1.currentSong.IsPlaying)
Game1.changeMusicTrack("Upper_Ambient");
}
public override void UpdateWhenCurrentLocation(GameTime time)
{
ModEntry.SMonitor.Log("DynamicDungeon#UpdateWhenCurrentLocation", StardewModdingAPI.LogLevel.Trace);
Game1.timeOfDay = this.Time;
foreach (ResourceClump current in this.ResourceClumps)
current.tickUpdate(time, current.tile.Value, this);
base.UpdateWhenCurrentLocation(time);
}
public override SObject getFish(float millisecondsAfterNibble, int bait, int waterDepth, Farmer who, double baitPotency, Vector2 bobberTile, string locationName = null)
{
return LootHandler.LootTables["Fishing"].GetDrop(this.Seed, (this.Difficulty + baitPotency) / (30 - waterDepth));
}
public override string checkForBuriedItem(int xLocation, int yLocation, bool explosion, bool detectOnly, Farmer who)
{
if (Game1.random.NextDouble() < 0.15)
Game1.createItemDebris(LootHandler.LootTables["Digging"].GetDrop(this.Seed, this.Difficulty / 30), new Vector2(xLocation, yLocation), 1);
return "";
}
public override void monsterDrop(Monster monster, int x, int y, Farmer who)
{
//TODO: Consider implementing custom monster loot?
base.monsterDrop(monster, x, y, who);
}
public override void draw(SpriteBatch b)
{
ModEntry.SMonitor.Log("DynamicDungeon#draw", StardewModdingAPI.LogLevel.Trace);
foreach (ResourceClump current in this.ResourceClumps)
current.draw(b, current.tile.Value);
base.draw(b);
}
public override bool isCollidingPosition(XnaRectangle position, xTileRectangle viewport, bool isFarmer, int damagesFarmer, bool glider, Character character)
{
foreach (ResourceClump current in this.ResourceClumps)
if (!glider && current.getBoundingBox(current.tile.Value).Intersects(position))
return true;
return base.isCollidingPosition(position, viewport, isFarmer, damagesFarmer, glider, character);
}
public override bool isTileOccupied(Vector2 tileLocation, string characterToIgnore = "", bool ignoreAllCharacters = false)
{
foreach (ResourceClump current in this.ResourceClumps)
if (current.occupiesTile((int)tileLocation.X, (int)tileLocation.Y))
return true;
return this.EntryPoint.Equals(new Point((int)tileLocation.X, (int)tileLocation.Y)) || base.isTileOccupied(tileLocation, characterToIgnore);
}
public new bool performAction(string propertyValue, Farmer who, Location tileLocation)
{
string[] split = propertyValue.Split(' ');
string[] args = new string[split.Length - 1];
Array.Copy(split, 1, args, 0, args.Length);
string action = string.IsInterned(split[0]) ?? split[0];
switch (action)
{
case "DDLoot": // DDLoot <string:table> <int:dropcount> [bool:deleteTile] [<float:enemySpawnChance> <string:spawnedEnemy>]
Game1.drawObjectDialogue($"TODO: Trigger the \"{args[0]}\" loot table and drop ({args[1]}) items");
return true;
case "DDShop": // DDShop <vendorID> [Currently only DwarfVendor]
Game1.drawObjectDialogue($"TODO: Open the \"{args[0]}\" shop menu here");
return true;
case "DDLooted": // DDLooted
Game1.drawObjectDialogue(ModEntry.SHelper.Translation.Get("AlreadyLooted"));
return true;
case "DDExit":
this.createQuestionDialogue(ModEntry.SHelper.Translation.Get("LeaveDungeon"), new[]
{
new Response("yes", ModEntry.SHelper.Translation.Get("LeaveDungeon_Yes")),
new Response("no", ModEntry.SHelper.Translation.Get("LeaveDungeon_No"))
}, this.ExitResolver);
return false;
default:
return false;
}
}
public override bool checkAction(Location tileLocation, xTileRectangle viewport, Farmer who)
{
Vector2 vector = new Vector2(tileLocation.X, tileLocation.Y);
xTile.Tiles.Tile tile = this.map.GetLayer("Buildings").PickTile(new Location(tileLocation.X * Game1.tileSize, tileLocation.Y * Game1.tileSize), viewport.Size) ?? this.map.GetLayer("Buildings").PickTile(new Location(tileLocation.X * Game1.tileSize, (tileLocation.Y + 1) * Game1.tileSize), viewport.Size);
if (tile != null && tile.Properties.TryGetValue("Action", out PropertyValue propertyValue) && propertyValue != null)
return (this.currentEvent != null || this.isCharacterAtTile(vector + new Vector2(0f, 1f)) == null) && this.performAction(propertyValue, who, tileLocation);
return base.checkAction(tileLocation, viewport, who);
}
/*********
** Protected methods
*********/
/*
protected override void resetLocalState()
{
ModEntry.SMonitor.Log("DynamicDungeon#resetLocalState", StardewModdingAPI.LogLevel.Trace);
this.Time = Game1.timeOfDay;
base.resetLocalState();
this.resetForPlayerEntry();
this.forceViewportPlayerFollow = true;
}
*/
private void ExitResolver(Farmer player, string answer)
{
answer = string.IsInterned(answer) ?? answer;
if (answer == "yes")
Game1.warpFarmer("DynamicDungeonEntrance", 5, 7, false);
}
}
}