DEV Community

Tre
Tre

Posted on

Why I Coded Wall Detection in my Game

For the past week I've been building a topdown sandbox game in Javascript and Phaser.

Im sorry if this image is tiny, I will not pretend Im good at these kinds of blogs. Next time I'll bring a bigger one.
My phaser game after implementing wall detection

While doing this project, I've learned alot about not only Phaser but problem solving altogether.
Lately I began to worry AI has been hijacking my critical thinking skills. This project has been my way to combat that notion.
Everytime I encounter an issue, I've began following a workflow in order to plan my solutions.
I begin by articulating the problem and the goal. Today, my problem was how can I grab data from the tiles around my characters?
I want my player and npcs to be able to interact with the tiles around them even if they're just knocking on wood or inspecting a table.

All I have so far in my world are walls so I started with detecting those.

I studied the way Cataclysm Dark Days Ahead did it and noticed they stored all their data in json files that are later interpreted. I figured I could do the same
with object in Javascript (So I dont end up writing an interpreter language).

To get the tiles around my player, I had to grab my player's tile location. Then I stored locations in each cardinal direction using this code:

let collisions = {
      TopWallTile: this.wallsLayer.getTileAtWorldXY(this.x, this.y + -1 * this.tileSize),
      BottomWallTile: this.wallsLayer.getTileAtWorldXY(this.x, this.y + 1 * this.tileSize),
      LeftWallTile: this.wallsLayer.getTileAtWorldXY(this.x + -1 * this.tileSize, this.y),
      RightWallTile: this.wallsLayer.getTileAtWorldXY(this.x + 1 * this.tileSize, this.y),
    }
Enter fullscreen mode Exit fullscreen mode

Its probably not the best way but its honest work. Lots of brain juice went into this.
I used this object to get... wall data... and send it to my UIScene to display.
In hindsight, this was me just trying to figure out how tiles work and how I can grab and access tile data.
I didnt have much useful data on my tiles as of now but I plan to add more properties the player could mess with like "breakable", or "inspectable" (Most everything will be inspectable. Thats only proper for these kinds of games).

So we made a small (3 hour long) step today.
I feel more confident tackling the rest of this interaction system.
I promise in the future I'll have more interesting things to share but for now I need to start slow and try to understand everything. I anticipate this game to have alot of systems and if im going to work on this, I dont want to navigate something I cant understand.
Working in tech has taught me the importance of being able to articulate not only your projects, but your problems and proposed solutions. And In that regard, I've learned a ton.

Till next time!

Top comments (0)