/* some test code for jsgame */

/* first we define some entities */
function uco(doc, game) {
    /* user controlled object */
    if (doc) {
        this.doc = doc;
        this.game = game;
        this.left = 100;
        this.top = 100;
        this.width = 10;
        this.height = 10;
        this.name = 'uco';

        this.init_entity();
    };
};

uco.prototype = new jsgame.entity;

uco.prototype.init_entity = function() {
    (new jsgame.entity).init_entity.apply(this);
};

var _lastid = 0;
function cco(doc, game, target, x, y, speed) {
    /* computer controlled object

       this is a very stupid thing that just walks to the uco
    */
    if (doc) {
        this.doc = doc;
        this.game = game;
        this.target = target;
        this.left = x;
        this.top = y;
        this.width = 10;
        this.height = 20;
        this.speed = speed;
        this.name = 'cco ' + ++_lastid;
        this.removed = false;
        this.init_entity(x, y);
    };
};

cco.prototype = new jsgame.entity;

cco.prototype.init_entity = function(x, y) {
    var body = this.doc.getElementsByTagName('body')[0];
    var entity = this.entity = this.doc.createElement('div');
    entity.appendChild(this.doc.createTextNode('S'));
    entity.style.position = 'absolute';
    entity.style.left = x + 'px';
    entity.style.top = y + 'px';
    entity.style.backgroundColor = 'blue';
    body.appendChild(entity);
};

cco.prototype.update = function(state, timespent) {
    if (this.removed) {
        return;
    };
    var distance = this.speed * timespent;
    var rtop = Math.round(this.top);
    var trtop = Math.round(this.target.top);
    if (trtop < rtop) {
        this.top -= distance;
    } else if (trtop > rtop) {
        this.top += distance;
    };
    var rleft = Math.round(this.left);
    var trleft = Math.round(this.target.left);
    if (trleft < rleft) {
        this.left -= distance;
    } else if (trleft > rleft) {
        this.left += distance;
    };
    this.entity.style.left = Math.round(this.left) + 'px';
    this.entity.style.top = Math.round(this.top) + 'px';
};

cco.prototype.collide = function(other) {
    if (this.removed) {
        return;
    };
    if (other.name == 'uco') {
        this.entity.style.backgroundColor = 'black';
        this.speed = 0;
        misclib.schedule(this, this.remove, 2000);
    };
};

cco.prototype.remove = function() {
    if (this.removed) {
        return;
    };
    this.entity.parentNode.removeChild(this.entity);
    this.game.engine.del_entity(this);
    this.removed = true;
};

var ballid = 0;
function ball(doc, game, x, y, speed, accel) {
    /* some ball with a couple of properties */
    if (doc) {
        this.doc = doc;
        this.game = game;
        this.left = x;
        this.top = y;
        this.width = 10;
        this.height = 10;
        this.accel = accel;
        this.horizontal_speed = speed;
        this.currspeed = 0;
        this.directionx = 1;
        this.directiony = 1;
        this.name = 'ball_' + ballid++;
        this.init_entity(x, y);
    };
};

ball.prototype = new jsgame.entity;

ball.prototype.init_entity = function(x, y) {
    var body = this.doc.getElementsByTagName('body')[0];
    var entity = this.entity = this.doc.createElement('div');
    entity.appendChild(this.doc.createTextNode('o'));
    entity.style.position = 'absolute';
    entity.style.left = x + 'px';
    entity.style.top = y + 'px';
    entity.style.width = '10px';
    entity.style.height = '10px';
    entity.style.lineHeight = '10px';
    body.appendChild(entity);
};

ball.prototype.update = function(state, timespent) {
    if (this.direction == 1 && this.currspeed == 0 && this.horizontal_speed == 0) {
        return;
    };
    this.currspeed += this.directiony * this.accel * timespent;
    this.top += this.directiony * this.currspeed;
    if (this.directiony == 1 && this.top >= 480) {
        this.top = 480;
        this.directiony = -1;
        this.currspeed -= 1;
        if (this.horizontal_speed > 0) {
            this.horizontal_speed -= 20 * timespent;
        } else {
            this.horizontal_speed = 0;
        };
        if (this.currspeed < 0) {
            this.currspeed = 0;
        };
    } else if (this.directiony == -1 && this.currspeed <= 0) {
        this.currspeed = 0;
        this.directiony = 1;
    };
    this.entity.style.top = Math.round(this.top) + 'px';

    this.left += this.directionx * this.horizontal_speed * timespent;
    if (this.directionx == 1 && this.left >= 699) {
        this.left = 699;
        this.directionx = -1;
    };
    if (this.directionx == -1 && this.left <= 0) {
        this.left = 0;
        this.directionx = 1;
    };
    this.entity.style.left = Math.round(this.left) + 'px';
};

ball.prototype.collide = function(other) {
    if (other.name.substr(0, 5) == 'ball_') {
        this.directionx = -this.directionx;
    } else if (other.name == 'uco') {
        this.remove();
    };
};

ball.prototype.remove = function() {
    this.entity.parentNode.removeChild(this.entity);
    this.game.engine.del_entity(this);
};

/* now that main game class, contains some properties and initializes the game
*/
function jstestgame(doc) {
    if (doc) {
        this.left = 1;
        this.top = 1;
        this.width = 700;
        this.height = 500;

        this.doc = doc;

        try {
            var engine = this.engine = new jsgame.engine(doc, this);
            engine.create_canvas();

            var user = new uco(doc, this);
            var enemy1 = new cco(doc, this, user, 300, 200, 80);
            var enemy2 = new cco(doc, this, user, 500, 400, 40);
            engine.add_entity(user);
            engine.add_entity(enemy1);
            engine.add_entity(enemy2);

            for (var i=0; i < 10; i++) {
                var b = new ball(doc, this, 5 + Math.random() * 690,
                                 5 + Math.random() * 200, 5 + Math.random() * 10,
                                 2 + Math.random() * 10);
                engine.add_entity(b);
            };

            function init_serverchannel() {
                this.serverchannel = new chat.ServerChannel('chat.cgi');
                var handler = function(data) {
                    testing.debug('data received: ' + data);
                };
                this.serverchannel.initialize(handler);
            };
            misclib.schedule(this, init_serverchannel, 100);
            
            engine.start();
        } catch(e) {
            var message = e.message || e.msg || e + '';
            if (e.stack) {
                message += '\r\n' + e.stack;
            };
            testing.debug(message);
        };
    };
};

jstestgame.prototype.quit = function() {
    this.serverchannel.close();
};


