// Student Unions & Activities
// University of Minnesota

var ANDYWIDTH = 100;
var ANDYHEIGHT = 100;

function Andy(div) {
    this.div = div;
    
    this.width = ANDYWIDTH;
    this.height = ANDYHEIGHT;
    
    this.alive = true;
    
    this.x = 0;
    this.y = 0;
    
    // Apparently eliminates scope problems in timers/event handlers
    //var self = this;
}

Andy.prototype.setX = function (x) {
    if ( x < 0 ) {
        x = 0;

    } else if ( x > GAMEWIDTH - this.width ) {
        x = GAMEWIDTH - this.width;
    }
    
    this.x = x;
    $D.setStyle(this.div, 'left', x+'px');
}

Andy.prototype.setY = function (y) {
    if ( y < 0 ) {
        y = 0;
    } else if ( y > GAMEHEIGHT - this.height ) {
        y = GAMEHEIGHT - this.height;
    }
    
    this.y = y;
    $D.setStyle(this.div, 'top', y+'px');
}

Andy.prototype.left = function () {
    this.setX( this.x - 15 );
    //this.hitAnything();
}

Andy.prototype.right = function () {
    this.setX( this.x + 15 );
    //this.hitAnything();
}

Andy.prototype.up = function () {
    this.setY( this.y - 15 );
    //this.hitAnything();
}

Andy.prototype.down = function () {
    this.setY( this.y + 15 );
    //this.hitAnything();
}

Andy.prototype.collidesWith = function (sprite) {
    return ( sprite.x + sprite.width > this.x + (this.width * 0.25) && sprite.x < this.x + (this.width * 0.75) && sprite.y + sprite.height > this.y + (this.height * 0.25) && sprite.y < this.y + (this.height * 0.75) );
}

Andy.prototype.hitAnything = function () {
    var i;
    for (i = 0; i < badguys.length; i++) {
        if (badguys[i].alive && this.collidesWith(badguys[i])) {
            badguys[i].kill(true);
            // Prevents bug of killing two key holders at the same time
            // which screws up the question and answer system.
            if (badguys[i].hasKey()) {
                break;
            }
        }
    }
}