Tuesday, 16 October 2018

Implementing basic systems

Over the course of the past two weeks or so I've been working on implementing the basic versions of the systems that will be needed. This way it's easier to break tasks in to smaller tasks to knock out while adding features!

First thing I worked on after the Inventory is the battle system. Creating it in it's basic form was actually very simple. All you really need is a health variable for each enemy and to make attacks take away from that and destroy them when it reaches 0.

So, first thing I did was set up some of the UI and battle screen to get more of an idea of how to code it. I had already designed a rough template before, so it was very quick.

Some simple animation added to also make it a tiny bit more alive! 

After this was done, most of the work for it was done in a day, I made an enemy and a player class to feed stats in to. The players stats are stored as a saved PlayerPrefs value in unity, but there has to be an additional way to store the player's stats, at least for now, to allow the player to not lose their health through a battle and then load their save before the battle and still be down health!

The stats for enemies include a stat that determines how far their stats can range. For example, a ghost might have a health range of 7 and a health of 20, this means that when the monster is spawned for the battle, it can have 20 - 27 health.

For the basic system, the player only currently has a normal attack that is calculated from the strength and agility of the player divided by 2. This does flat damage to the enemy but will take in to account the weapon type, relevant player stats and relevant enemy defenses in the future (enemy damage already takes some of this in to account!)


Lastly, I implemented turns! This is well, pretty standard. Just some checks on whose turn it is, which is controlled by their variables player turn > is enemy 1 alive? if so, take turn > enemy 2 etc. There's more but I won't go in to too much detail for this one.

Ok, so next, between a few bug fixes and small changes, I worked on a pretty important thing to implement in to many systems - item rewards! 

The first way I implemented this is through a prefab that is created in the reward array script. The reward array is linked to by different event outputs so if the players choose a certain option during an event I can give them a specific reward. 

It starts with a function that creates a UI prefab for holding the items, and then feeding an item array in to the function which decides what items are given. This can be different depending on the option the player chose!

Most of the magic comes from the script attached to the item prefab. There's a few things it does; it pulls the item's data and puts it in the tooltip when hovered, gets rid of the tooltip when the item is no longer hovered and adds the item to the player's inventory when clicked (if there is any space in the inventory)

After this, it was a relatively small amount of work to implement this in to other systems. Between this time and now, I created very basic campfire, treasure and shop systems. Kinda drawing the rest of the owl here but there's more to do! I'm sure to talk about these new systems when I've added a bit more. For now feel free to take a look at the newest showcase!

Progress is a bit slower right now as I'm dealing with some personal problems. Looking forward, the next step is a lot of pollish and new features to the existing systems. Stay tuned!

Monday, 8 October 2018

Creating the Inventory System

Last week I started work on implementing the inventory system that will be used in my game. Here I'm going to document how I managed to simplify it into simple steps that I could implement one at a time to get to a system that functions as you would expect from a classic RPG inventory system.

The first step I took towards creating the inventory was implementing the equipment for the character. I had a good idea of how I wanted it displayed and what equipment to be displayed, so I got to work on creating the base visual elements I needed and got to work building an example tooltip that would be displayed when hovering over an item.

I managed to make a good system for the tool tip to be displayed by using various elements of unity. The ContentSizeFitter element allows the children of the tooltip to expand it, something you'd expect from a tooltip. 

After creating the base template I went to work creating an equipment class that could hold the various stats of that specific equipment. To hold the various equipment the player had equipped I decided to have them implemented as an array of equipment in another class (you are probably better of using JSON)

I went to work making the tooltip pull data from the right point in the array that corresponds with the item that's hovered. I managed this by having each equipment slot having a public variable that I can change in the editor, allowing me to set each equipment slot to a integer that points to that number in the array. The script attached to each inventory slot allows the tooltip to be shown when hovered over and changes the tooltip to be visible and it's content to be changed, pulling it's stats from the ones stored in the array.

From here I decided to develop the inventory side which would hold all the other items and equipment that the player did not have equipped. To start, I made it look more like an inventory with some additional assets and then made the inventory button open and close it, as you'd expect. Some various fixes went in to this step as well, such as not moving the map when the inventory is open.

The ContentSizeFitter and GridLayout helped me get the slots looking nice and organized!

So, between the next step I implemented the first version of the Item class, storing similar variables to the equipment class but without the stats. Most of the stuff here was behind the scenes so not much to show, but here is where I made the tooltip switch from displaying left of the mouse to right of the mouse depending on which side of the screen the mouse is hovered over (again one of those simple things that you come to expect).

The next big step of the process was creating drag and drop features! This was fun. I started by making a smaller image of the item that the player is dragging under the mouse. This quickly evolved to being able to drag items and equipment to other slots although it definitely wasn't perfect. A lot of polish was needed in the next steps!

Yes, you could equip apples.

I had a lot to get sorted in the next step. The way items and equipment interacted when moving with each other wasn't very logical, the inventory slots stored item variables and the equipment slots stored equipment variables... Causing a lot of unwanted behaviour (you can see when the helmet is moved in the video it's stats stay in the slot!)

My solution seemed quite obvious after awhile. I needed to use polymorphism between the item and equipment classes. The Equipment class would inherit from the Item class and various item functions needed to be changed to virtual functions to allow the Equipment functions to override them if needed. For example the tooltip for item needed to use it's name and item type, whereas the tooltip for equipment needed all the stats added in too.

Item classes moving function
There was a very long day of figuring out logic due to the interactions between the Item class and the Equipment class during switching positions with each other. When moving an item with a piece of equipment there is logic required that I didn't expect. 

There has to be many constraints; you can't move an item in to an equipment slot, equipment of a specific type could only be equipped on their assigned slot (helmets to helmet slot, rings to ring slots etc) and empty slots need to be able to switch places with anything unless in the equipment slots (this stumped me for a while). This required several checks when moving an item and several more when moving equipment.

 SOME of the spaghetti that is the equipment move checks

I think I'd have to write a whole book to document it here, but solving the problems required a lot of debugging and scuffed MS Paint drawings to understand the logic. Maybe I'll write more about it some day. But ultimately after I solved these logic problems it was mostly finished.

As the finishing touch, I made it so that the stats gained from equipment were updated on equipment change and added a few more placeholder assets. It kind of even feels like a proper inventory now! Here is the final product.

I still have many features to implement, saving and loading equipment/inventory most notably. But right now it's at stage where I'm able to work on the next step - the battle system. Thanks for reading!