// Trader logic

#include "RunnerCommon.as"

void onInit( CMovement@ this )
{
    RunnerMoveVars moveVars;
    //walking vars
    moveVars.walkSpeed = 1.0f;
    moveVars.walkSpeedInAir = 1.0f;
    moveVars.walkFactor = 1.0f;
    moveVars.walkLadderSpeed.Set( 0.05f, 0.3f );
    //jumping vars
    moveVars.jumpMaxVel = 1.5f;
    moveVars.jumpStart = 0.2f;
    moveVars.jumpMid = 0.2f;
    moveVars.jumpEnd = 0.1f;
    moveVars.jumpFactor = 0.4f;
    moveVars.jumpCount = 0;
    moveVars.overallScale = 1.0f;
    moveVars.stoppingForce = 0.35f; //function of mass
    moveVars.stoppingForceAir = 0.3f; //function of mass
    this.getBlob().set( "moveVars", moveVars );
    this.getBlob().getShape().getVars().waterDragScale = 20.0f;
}

void onInit( CBlob@ this )
{
    //no spinning
    this.getShape().SetRotationsAllowed(false);
    this.set_f32("gib health", -1.5f);
    this.Tag("flesh");
    this.getBrain().server_SetActive( true );
}

void onReload( CSprite@ this )
{
    this.getConsts().filename = this.getBlob().getSexNum() == 0 ?
                                "Entities/Special/WAR/Trading/TraderMale.png" :
                                "Entities/Special/WAR/Trading/TraderFemale.png";
}


void onTick( CBlob@ this )
{
    if (this.getHealth() < 1 && !this.hasTag("dead"))
    {
        this.Tag("dead");
        this.server_SetTimeToDie( 20 );
    }

    if (this.getHealth() < 1.5)
    {
        //this.getSprite().Gib();
        //this.server_Die();
        //return;
        this.server_SetHealth(1.5);
    }

    CSprite@ sprite = this.getSprite();

    // set dead animations

    if (this.hasTag("dead"))
    {
        sprite.SetAnimation("dead");

        if (this.isOnGround()) {
            sprite.SetFrameIndex( 1 );
        }
        else {
            sprite.SetFrameIndex( 0 );
        }

        return;
    }

    // set animations
    Vec2f pos = this.getPosition();
    Vec2f aimpos = this.getAimPos();
    bool facingLeft = this.getVelocity().x < 0.0f;
    this.SetFacingLeft( facingLeft );
    bool ended = sprite.isAnimationEnded();

    if ((this.isKeyPressed(key_left) || this.isKeyPressed(key_right)) ||
            (this.isOnLadder() && (this.isKeyPressed(key_up) || this.isKeyPressed(key_down)) ) ) {
        sprite.SetAnimation("walk");
    }
    else if (ended) {
        sprite.SetAnimation("default");
    }
}

void onGib(CSprite@ this)
{
    print("TODO TRADER GIBBING");
    CBlob@ blob = this.getBlob();
    Vec2f pos = blob.getPosition();
    Vec2f vel = blob.getVelocity();
	vel.y -= 3.0f;
    f32 hp = Maths::Min(Maths::Abs(blob.getHealth()),2.0f) + 1.5;
    CParticle@ Gib1     = makeGibParticle( "Entities/Special/WAR/Trading/TraderGibs.png", pos, vel + getRandomVelocity( 90, hp , 80 ), 0, 0, Vec2f (16,16), 2.0f, 20, "Sounds/bodypart?.wav" );
    CParticle@ Gib2     = makeGibParticle( "Entities/Special/WAR/Trading/TraderGibs.png", pos, vel + getRandomVelocity( 90, hp - 0.2 , 80 ), 1, 0, Vec2f (16,16), 2.0f, 20, "Sounds/bodypart?.wav" );
    CParticle@ Gib3     = makeGibParticle( "Entities/Special/WAR/Trading/TraderGibs.png", pos, vel + getRandomVelocity( 90, hp , 80 ), 2, 0, Vec2f (16,16), 2.0f, 0, "Sounds/bodypart?.wav" );
}

bool canBePickedUp( CBlob@ this, CBlob@ byBlob )
{
    return (!this.hasTag("at post"));
}
