Skip to content

Commit

Permalink
Update code to VS2019, fix unload errors, and remove recipe hack
Browse files Browse the repository at this point in the history
  • Loading branch information
DaeCatt committed Jul 25, 2019
1 parent ad7cd8c commit 5b9f849
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 142 deletions.
100 changes: 70 additions & 30 deletions GnomeWordsmith.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,31 @@
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using Terraria;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.UI;
using Terraria.ID;

namespace GnomeWordsmith {
class GnomeWordsmith : Mod {
namespace GnomeWordsmith
{
class GnomeWordsmith : Mod
{
internal static GnomeWordsmith instance;
internal static bool unityMouseOver = false;
public UserInterface customResources;
public ReforgeUI reforgeUI;

public GnomeWordsmith() {
public GnomeWordsmith()
{
}

public override void Load() {
public override void Load()
{
instance = this;
ReforgeUI.visible = false;

if (!Main.dedServ) {
if (!Main.dedServ)
{
// Create Gnome Reforge Interface
reforgeUI = new ReforgeUI();
ReforgeUI.visible = true;
Expand All @@ -32,15 +37,28 @@ public override void Load() {
}
}

public override void Unload()
{
base.Unload();

instance = null;
reforgeUI = null;
customResources = null;
}

// Insert our interface layer
public override void ModifyInterfaceLayers(List<GameInterfaceLayer> layers) {
public override void ModifyInterfaceLayers(List<GameInterfaceLayer> layers)
{
int inventoryIndex = layers.FindIndex(layer => layer.Name.Equals("Vanilla: Inventory"));

if (inventoryIndex != -1) {
if (inventoryIndex != -1)
{
layers.Insert(inventoryIndex, new LegacyGameInterfaceLayer(
"GnomeWordsmith: Reforge UI",
delegate {
if (ReforgeUI.visible) {
delegate
{
if (ReforgeUI.visible)
{
customResources.Update(Main._drawInterfaceGameTime);
reforgeUI.Draw(Main.spriteBatch);
}
Expand All @@ -51,8 +69,10 @@ public override void ModifyInterfaceLayers(List<GameInterfaceLayer> layers) {
}
}

public override void PostDrawFullscreenMap(ref string mouseText) {
if (!Main.player[Main.myPlayer].HasItem(instance.ItemType("PortableWormhole"))) {
public override void PostDrawFullscreenMap(ref string mouseText)
{
if (!Main.player[Main.myPlayer].HasItem(instance.ItemType("PortableWormhole")))
{
unityMouseOver = false;
return;
}
Expand All @@ -64,8 +84,10 @@ public override void PostDrawFullscreenMap(ref string mouseText) {
float offsetX = Main.screenWidth / 2 - Main.mapFullscreenPos.X * Main.mapFullscreenScale;
float offsetY = Main.screenHeight / 2 - Main.mapFullscreenPos.Y * Main.mapFullscreenScale;

if (Main.netMode == 1 && Main.player[Main.myPlayer].team > 0 && !Main.player[Main.myPlayer].hostile) {
for (int i = 0; i < Main.player.Length; i++) {
if (Main.netMode == 1 && Main.player[Main.myPlayer].team > 0 && !Main.player[Main.myPlayer].hostile)
{
for (int i = 0; i < Main.player.Length; i++)
{
/**
* Ignore players that are:
* - Yourself.
Expand All @@ -74,7 +96,8 @@ public override void PostDrawFullscreenMap(ref string mouseText) {
* - On another team.
* - Hostile.
*/
if (i == Main.myPlayer || !Main.player[i].active || Main.player[i].dead || Main.player[Main.myPlayer].team != Main.player[i].team || Main.player[i].hostile) {
if (i == Main.myPlayer || !Main.player[i].active || Main.player[i].dead || Main.player[Main.myPlayer].team != Main.player[i].team || Main.player[i].hostile)
{
continue;
}

Expand All @@ -88,22 +111,27 @@ public override void PostDrawFullscreenMap(ref string mouseText) {
float maxX = minX + 28f * Main.UIScale;
float maxY = minY + 28f * Main.UIScale;

if (Main.mouseX >= minX && Main.mouseX <= maxX && Main.mouseY >= minY && Main.mouseY <= maxY) {
if (Main.mouseX >= minX && Main.mouseX <= maxX && Main.mouseY >= minY && Main.mouseY <= maxY)
{
// For some reason Main.DrawPlayerHead is protected :(
// Main.DrawPlayerHead(Main.player[i], playerHeadCenterX, playerHeadCenterY, 2f, Main.UIScale + 0.5f);

if (!unityMouseOver) {
if (!unityMouseOver)
{
Main.PlaySound(SoundID.MenuTick);
}

foundTarget = true;
unityMouseOver = true;

if (Main.mouseLeft && Main.mouseLeftRelease) {
if (Main.mouseLeft && Main.mouseLeftRelease)
{
Main.mouseLeftRelease = false;
Main.mapFullscreen = false;
Main.player[Main.myPlayer].UnityTeleport(Main.player[i].position);
} else if (text == "") {
}
else if (text == "")
{
text = Language.GetTextValue("Game.TeleportTo", Main.player[i].name);
}

Expand All @@ -112,15 +140,19 @@ public override void PostDrawFullscreenMap(ref string mouseText) {
}
}

if (!foundTarget) {
for (int i = 0; i < Main.npc.Length; i++) {
if (!foundTarget)
{
for (int i = 0; i < Main.npc.Length; i++)
{
// Only check active NPCs that are set to townNPC.
if (!Main.npc[i].active || !Main.npc[i].townNPC) {
if (!Main.npc[i].active || !Main.npc[i].townNPC)
{
continue;
}

int headIndex = NPC.TypeToHeadIndex(Main.npc[i].type);
if (headIndex <= 0) {
if (headIndex <= 0)
{
continue;
}

Expand All @@ -134,28 +166,34 @@ public override void PostDrawFullscreenMap(ref string mouseText) {
float maxX = minX + headTexture.Width * Main.UIScale;
float maxY = minY + headTexture.Height * Main.UIScale;

if (Main.mouseX >= minX && Main.mouseX <= maxX && Main.mouseY >= minY && Main.mouseY <= maxY) {
if (Main.mouseX >= minX && Main.mouseX <= maxX && Main.mouseY >= minY && Main.mouseY <= maxY)
{
SpriteEffects effect = SpriteEffects.None;
if (Main.npc[i].direction > 0) {
if (Main.npc[i].direction > 0)
{
effect = SpriteEffects.FlipHorizontally;
}

Main.spriteBatch.Draw(headTexture, new Vector2(npcHeadCenterX, npcHeadCenterY), new Rectangle(0, 0, headTexture.Width, headTexture.Height), Color.White, 0f, new Vector2(headTexture.Width / 2, headTexture.Height / 2), Main.UIScale + 0.5f, effect, 0f);

if (!unityMouseOver) {
if (!unityMouseOver)
{
Main.PlaySound(SoundID.MenuTick);
}

foundTarget = true;
unityMouseOver = true;

if (Main.mouseLeft && Main.mouseLeftRelease) {
if (Main.mouseLeft && Main.mouseLeftRelease)
{
Main.mouseLeftRelease = false;
Main.mapFullscreen = false;

Main.NewText(Language.GetTextValue("Game.HasTeleportedTo", Main.player[Main.myPlayer].name, Main.npc[i].FullName), 255, 255, 0);
Main.player[Main.myPlayer].Teleport(Main.npc[i].position);
} else if (text == "") {
}
else if (text == "")
{
text = Language.GetTextValue("Game.TeleportTo", Main.npc[i].FullName);
}

Expand All @@ -164,11 +202,13 @@ public override void PostDrawFullscreenMap(ref string mouseText) {
}
}

if (!foundTarget && unityMouseOver) {
if (!foundTarget && unityMouseOver)
{
unityMouseOver = false;
}

if (text != "") {
if (text != "")
{
mouseText = text;
}
}
Expand Down
3 changes: 3 additions & 0 deletions GnomeWordsmith.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
<Target Name="BuildMod" AfterTargets="Build">
<Exec Command="&quot;$(tMLBuildServerPath)&quot; -build $(ProjectDir) -eac $(TargetPath) -define $(DefineConstants) -unsafe $(AllowUnsafeBlocks)" />
</Target>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="&quot;C:\Program Files (x86)\Steam\steamapps\common\Terraria\tModLoaderServer.exe&quot; -build &quot;$(ProjectDir)\&quot;" />
</Target>
</Project>
18 changes: 12 additions & 6 deletions GnomeWordsmithPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,42 @@
using Terraria.ModLoader;
using Terraria.ModLoader.IO;

namespace GnomeWordsmith {
internal class GnomeWordsmithPlayer : ModPlayer {
namespace GnomeWordsmith
{
internal class GnomeWordsmithPlayer : ModPlayer
{
public Item ReforgeItem;
public bool hasItem = false;

/**
* Keep track of the active reforging item on the player, to avoid
* having the item disappear under some conditions.
*/
public override void Initialize() {
public override void Initialize()
{
ReforgeItem = new Item();
ReforgeItem.SetDefaults(0, true);
}

public override TagCompound Save() {
public override TagCompound Save()
{
return new TagCompound
{
{ "HasItem", hasItem },
{ "ReforgeItem", ReforgeItem }
};
}

public override void Load(TagCompound tag) {
public override void Load(TagCompound tag)
{
/**
* HasItem ensures that we don't accidentally create nonsense items
* out of thin air.
*/
bool hasItem = tag.GetBool("HasItem");
Item loadedItem = tag.Get<Item>("ReforgeItem");
if (hasItem) {
if (hasItem)
{
ReforgeItem = loadedItem;
}
}
Expand Down
9 changes: 6 additions & 3 deletions Items/PortableWormhole.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using Terraria;
using Terraria.ModLoader;

namespace GnomeWordsmith.Items {
public class PortableWormhole : ModItem {
namespace GnomeWordsmith.Items
{
public class PortableWormhole : ModItem
{
public override void SetStaticDefaults() { }

// Teleport effect is handled in GnomeWordsmith.cs
public override void SetDefaults() {
public override void SetDefaults()
{
item.CloneDefaults(2997);
item.maxStack = 1;
item.consumable = false;
Expand Down
Loading

0 comments on commit 5b9f849

Please sign in to comment.