// vim:syntax=javascript

class Guy extends Actor
{
    static var sprite_names : Array = ["guy_up","guy_left","guy_down","guy_right"];
    static var point_sprite_names : Array = ["guy_point_up","guy_point_left","guy_point_down","guy_point_right"];
    static var GUY_SIZE : Number = 20;
    static var GUY_SPEED : Number = 2;
    static var GUY_RUN_SPEED : Number = 5;
    static var DANGER_ZONE : Number = 80;
    static var POINT_FRAMES : Number = 15;

    static var W_KEY : Number = 87;
    static var A_KEY : Number = 65;
    static var S_KEY : Number = 83;
    static var D_KEY : Number = 68;

    var _point_count : Number;
    var _point_dir : Number;

    function Guy(parent_mc : LevelClip, pos : Point) {
        super(parent_mc, sprite_names, pos.x, pos.y, Actor.DIR_RIGHT, GUY_SIZE);

        // TODO: there some real stink coming from this being duplicated from
        // Enemy
        onDeath = function () : Actor {
            return new Explosion(parent_mc, this.x+this.size/2-Explosion.EXPLOSION_SIZE/2, this.y+this.size/2-Explosion.EXPLOSION_SIZE/2);
            //return new DeadGuy(parent_mc, this.x, this.y);
        };

        var static_this = this;
        set_onPress(function () {
            if (parent_mc.canAsplode(static_this)) {
                static_this.setDead();
            }
        });

        _point_count = 0;
    }

    function doPoint(wherex:Number, wherey:Number) {
        _point_dir = vectorToMajorDir(wherex-(x+size/2), wherey-(y+size/2));

        _point_count = POINT_FRAMES;
    }

    function doFrame() {
        var next_x = x;
        var next_y = y;

        var speed = GUY_SPEED;
        if (Key.isDown(Key.SHIFT)) {
            speed = GUY_RUN_SPEED;
        }

        if (Key.isDown(W_KEY) || Key.isDown(Key.UP)) {
            next_y = int((y - speed)/speed)*speed;
        }
        if (Key.isDown(A_KEY) || Key.isDown(Key.LEFT)) {
            next_x = int((x - speed)/speed)*speed;
        }
        if (Key.isDown(S_KEY) || Key.isDown(Key.DOWN)) {
            next_y = int((y + speed)/speed)*speed;
        }
        if (Key.isDown(D_KEY) || Key.isDown(Key.RIGHT)) {
            next_x = int((x + speed)/speed)*speed;
        }

        if (_point_count > 0) {
            if (_point_count == POINT_FRAMES) {
                if (next_x != x || next_y != y) {
                    _point_dir = dir;
                }
                setImages(point_sprite_names, _point_dir);
            }

            _point_count --;
            if (_point_count == 0) {
                setImages(sprite_names, dir);
            }
        }

        if ((next_x != x || next_y != y)) {
            var next_dir = vectorToDir(next_x-x, next_y-y);

            if (canMoveTo(next_x, next_y)) {
                moveTo(next_x, next_y, next_dir);
            } else if (canMoveTo(x, next_y)) {
                moveTo(x, next_y, next_dir);
            } else if (canMoveTo(next_x, y)) {
                moveTo(next_x, y, next_dir);
            } else {
                setDirection(next_dir);
            }
        }
    }

    function isGuy() : Boolean {
        return true;
    }

    function canBeKilled() : Boolean {
        return true;
    }

}
