Search code examples
javascriptenchantjs

How to change the obstacle rules in enchant.js?


I'm making a RPG game with enchant.js,and then I created a map like this: sample map

The players can not pass through the water,but when players take a boat,they can pass through.My question is that should I have to rewrite the collistionData each time when players walking/boating?Is there a easy way to do that if the map is large?Thanks for help.


Solution

  • Not familiar with enchant.js, but typically how this is handled is using collision masks. So depending on what physics engine you're using, there should be a way to set collision mask values (use integers) on the physics bodies. With box2d for example you have two values:

    1. category bits
    2. mask bits

    The category is the number value that this object is or the type. So if you have a bunch of variables:

    const PLAYER = 1;
    const WATER = 2;
    const BOAT = 4;
    

    You would set the category bits to one of them. The mask bits is then the value of what masks it can collide with.

    const PlayerEntity = {
      category: PLAYER,
      mask: PLAYER | WATER
    };
    
    const WaterEntity = {
      category: WATER,
      mask: PLAYER
    };
    

    What physics engines do internally with this is an & operation to see if the category and mask values equate to something else than 0. If it does, then there's a collision.