Thinking in graphs

So in my game, I added code to my cursor class so that it does a little bit more than move around and do nothing. I added some functions to trace the expected path that the character will take. However, I want it to act more intelligently rather than rely on the player to select each step. And there is this additional problem.

2018-07-25_1647

That’s right, I need to figure out how to maneuver around obstacles that the character shouldn’t be able to walk through. Pathfinding in general, is a topic that I don’t know very much about. And from initial research into the topic, I’ve learned that it requires that I view my map differently. Rather than just (x,y) coordinates, there is a mathematical concept called a graph which is used to model relationships between the locations. The terms used are nodes which represents locations and edges which are the connections/paths between the locations.

squares

Taking that information and translating it into code that actually does something is a bit overwhelming. So the first step for me is to take a square grid and figure out how to turn that into a graph. Here is my grid. The problem to solve is: Where are the connections?

Doing this in Unity is overkill for a problem like this and having to manage game objects seems like it would be distracting. So instead, I’m doing my research in Java.

The class ends up being simpler than I expected. Essentially all I need are an x and y variable that specify where they are and a list of objects of itself representing the different connections. Then I just add all the nodes to  a static list and build the connections with a for loop and similarly print by referencing the node from the static list.

So for instance this

</pre>
<pre>for(DemoGraph d : nodes)
        {
            d.addConnections();
            d.printConnections();
        }

Will output to 2018-07-25_1715


	

My time as a hobbyist game dev… so far

The main difficulty I’ve had when learning programming is after a point, it’s hard to find projects to practice programming skills. After a while, coming up with simple application ideas like coin sorting programs becomes boring and unchallenging even when you add fancy GUI components.

I’m a fan of RPG style games. In particular turn based JRPG games like the ones I played in my younger days on the snes. So naturally, I picked A JRPG as a project I where I could both practice coding and challenge myself. I named the project the Weekend RPG. So I buckled down to see how far I could get. For anyone who is interested, I used java with netbeans on a mac. The only non-standard library I used was Gson to make working with json a bit easier. I created all the assets myself and made no effort whatsoever to make them look nice. I only cared they looked like what they represent.

JRPG prototype in Java

As evident in the screenshot, the results are crude. It’s also doesn’t satisfy the label: “game” since it’s missing a way to progress and win/lose. However, it runs. It takes user input to move around and you can chat with the 3 npcs. There is logic associated with moving. You can’t stand on an NPC, or walk on water (blue tiles). As a first effort I’m happy with it.

It was also a good learning experience. When I first started it, I didn’t realize how cluttered my code would end up. Changing pretty much anything would difficult and I found it extremely useful to generalize my code through abstract classes and inheritance rather than duplicating implementation of common tasks. Lastly I came to accept that I’m likely doing a lot of things in a horribly inefficient way. As an example of that, the tile layout is stored as an external json file, and I found that reading the file and unpacking them as objects gets really slow on larger maps.

I’m confident that I can expand this prototype further with some more work. However, I’m not going to. At least not this java project. Coming up with my own implementations of common tasks like displaying sprites on a screen is going to consume a lot of time for little gain. Instead I’m going to switch to C# and use the Unity engine.

So why did I choose Unity.

  • It is free (at least it is for educational purposes)
  • It is used commercially by big name development studios
  • It doesn’t do all the work for me. Despite offloading a lot of the background tasks, it will still provide a challenge.

So my first task is to replicate my java prototype on unity. Exciting. Lets see how it goes.