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.


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.


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>for(DemoGraph d : nodes)

Will output to 2018-07-25_1715


Getting started on the battle system



Looks pretty much the same…





Refining player movement






 protected IEnumerator SmoothMovement(Vector3 destination)
Move(destination - transform.position);
yield return null;





Getting fancy with tiles






public void AddPrototype(string key, TerrainTile tt)
if (prototypes.ContainsKey(key) == false)
prototypes.Add("key", tt);
Debug.Log("Invalid Prototype");
//throw new ArgumentException();


Working with tiles and managing players



So what’s different? Well 3 things.





Adding tile based collision

My original plan of adding the colliders and letting Unity deal with all the collisions failed pretty miserably the first time. My new plan is to call a function to check if there are any collisions at the destination, and only move if it is clear. In Unity, typically functions called raycasts or linecasts are used. They invisibly extend in the given direction and return any collisions.  After a bit of checking the documentation on linecasts, I wrote a function to validate movement.

protected bool ValidateMove(Vector3 movement)
// linecast one unit over in the proper direction. If null return false. Otherwise return true.
float xPos = transform.position.x;
float yPos = transform.position.y;
Vector2 startPoint = new Vector2(xPos, yPos);
Vector2 endPoint = new Vector2(movement.x + xPos, movement.y + yPos);

if (Physics2D.Linecast(startPoint, endPoint))
Debug.Log("Blocked - Something is in the way");
return false;
return true;

There is a problem thought. Turns out that linecasts will return any  collisions from the start point and end point and this includes the box collider surrounding the very object I am moving, so I need to disable it while running the linecast. Here is the second try, accounting for that.

protected bool ValidateMove(Vector3 movement)
float xPos = transform.position.x;
float yPos = transform.position.y;
Vector2 startPoint = new Vector2(xPos, yPos);

// get the objects box collider and disable for the linecast. Reenable after the linecast is complete.
BoxCollider2D box = this.GetComponent();
box.enabled = false;
RaycastHit2D linecast = Physics2D.Linecast(startPoint, endPoint);
box.enabled = true;

if (linecast)
return false;
return true;

So, I save everything and run it. Voila, my sprite moves around and properly collides with the water, rock and npc. No walking on water or clipping through things. There are a couple things, I can still do to improve the code. For instance, I can direct my linecast to check particular layers in case I wanted to treat collisions differently. An example of that would be if I wanted to let my player push the rock instead of having it block movement (something, I will do eventually). But for now, I’m satisfied with it.



Generalizing movement


public class PlayerController : MonoBehaviour {

void Update () {
if (Input.GetKeyDown("right"))
else if (Input.GetKeyDown("left"))
else if (Input.GetKeyDown("up"))
else if(Input.GetKeyDown("down"))






public abstract class Entity : MonoBehaviour {
protected void Move(Vector3 movement)
transform.position += movement;



<blockquote>void KeyboardInput()
if (Input.GetKeyDown("up"))
else if (Input.GetKeyDown("down"))
else if (Input.GetKeyDown("left"))
else if (Input.GetKeyDown("right"))


Somewhere to get started




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.