Search code examples
javascripthtmlnode.jsweb-worker

How to "add" concurrency for existing JavaScript programming game


What would be some ways / the "best" way to add concurrency to the JavaScript programming game at jsrobots.com ??

It's essentially a JavaScript rewrite of an ancient DOS game called CROBOTS BUT with a very important omission - there is no proper concurrency or virtual machining of the robot scripts. Each robot user script executes, completely, turn by turn.

I explain more about this limitation in the "Robot scripting notes" section of the how to play page but basically it sucks and makes the game both unplayable and untrue to the original CROBOTS.

My question is how should I go about putting concurrency of robot scripts into the game? It's looking like one of NodeJS for a more server-side solution or browser Web Workers to keep it in the browser. Initially I'm mostly playing with web workers. Should I make the whole robot object a web worker? Or just the user scripted bit? The current robot object is responsible for drawing itself but web workers can't do this as no access to canvas object or DOM etc.

How would you do it?

The basic game design is like http://tinypic.com/view.php?pic=1zoxnbq&s=5 but this is basically a FOSS project so feel free to go crazy with View Source ;-) [actually there's a slightly older version in GitHub https://github.com/danielrhodeswarp/JS-Robots ]

Thank you


Solution

  • Web workers can be used to run the user script and then use the messaging api between the webworker and main thread to return anything the main thread needs to update the dom / draw once the worker has run everything it needs to. This would allow you to have a worker for each robot you want and run them in parallel.

    Check out this especially the passing data part.