Making a Multiplayer WEB RPG - Part 5: Y Axis, Spawning & Better AI
Tags: babylonjs, colyseus, gamedev
Published: 2023-08-07 04:27:20

[GITHUB](https://github.com/oriongunning/t5c) Link to the full open source code
[DEMO](https://t5c.onrender.com/) Link to the latest demo build 
[PROGRESS POST](https://forum.babylonjs.com/t/multiplayer-top-down-rpg-babylon-js-colyseus/35733) The detailled progress post on Babylon.js forum
Technology used: Babylon.js 6.x / Colyseus 0.15 / SQLite

---

Hi there,

It's been a while, and much has changed.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vvxw3wd70ym909325u8m.PNG)

---

# Restructuring the server (again)

Just a quick update on the project: I’ve wasted ALOT of time on trying to restructure the server (test/yuka branch) in order to simplify and decouple the different parts (Colyseus, Yuka, Game manager, etc) and after a few different attempts and alot of work, I’ve not managed to get a solution that is as simple as having the Colyseus Schema as the main object of my game entities.

I’ve tried a few different ways:

- Having Yuka game entity as a base, and Colyseus schema as a child under it, that works but leads to more complexity and duplication and forcing me to create a sync function that runs on every iteration
- Having a custom game entity as a base, and Colyseus schema as a child but that leads to the same issues as above,
- Having a custom game entity and Colyseus at the same level and synchronize them using a specific id but that create its own complexity.
So I’ve reverted to using Colyseus schema as my base game entity, in order to be able to do for ex: this.mana = 100; and it will sync automatically to clients.

Simpler is better,

# Better AI using Finite State Machine
During the refactor above, I added a simple FSM state machine with an ENTER, EXECUTE & EXIT method to the enemy AI.

This is reasonably versatile and allowed to improved AI behaviour quite significantly all while reducing duplicate code.

Turns out, I only really need these 5 states:
- IDLE
- PATROL
- CHASE
- ATTACK
- DEAD

In the future, I will move from FSM to a behaviour tree, but that would be a little overkill currently.

You can find some video showing the results here: https://forum.babylonjs.com/t/open-source-multiplayer-3d-rpg-using-colyseus/35733/168?u=oriongu

---

# Dynamic spawns

I also added a simple spawning controller, and using a basic JSON structure attached to each level, it will keep adding and spawning enemies and NPC as needed.

```
// will create an aggresive ennemy (attackable by player) that will patrol by cycling through each of the specified waypoints
{
    type: "path",
    behaviour: "patrol",
    aggressive: true,
    canAttack: true,
    points: [
        new Vector3(17.47, 0.04, 2.55),
        new Vector3(31.77, 3.54, 2.56),
        new Vector3(32.46, 3.54, -11.21),
        new Vector3(16.87, 0.04, -8.92)
    ],
    radius: 0,
    amount: 1,
    race: "male_enemy",
    name: "Guard Pirate",
},
```

---

# Equipping & un-equipping Items

I added the ability to equip & un-equipping and item by right-clicking on them in the inventory.

I also added the relevant UI that goes with it to show what item are equipped.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lt3z54l2ogniegk72dlk.jpeg)

---

# Added the Y axis

The latest addition, and a big one, is the addition of the long waited/planned Y axis to the project.

So levels DO NOT need to be flat, YAY. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xb2gxoqrt3ds9m2livmz.jpeg)