About

This is a proof-of-concept, clone of Robocode. Built using Javascript & CoffeeScript using HTML5 Canvas and Web workers.

I'm proud to say this implementation has been the inspiration to a LiveScript port from Youchen Lee. Which is being used by the Taiwanese open-source community to teach a college course!

Demo

Try it out yourself!

Source

Fork me on github!

How does it work?

Every Robot runs in it's own Web Worker thread. A robot sub-classes a base robot which handles the worker/event-loop communication and event propagation. The new robot just has to implement behaviour using call-backs, reacting on possible events:

importScripts('base-robot.js');

ScanBot = BaseRobot;

ScanBot.run = function() {
	var robot = this;
	robot.shoot();

	robot.turn_turret_right(45);
	robot.move_forward(Math.random()*400, {
		DONE: function() {
			robot.shoot();
			robot.turn_right(Math.random()*90, {
				DONE: function() {
					robot.shoot();
					robot._run();
				}
			});
		},
		ENEMY_COLLIDE: function() {
			robot.shoot();
			robot.move_backward(100, {
				DONE: function() {
					robot._run();
				},
				WALL_COLLIDE: function() {
					robot._run();
				}
			});
		},
		WALL_COLLIDE: function() {
			robot.turn_left(180, {
				DONE: function() {
					robot.shoot();
					robot._run();
				}
			});
		}
	});
}

CoffeeScript

A preliminary CoffeeScript port is also included, making the whole base-bot extending and callback handling a bit cleaner:

importScripts('base-robot.js')

class TestRobot1 extends BaseRobot
    run: () ->
        @move_forwards 10, =>
            @move_backwards 10
            @turn_left 45, =>
            @_run()

tr = new TestRobot1("My first test robot")
blog comments powered by Disqus