Jump to content

Can Babylon help with game logic?


Zagrava
 Share

Recommended Posts

In every game you have entities (players, mobs), they almost always have some attributes: it can be an array of skills, hp, speed, all of that very common game logic stuff. Most likely during a game process, these attributes will change: an array will be pushed/popped, hp will be changed and so on. I am going to use Babylon on the server side (using NullEngine) as a physics engine. But I am also curious can it help me with this very common game logic stuff that I have described. I glanced through the docs but haven't seen anything that I want yet.

Does this functionality exist in game engines at all? I am talking about helping with common game logic stuff. It should be, right? Does Babylon have it? And by the way what do you think about the idea of using Babylon on the server?

Link to comment
Share on other sites

1 minute ago, Deltakosh said:

Hello and welcome :)

In babylon.js you have multiple options to store game data:

- you can use entity.metadata and save whatever you want here (recommended solution)

- you can use tags: http://doc.babylonjs.com/resources/tags

- perhaps even behaviors: http://doc.babylonjs.com/features/behaviour

Thanks! Glad to be in your community! But what do you think about the idea of using Babylon on the server? Will it work if I let's say use PlayFab as Baas, and Babylon or maybe even Playcanvas on the client side?

Link to comment
Share on other sites

9 minutes ago, Deltakosh said:

Yes no problem, a lot of people already did it

Thank you so much! If you have time, can you give me more info about games that use it like that? How do they use? How can I contact them? Do they use Babylon also on the client? Do they use Playfab? Don't want to be annoying, I just want to have all the info I can gather. ?

Link to comment
Share on other sites

Hi @Zagrava and welcome from me. One thing to try would be to do search for multiplayer in the following sub-forums of the Babylon.js forum:

Announcements

Questions and Answers

Demos and Projects

Make sure you select the This Forum radio button before searching each sub-forum.

For some reason trying to search with This Forum selected  in the overarching Babylon.js forum leads to no results

Link to comment
Share on other sites

I'm working on an ECS. You can checkout code here. Below is a snippet of how its structured.

class Entity {
  foo() {
    console.log('foo from Entity');
  }
}

Entity.with = function (...components) { 
    return components.reduce((base, component) => component(base), this);
}


let ActionComponent = (base) => class extends base {  
  foo() {
    console.log('foo from ActionComponent');
    if (super.foo) super.foo();
  }
};


let NavigationComponent = (base) => class extends base {  
  foo() {
    console.log('foo from NavigationComponent');
    if (super.foo) super.foo();
  }
};


class Player extends Entity.with(ActionComponent, NavigationComponent)  {  
  foo() {
    console.log('foo from Player');
    super.foo();
  }
}

new Player().foo();

 

 

Link to comment
Share on other sites

I'm sure its a matter of taste, but personally I like to keep the bulk of my game logic outside of any other engine. I just use the engine features a la carte. Here's a sample entity from a multiplayer game I'm working on now.

class PlayerCharacter {
    constructor(scene) {
        this.mesh = new BABYLON.Mesh('dummy', scene)
        this.mesh.ellipsoid = new BABYLON.Vector3(1, 4, 1)       

        this.hp = 100
        this.kevlar = 25
        this.likesCats = true
        this.acceleration = 25
        this.recoilAmount = 0
        this.x = 1
        this.z = 1
        this.y = 20
        this.velocity = new BABYLON.Vector3(0, 0, 0)

        this.weaponSystem = new CooldownElement(0.1)
        this.jumpSystem = new CooldownElement(0.5)

        this.scene = scene
    }

    get x() { return this.mesh.position.x }
    set x(value) { this.mesh.position.x = value }

    get y() { return this.mesh.position.y }
    set y(value) { this.mesh.position.y = value }

    get z() { return this.mesh.position.z }
    set z(value) { this.mesh.position.z = value }

    update(delta) {
       /* skipped */
    }

    processPlayerCommand(command) {
        if (command.primary) {
            if (this.weaponSystem.isOffCooldown()) {
                this.weaponSystem.use()
                /* skipped: code that casts a ray through
                 * the scene and does damage to other players */
            }
        }

        this.weaponSystem.update(command.delta)
        this.jumpSystem.update(command.delta)
  
        if (command.forward) {  }
        if (command.backward) {  }
        if (command.left) { }
        if (command.right) {  }

        /* skipped: moving the player collisions, jumping, external forces, etc */
    }
    takeDamage(amount) { /* does what you would think */
}

All I'm really trying to show with the pseudo code is that this class just has whatever properties I want and it uses Mesh, Scene, and Vector3 where it needs to. This as opposed to subclassing an entity from some engine, or a mesh from babylon. 

This game uses NullEngine on the server where it creates objects that use either the real meshes or dummy meshes and ellipsoids depending on which collisions I care about. On the client it is just normal Babylon stuff. It uses websockets, and sends primarily the position xyz and rotation xyz of the players moving around. It also sends some messages when a player fires a shot so that the other players can draw it. Both client and server just load the same map when they start up.  I'm a network programmer so I may have drastically oversimplified some things, and I don't mean to suggest that programming a multiplayer first person game is easy,  but it is definitely possible and NullEngine helps.

Depending what you mean by game logic babylon can be more or less involved. I'm keeping it *less* involved deliberately, but still I have big sections of code surrounding collisions and animations that are babylon-centric. Entity management I do myself.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...