Sunday 13 March 2016

Final A* Implementation and Inventory

Following on from my last, Continuing with A*, I met with my lecturer Chris Janes, I have now got my A* pathfinding working much more accurately.

Last time, I was struggling to alter the diagonal movement within the pathfinding. With the advice I was given at my meeting, I have now altered the pathfinding to work along horizontal and vertical connections.

Previously, the pathfinding would look at neighbouring nodes (public List<Node> GetNeighbours in Grid.cs) by creating a 3x3 grid, with the current node in the centre, and checking every position in the grid bar the centre to determine how to move. Now, when neighbours are being added, it simply calls on the nodes positioned above, below, left, and right. I had originally attempted to alter the use of the 3x3 grid, cutting out diagonally placed nodes, but it was pointed out that I could achieve this much quicker by simply calling on the 4 neighbours the node required.

Movement has been tweaked, so there is a slight delay in a unit moving from space to space, giving a sense of visible movement. While I have issues that will be ironed out later, at this point, I now have a successfully working pathfinding system.

Following this, I decided to start looking towards my inventory system. Going along the ideas explained in my previous post, I still believe that it will be most beneficial to get an inventory system implemented, before moving onto AI.

I have created 2 new classes, the Item class, and the Items class:

  • The Item class is a representation of an item itself. It can function as either a Weapon or utility item, currently using the variable stats Damage and Hit Chance, or Heal amount.
  • These items are created within the Items class. Though currently containing only one, the items class will have a method for every item available within the game. The method creates a new Item class, assigning the required stats, then pushes it into the players Inventory List.
At first, I ran across some issues:
  • When I initially tried the method above, I received a warning stating that a MonoBehaviour based script, I could not create it as I was attempting to:


public void IronSword(Unit unit)  {


            Item item = new Item(0, "Iron Sword", 5, 10, 0);

              unit.inventory.Add(item); 
      (This was originally being pushed externally. In the generateUnits method in GameManager, the item was being called to be created through the Items script, being stored as a public Item variable on that script, then trying to move that Item into the Unit's inventory.)
            }
        • Following this, I removed the MonoBehaviour from the Item class. While I was no longer receiving the warning, I was getting Errors stating that, once the item was being pushed into the inventory, that no object existed to be pushed.

        • Not clear on why this error was being received, I instead chose the opposite route; to work with the item as a MonoBehaviour class. To do this, I had to use AddComponent(), to attach the item class as a script to the unit gameObject. Due to how the Item class has been set up (Visible below under Item.cs), I could create a new Item class, but I could not input the values required, instead leaving a blank item class on the unit.

        • As this was getting me nowhere, I went back once again, removing MonoBehaviour again from the Item class. I attempted the method seen above, with unit.inventory.Add(item) being added at this point. The unit in question is currently being set through the generateUnits method in GameManager. As items outside of set up should only be accessible to the current unit at the time, I will be able to call on the current unit as the (Unit unit).

        Overall, while I still have some steps to make to finishing the inventory system, I have found it much easier to implement than expected. My main concern at this point is the sorting and re-ordering of Inventory List. While I may be able to create a work-around, I intend to keep the active weapon in space [0] of the list, rather than requiring further bools in the Item class, or for the player to specifically keep referencing items within the inventory.

        This shall be completed and implemented around the middle of next week, so AI implementation can be ideally before next week is out.

          No comments:

          Post a Comment