Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I run this in a server? #14

Closed
jacklaplante opened this issue Nov 27, 2019 · 10 comments

Comments

@jacklaplante
Copy link

@jacklaplante jacklaplante commented Nov 27, 2019

I want to add AI controlled enemies to my multiplayer three.js game. How would I go about running this on a server?

@Mugen87

This comment has been minimized.

Copy link
Owner

@Mugen87 Mugen87 commented Nov 28, 2019

Some context: https://discourse.threejs.org/t/i-need-ideas-on-how-to-add-enemies-in-my-multiplayer-game/11006

Well, Yuka is just a bunch of classes with no rendering dependencies. So it's actually very easy to import Yuka into a node.js application and implement game logic. A very basic setup would look like so:

const YUKA = require( 'yuka' );

const entity = new YUKA.GameEntity();

setInterval( () => {

	entity.position.x += 1;

	console.log( 'New position:', entity.position );

}, 1000 );

This would update the game entity once per second. However, Yuka does not provide any kind of multiplayer/networking features you would normally find in a game engine. Writing a game server and the respective protocols is very difficult. So this would be a project of its own^^.

If you are interested, I suggest you start with studying how WebRTC and WebSockets work. Maybe you can build something on top of these Web APIs.

@jacklaplante

This comment has been minimized.

Copy link
Author

@jacklaplante jacklaplante commented Nov 28, 2019

Ya my current server uses websockets. It's very simple though. It essentially just relays the player's position to all the other players. But I need to figure out how to represent the world on the server side especially if I want to add AI. I will look into running three.js on the server side. It probably won't be the most efficient solution but at least it would allow some code parity.

Yuka looks really cool for what I'm trying to do. I'm excited to try it!

@jacklaplante

This comment has been minimized.

Copy link
Author

@jacklaplante jacklaplante commented Nov 28, 2019

Actually, if I just use a nav mesh with Yuka could I skip using three entirely?

@Mugen87

This comment has been minimized.

Copy link
Owner

@Mugen87 Mugen87 commented Nov 28, 2019

Actually, if I just use a nav mesh with Yuka could I skip using three entirely?

Yes, at least on the server side. I don't think using three.js or any other 3D engine is much helpful in addition to Yuka if your focus is just on implementing game logic.

BTW: The API style and math classes are quite similar to three.js. If you already know three.js, it should be a bit easier to get familiar with Yuka.

@Mugen87 Mugen87 closed this Nov 29, 2019
@jacklaplante

This comment has been minimized.

Copy link
Author

@jacklaplante jacklaplante commented Dec 1, 2019

Hey @Mugen87 I've hit a snag with running Yuka on my node server. I'm getting this error when I try and load a NavMesh.

ReferenceError: fetch is not defined
    at Promise (C:\Users\jack\bowdown\server\node_modules\yuka\build\yuka.js:18666:5)
    at new Promise (<anonymous>)
    at NavMeshLoader.load (C:\Users\jack\bowdown\server\node_modules\yuka\build\yuka.js:18664:11)
    at Object.entities.init (C:\Users\jack\bowdown\server\entities.js:15:12)

I think Fetch is a browser API and doesn't work in node. I'm going to try and see if I can fix this myself (I've never contributed to an open source project and I would like to) But any advice you have would be helpful.

@Mugen87

This comment has been minimized.

Copy link
Owner

@Mugen87 Mugen87 commented Dec 1, 2019

Try it with this npm package:

https://www.npmjs.com/package/node-fetch

We also use it in our unit tests which are executed in a node environment.

@jacklaplante

This comment has been minimized.

Copy link
Author

@jacklaplante jacklaplante commented Dec 1, 2019

node-fetch doesn't seem to allow the use of relative urls. So I would have to also set up a local webserver just to serve the navmesh. I'm looking into other possible approaches. I'm thinking fs.readFile might be my best bet. But that doesn't return a Promise so it would require some rewriting in Yuka

@Mugen87

This comment has been minimized.

Copy link
Owner

@Mugen87 Mugen87 commented Dec 2, 2019

61f2aaf introduced NavMeshLoader.parse(). You can now use the API like so:

const data = fs.readFileSync( path.join( __dirname, '../../../assets/navmesh/glb-embedded/navmesh.glb' ) );
const loader = new NavMeshLoader();
loader.parse( data.buffer ).then( ( navMesh ) => {
expect( navMesh ).is.an.instanceof( NavMesh );
expect( navMesh.regions ).to.have.lengthOf( 5 );
expect( navMesh.graph.getNodeCount() ).is.equal( 5 );
expect( navMesh.graph.getEdgeCount() ).is.equal( 8 );
done();
} );

I hope this makes the class more usable for you 👍

@jacklaplante

This comment has been minimized.

Copy link
Author

@jacklaplante jacklaplante commented Dec 3, 2019

I can confirm that this works now, I just needed to reinstall yuka (maybe the version number should be increased)

Thank you @Mugen87 !

@Mugen87

This comment has been minimized.

Copy link
Owner

@Mugen87 Mugen87 commented Dec 3, 2019

maybe the version number should be increased

I've actually increased it from 0.1.0 to 0.2.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.