Project: Treasure Hunter (formerly Dungeon Explorer)
Goal: Develop and release first game
Initial Commit: 7 Aug 2019
Dev Hours Since Last Devlog: 60.29
Total Dev Hours Spent: 130.5
Allocated Dev Hours: 160
At a Glance
If I let them side quests will suck away all my time, and that’s what happened since I wrote my last devlog in September. On the up side, I finally made my way back to my primary quest line, to develop and release my first game. Since then, I managed to log about 60 hours of dev time as well as naming my game Treasure Hunter: Quixxen’s Suit. Rather than discuss all of the things I worked on I will focus on the draggable panel I created.
Main Quest Progress
After making it so a player can complete a quest, I turned my attention to the research tree. The first thing I created was a custom sortable ResearchItem class. That was straightforward, so next up I wanted a draggable panel to display the research tree. I found some code that implemented a simple draggable window, however, it didn’t work whenever I changed the resolution and had a few other issues with some features I wanted to add.
I wanted to add a few features that seemed simple.
- Clamping the panel to the screen at all resolutions;
- Allow the user to toggle clamping on/off;
- Toggle on/off the ability to move the panel partially off screen and then have it close automatically;
- Configured the offset amount for the auto-close feature.
It turned out that the code I found couldn’t support my features without extensive refactoring. Back to Google I went.
On my second try I found the Unity Samples: UI asset pack. The DragPanel.cs script had the basic functionality I needed and was simpler then my last code. At first I tried to merge the two code bases together, but that didn’t work. After that I wiped the slate clean and dropping in Unity’s code. It worked perfectly at all tested resolutions. Awesome!
Now that I got the basics working, next up my extras, which turned out to be easy to fold into Unity’s code. Now that I had my code working I created a GitHub public repo for my 2D examples. So far, just DragWindow.cs and a GameObject extension called FindInParents<T> are there but I have to start somewhere.
The gifs below showoff my work. The first one displays the panel clamped to the screen, while the second showcases the offset close feature.
Here is my code, the first one is the GameObject extension that implements FindInParents<T> and the second is my DragWindow script.
using UnityEngine;
namespace SpaceMonkeys.Core
{
public static class GameObjectExtension
{
public static T FindInParents<T>(this GameObject gameObject, GameObject go) where T : Component
{
if (go == null) return null;
var comp = go.GetComponent<T>();
if (comp != null) return comp;
var t = go.transform.parent;
while (t != null && comp == null)
{
comp = t.gameObject.GetComponent<T>();
t = t.parent;
}
return comp;
}
}
}
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using SpaceMonkeys.Core;
namespace SpaceMonkeys.UI
{
/// <summary>
/// A script that you drop on a child object to the "window" GameObject you want to move around.
/// </summary>
/// <remarks>
/// Find the GitHub code at https://github.com/WeirdBeardDev/Unity-2D-Examples.
/// Find the specific file at https://github.com/WeirdBeardDev/Unity-2D-Examples/blob/master/Assets/_Game/Scripts/UI/DragWindow.cs.
/// </remarks>
public class DragWindow : MonoBehaviour, IDragHandler, IPointerDownHandler, IPointerUpHandler
{
#region Members
[Header("References")]
[SerializeField] private Image _contentImage = default;
[SerializeField] [Range(0f, 1f)] private float _contentImageAlpha = .4f;
[Header("Clamping Options")]
[SerializeField] private bool _clampToScreen = true;
[SerializeField] private bool _useOffScreenToClose = false;
[SerializeField] private float _allowedOffset = 250f;
private RectTransform _panelRT;
private RectTransform _dragAreaRT;
private Vector2 _originalLocalPointerPos;
#endregion Members
#region MonoBehaviours
void Awake()
{
_panelRT = transform.parent as RectTransform;
_dragAreaRT = gameObject.FindInParents<Canvas>(transform.parent.gameObject).gameObject.GetComponent<RectTransform>();
}
#endregion MonoBehaviours
#region Interface Implementations
public void OnPointerDown(PointerEventData data)
{
_panelRT.SetAsLastSibling();
OriginalPosition = _panelRT.localPosition;
RectTransformUtility.ScreenPointToLocalPointInRectangle(_dragAreaRT, data.position, data.pressEventCamera, out _originalLocalPointerPos);
ChangeAlpha();
}
public void OnDrag(PointerEventData data)
{
if (_panelRT == null || _dragAreaRT == null) return;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(_dragAreaRT, data.position, data.pressEventCamera, out Vector2 localPointerPos))
{
Vector3 offsetToOriginal = localPointerPos - _originalLocalPointerPos;
_panelRT.localPosition = OriginalPosition + offsetToOriginal;
}
if (_clampToScreen)
_panelRT.localPosition = _useOffScreenToClose ? ClampToScreen(_allowedOffset) : ClampToScreen();
}
public void OnPointerUp(PointerEventData data)
{
ChangeAlpha();
if (!IsFullyOnScreen)
{
_panelRT.gameObject.SetActive(false);
_panelRT.localPosition = OriginalPosition;
}
}
#endregion Interface Implementations
#region Properties
public Vector3 OriginalPosition { get; private set; }
public bool IsFullyOnScreen
{
get
{
bool ans = true;
Vector3[] worldCorners = new Vector3[4];
_panelRT.GetWorldCorners(worldCorners);
ans &= !(worldCorners[0].x < 0);
ans &= !(worldCorners[2].x > Screen.width);
ans &= !(worldCorners[0].y < 0);
ans &= !(worldCorners[2].y > Screen.height);
return ans;
}
}
#endregion Properties
#region Helpers
private void ChangeAlpha()
{
if (_contentImage != null)
{
Color bg = _contentImage.color;
switch (bg.a)
{
case 1f:
bg.a = _contentImageAlpha;
break;
default:
bg.a = 1f;
break;
}
_contentImage.color = bg;
}
}
private Vector3 ClampToScreen() => ClampToScreen(0f);
private Vector3 ClampToScreen(float offset)
{
Vector3 pos = _panelRT.localPosition;
Vector3 minPos = _dragAreaRT.rect.min - _panelRT.rect.min;
Vector3 maxPos = _dragAreaRT.rect.max - _panelRT.rect.max;
pos.x = Mathf.Clamp(_panelRT.localPosition.x, minPos.x - offset, maxPos.x + offset);
pos.y = Mathf.Clamp(_panelRT.localPosition.y, minPos.y - offset, maxPos.y + offset);
return pos;
}
#endregion Helpers
}
}
Keep On Questing
I’m roughly on track with my schedule from Planning For More Adventures. However, I don’t think I’ll make the 160 hours for v1. It’s good to have goals and it’s good to know when need to adapt to meet those goals.
Life’s an adventure – what’s your quest?
Git Commit History
fe6ae22 Update DragWindow so it acutally works; 2020-03-302327193 Create baseline to start implementing Research; 2020-03-230f1a39f Update Quest Location prefab button to cover progress bar; 2020-03-23e89ee92 Create basic Research Item game object for testing; 2020-03-23b185cfa Create StartingIncomeResearch class; 2020-03-2347df235 Move ResearchItem to Classes folder; 2020-03-2378702aa Merge branch ‘Research’ of https://github.com/WeirdBeardDev/dungeonexplorer into Research; 2020-03-23e2e4376 Refactor LeveledFloat; 2020-03-2307b2b76 Refactor LeveledFloat; 2020-03-23657b6e2 Refactor ResearchItem; 2020-03-23e036d7e Refactor Equality and HashCode; 2020-03-20f3ce21f Fix DragWindow for multiple screen sizes; 2020-03-16cfdd968 Fix ResearchPanel blocking GameManager; 2020-03-163aed3c6 Save MainGame file; 2020-03-15d21282b Remove temp sounds and unneeded areas; 2020-03-15cc7f6bd Update research points; 2020-03-1474d10e4 Update ResearchPanel to correctly display available zones; 2020-03-09ebfdcaa Make Goals easier to achieve for testing; 2020-03-01598efa1 Update PointDisplay to use ReserachManager; 2020-03-01a8a935d Add ResearchManager to ExplorationZone; 2020-03-01caf5768 Update Researchitem for equality and comparability; 2020-02-29c265dff Implement ResearchManager; 2020-02-299fc50ce Fix Unity project ref warnings; 2020-02-2933d5345 Updated to Unity 2019.2.21f1; 2020-02-26aa066ef Refactored EncounterIndex; 2020-02-257e6018c Convert To Unity 2019.3; 2020-02-189b46931 Fixed IncomeGoal Bug; 2020-02-112cc6c0d Fixed Research Toggle Bug; 2020-02-1103bcdb4 Fixed Double GoalCompleted Bug; 2020-02-11d2579d0 Refactored Stat; 2020-02-106801422 Refactored Lots; 2020-02-10bc2d009 Added RequiredComponent; 2020-02-10c0efeab Standardizing Code; 2020-02-10c2afafa Renamed QuestGoal to Goal; 2020-02-09a78eb22 Created ExplorationZone; 2020-02-09abe2429 Added ‘s’ to BurialMound; 2020-02-09521f720 Change QuestZone to ExplorationZone; 2020-02-092dd72a3 Wired CompleteGoal In Editor; 2020-02-097faaf9e Tweaked QuestZoneDetails; 2020-02-09a0cca43 Tweaked Location; 2020-02-09d6b3230 Lazy Load Location’s Quest; 2020-01-28b1e8725 Refactored WorkingQuest to Location; 2020-01-27b010771 Added Location to Quest; 2020-01-27fd6376f Removed Awake from Location.cs; 2020-01-2047b72d4 Prepped Changes For Adding Location to Quest; 2020-01-208722104 Updated DisplayCurrency; 2020-01-2097a54cb Created Location To Replace WorkingQuest; 2020-01-2088a0987 Cleaned Up GameManager Code; 2020-01-11f9704e8 Saved MainGame File; 2020-01-1066931e8 Updated ScreenActionShortcuts For Research Toggle; 2020-01-013d5fab8 Updated Point Display Prefab; 2020-01-0181404cd Created Window Prefabs; 2020-01-015a6e64f Saved MainGame File; 2019-12-31403280c Create GameObjectExtension Class; 2019-12-31a040872 Completed DragWindow Script; 2019-12-3006aa124 Creating Standard Window; 2019-12-295758411 Created Toggle Button Prefab; 2019-12-278e12441 Added Up/Down Arrow Image; 2019-12-26a2d869c Added ResearchItem Base class; 2019-12-268ecb78d More Folder Cleanup And Research Shortcut; 2019-12-265c3c301 Folder Cleanup On Aisle 2; 2019-12-26e9a05ad Created PointDisplay Prefab; 2019-12-247214067 Cleaned Up Prefab Folder; 2019-12-2413190e5 Numerous Updates; 2019-12-221debe03 Refactored WorkingQuests Images; 2019-11-3047e6953 Refactor QuestZoneDetails; 2019-11-3028c934f More ProgressBar Refactoring; 2019-11-138e95e34 Refactored ProgressBar; 2019-11-1256baeb9 Started Refactoring ProgressBar; 2019-11-12bd7a93b Refactored DisplayQuest; 2019-11-1074a8ade Refactored Shortcuts To Simulate Button Clicks; 2019-11-10d7d0461 Refactored Encounter Completion; 2019-11-099f86cae Commented Out Music Audio Source; 2019-11-09d24446b Refactored AdventurerDisplay; 2019-11-09ca43ca8 Refactor Shortcuts; 2019-11-0956af740 Coded EncounterList Class; 2019-11-09be5a513 Updated ActiveQuest Refs to ActiveWorkingQuest; 2019-11-0924c899c Code Reorg; 2019-11-09587c8e2 Removed DidComplete From EncounterCompletedEventArgs; 2019-11-097d5fda2 Changed Variable EncounterNumber to EncounterIndex; 2019-11-093858474 Created EncounterCompleted Event; 2019-11-02fd33999 Created Initial Adventurer’s UI; 2019-11-01be0e8de Started Overhauling Skills; 2019-10-27e79e6e1 Added Formulas and LeveledFloat Classes; 2019-10-2722ba643 Fixed Bug In Stat Class; 2019-10-27a9deb4d Refactored Quest Income To Use Stat Class; 2019-10-27b5189aa Renamed Trait to Stat; 2019-10-27d0932f9 Created Batch File To Change LFS Custom Location; 2019-10-204c76283 Updated Gitignore File To Include TextMesh Pro Folder; 2019-10-20e56dac8 Created GrantIncomeSkill; 2019-09-288cfd05a Created Base Skill Class; 2019-09-27904727c Created ExpertiseType; 2019-09-27acd0b77 Coded Trait Classes; 2019-09-260f0c6b4 Completed Alpha AudioManager; 2019-09-249ac7baa Created AudioManager Singleton; 2019-09-231926759 Created Save On Run Menu Item; 2019-09-230d139a7 Final GameManager-A Commit; 2019-09-230c3f380 Started Testing, Found Bugs, Fixed Some, Created More; 2019-09-22

Leave a reply to A Banner and Research – WeirdBeard's Blog Cancel reply