Skip to content

Commit

Permalink
Fix Localization Bug & Improve Teleportation Logic
Browse files Browse the repository at this point in the history
Localization for the Portable Wormhole was broken and is now fixed.
Teleportation logic is now 1:1 with vanilla Wormhole potions.
  • Loading branch information
DaeCatt committed Jan 12, 2019
1 parent 4f3f3a8 commit b2969da
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 48 deletions.
66 changes: 24 additions & 42 deletions Items/GnomeWordsmithGlobalItem.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,26 @@
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria;
using Terraria.Localization;
using Terraria.ModLoader;

namespace GnomeWordsmith.Items {
class GnomeWordsmithGlobalItem : GlobalItem {
public GnomeWordsmithGlobalItem() { }

/**
* Handle Portable Wormhole teleportations. This could be done on the
* Portable Wormhole item directly, but we might want to introduce
* other items that can teleport (eg Cellphone upgrade).
*/
public override void UpdateInventory(Item item, Player player) {
if (!Main.mapFullscreen && item.type != mod.ItemType("PortableWormhole"))
if (!Main.mapFullscreen || item.type != mod.ItemType("PortableWormhole") || !Main.player[Main.myPlayer].HasItem(mod.ItemType("PortableWormhole")))
return;

if (!Main.mouseLeft || !Main.mouseLeftRelease)
return;

// Translate fullscreen map position into world coordinates
float mapScale = 16f / Main.mapFullscreenScale;
float mapCenterX = Main.mapFullscreenPos.X * 16f - 10f;
float mapCenterY = Main.mapFullscreenPos.Y * 16f - 21f;

// Translate mouse position relative to the center of the screen
float mouseDeltaX = Main.mouseX - Main.screenWidth / 2;
float mouseDeltaY = Main.mouseY - Main.screenHeight / 2;

/**
* Translate cursor position on the fullscreen map into cursor
* position on the entire map.
*/
float cursorOnMapX = mapCenterX + mouseDeltaX * mapScale;
float cursorOnMapY = mapCenterY + mouseDeltaY * mapScale;
float mapWorldScale = Main.mapFullscreenScale / 16f;
float offsetX = Main.screenWidth / 2 - Main.mapFullscreenPos.X * Main.mapFullscreenScale;
float offsetY = Main.screenHeight / 2 - Main.mapFullscreenPos.Y * Main.mapFullscreenScale;

/**
* Only check teleports to other players if we're only, we're on a
Expand All @@ -53,27 +40,27 @@ public override void UpdateInventory(Item item, Player player) {
continue;
}

float minX = Main.player[i].position.X - 14f * mapScale;
float minY = Main.player[i].position.Y - 14f * mapScale;
float maxX = Main.player[i].position.X + 14f * mapScale;
float maxY = Main.player[i].position.Y + 14f * mapScale;
float playerHeadCenterX = offsetX + mapWorldScale * (Main.player[i].position.X + Main.player[i].width / 2);
float playerHeadCenterY = offsetY + mapWorldScale * (Main.player[i].position.Y + Main.player[i].gfxOffY + Main.player[i].height / 2);
playerHeadCenterX -= 2f;
playerHeadCenterY -= 2f - Main.mapFullscreenScale / 5f * 2f;

float minX = playerHeadCenterX - 14f * Main.UIScale;
float minY = playerHeadCenterY - 14f * Main.UIScale;
float maxX = minX + 28f * Main.UIScale;
float maxY = minY + 28f * Main.UIScale;

if (cursorOnMapX >= (double) minX &&
cursorOnMapX <= (double) maxX &&
cursorOnMapY >= (double) minY &&
cursorOnMapY <= (double) maxY) {
if (Main.mouseX >= minX && Main.mouseX <= maxX && Main.mouseY >= minY && Main.mouseY <= maxY) {
Main.mouseLeftRelease = false;
Main.mapFullscreen = false;
Main.player[Main.myPlayer].UnityTeleport(Main.player[i].position);
// hoverTarget = Main.player[i].name;

return;
}
}
}

for (int i = 0; i < Main.npc.Length; i++) {
// Only check NPCs that are set to townNPC.
// Only check active NPCs that are set to townNPC.
if (!Main.npc[i].active || !Main.npc[i].townNPC) {
continue;
}
Expand All @@ -83,25 +70,20 @@ public override void UpdateInventory(Item item, Player player) {
continue;
}

float halfWidth = Main.npcHeadTexture[headIndex].Width / 2;
float halfHeight = Main.npcHeadTexture[headIndex].Height / 2;
float npcHeadCenterX = offsetX + mapWorldScale * (Main.npc[i].position.X + Main.npc[i].width / 2);
float npcHeadCenterY = offsetY + mapWorldScale * (Main.npc[i].position.Y + Main.npc[i].gfxOffY + Main.npc[i].height / 2);

float minX = Main.npc[i].position.X - halfWidth * mapScale;
float minY = Main.npc[i].position.Y - halfHeight * mapScale;
float maxX = Main.npc[i].position.X + halfWidth * mapScale;
float maxY = Main.npc[i].position.Y + halfHeight * mapScale;
float minX = npcHeadCenterX - Main.npcHeadTexture[headIndex].Width / 2 * Main.UIScale;
float minY = npcHeadCenterY - Main.npcHeadTexture[headIndex].Height / 2 * Main.UIScale;
float maxX = minX + Main.npcHeadTexture[headIndex].Width * Main.UIScale;
float maxY = minY + Main.npcHeadTexture[headIndex].Height * Main.UIScale;

if (cursorOnMapX >= (double) minX &&
cursorOnMapX <= (double) maxX &&
cursorOnMapY >= (double) minY &&
cursorOnMapY <= (double) maxY) {
if (Main.mouseX >= minX && Main.mouseX <= maxX && Main.mouseY >= minY && Main.mouseY <= maxY) {
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);
// hoverTarget = Main.npc[i].FullName;

return;
}
}
Expand Down
6 changes: 1 addition & 5 deletions Items/PortableWormhole.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using Terraria;
using Terraria.ModLoader;
using Terraria.Localization;

namespace GnomeWordsmith.Items {
public class PortableWormhole : ModItem {
public override void SetStaticDefaults() {
DisplayName.SetDefault(Language.GetTextValue("Mods.GnomeWordsmith.ItemName.PortableWormhole"));
Tooltip.SetDefault(Language.GetTextValue("Mods.GnomeWordsmith.ItemTooltip.PortableWormhole"));
}
public override void SetStaticDefaults() { }

// Teleport effect is handled in GnomeWordsmithGlobalItem.cs
public override void SetDefaults() {
Expand Down
2 changes: 1 addition & 1 deletion build.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
author = DaxterSpeed
version = 0.99.3
version = 0.99.5
displayName = Gnome Wordsmith (Beta)
homepage = https://github.com/DaxterSpeed/GnomeWordsmith
hideCode = false
Expand Down

0 comments on commit b2969da

Please sign in to comment.