#import "Player.h"

@implementation Player

-(id) init {
    if ( (self = [super init] ) ) {
        pos = (Vector3){0.0f, 2.0f, 0.0f};
        height = 1.0f;
        activators = [[YSArray alloc] init];
    }

    return self;
}

-(void) showPos {
    char buffer[50];
    sprintf(buffer, "pos: (%.02f, %.02f, %.02f)", pos.x, pos.y, pos.z);
    DrawText(buffer, 0, 50, 20, BLACK);
}


//dont use this yet
-(void) attack {
    if ( [attachedCamera raycast] ) {
        
        //how to get the object it hit?
        [soundPlayer play];
    }
}

-(void) activate {
    int i;
    for (i = 0; i < activators->count; i++) {
        if ( [attachedCamera raycast: [activators->array[i] bbox]] ) {
            //printf("activators->array[%d] == %p\n", i, activators->array[i]);
            [(NPC *)activators->array[i] activate];
        }
    }
}

-(void) moveForward {
    Camera *cam = [attachedCamera addressOfCamera];
    Vector3 forward = GetCameraForward( cam );
    float distance = 0.1;
    forward = Vector3Scale(forward, distance);
    pos = Vector3Add(pos, forward);
    cam->position = Vector3Add(pos, (Vector3){0.0f, height, 0.0f} );
    cam->target = Vector3Add(cam->target, forward);
}

-(void) moveBack {
    Camera *cam = [attachedCamera addressOfCamera];
    Vector3 forward = GetCameraForward( cam );
    float distance = 0.1;
    forward = Vector3Scale(forward, distance);
    pos = Vector3Subtract(pos, forward);
    cam->position = Vector3Add(pos, (Vector3){0.0f, height, 0.0f} );
    cam->target = Vector3Subtract(cam->target, forward);
}

-(void) moveLeft {
    Camera *cam = [attachedCamera addressOfCamera];
    Vector3 right = GetCameraRight( cam );
    float distance = 0.1;
    right = Vector3Scale(right, distance);
    pos = Vector3Subtract(pos, right);
    cam->position = Vector3Add(pos, (Vector3){0.0f, height, 0.0f} );
    cam->target = Vector3Subtract(cam->target, right);
}

-(void) moveRight {
    Camera *cam = [attachedCamera addressOfCamera];
    Vector3 right = GetCameraRight( cam );
    float distance = 0.1;
    right = Vector3Scale(right, distance);
    pos = Vector3Add(pos, right);
    cam->position = Vector3Add(pos, (Vector3){0.0f, height, 0.0f} );
    cam->target = Vector3Add(cam->target, right);
}

@end