src/GameState.cc
author ekku
Thu, 04 Dec 2008 19:53:16 +0000
changeset 196 e2d32c4601ce
parent 195 53feb13a3d1c
child 199 f5c86420facd
permissions -rw-r--r--
kaikki hajoo

#include "GameState.hh"
#include "Engine.hh"
#include "Config.hh"

/**
 * shoots the selected weapon.
 * TODO: selection and weapon information
 */
void Player::shoot (void) {
    // here should be somehow considered which projectile it is
    if(!canShoot())
        return;
    reloadTimer += 0;
    Vector unitVectorAim = facingRight ? Vector(std::cos(aim), -std::sin(aim)) : 
            Vector(-std::cos(aim), -std::sin(aim));
    float shotspeed = 100*PHYSICS_TICK_MS;
    Vector shotRelativeVelocity = unitVectorAim * shotspeed;
    Vector shotVelocity = this->velocity + shotRelativeVelocity;
    this->state.addProjectile(new Shot(this->state, this->position, shotVelocity, true));
}

void Player::handleMove (PlayerInput_Move input) {
    float fx = 0; // Force in x-direction
    float da = 0; // Crosshair angle

    // handle left/right
    if ((input & INPUT_MOVE_LEFT) && (velocity.x > -PLAYER_MAX_SPEED))
        fx -= PLAYER_MOVE_FORCE;

    if ((input & INPUT_MOVE_RIGHT) && (velocity.x < PLAYER_MAX_SPEED))
        fx += PLAYER_MOVE_FORCE;

    if (input & INPUT_MOVE_UP)
        da += CROSSHAIR_ANGLE_SPEED;

    if (input & INPUT_MOVE_DOWN)
        da -= CROSSHAIR_ANGLE_SPEED;

    if (input & INPUT_MOVE_JUMP) {
        if ((input & INPUT_MOVE_LEFT))
            jump(-1);
        else if ((input & INPUT_MOVE_RIGHT))
            jump(1);
        else
            jump(0);
    }

    if (input & INPUT_MOVE_DIG) {
        // Should create Shot which destroys ground, but also should be destroyed then,
        // but it doesn't.
        // But this now just segfaults
//        world.addObject(new Shot(state, position, true));

        world.removeGround(position, 15);
    }

    if (input & INPUT_SHOOT) {
        this->shoot();
    }



    // Player facing
    if (fx < 0) setFacing(false);
    else if (fx > 0) setFacing(true);


    this->changeAim(da); // Move crosshair

    // Apply force
    applyForce(Vector(fx, 0));

}

void Player::debugInfo (void) {
    Engine::log(DEBUG, "Player.debugInfo") << "In air: " << this->inAir;
}

void Shot::onCollision() {
    world.removeGround(position, 20);
    this->destroyed = true;
}

bool Shot::isDestroyed (void) {
    return this->destroyed;
}

void Shot::draw(CL_GraphicContext *gc) {


    CL_Quad player(
                   (int)((position).x+1), (int)((position).y+1),
                   (int)((position).x-1), (int)((position).y+1),
                   (int)((position).x+1), (int)((position).y-1),
                   (int)((position).x-1), (int)((position).y-1)
                   );

    gc->fill_quad(player, CL_Color::green);

    const uint16_t chlen = 10;
    uint16_t x = player.center().x;
    uint16_t y = player.center().y;
    if(target_visible) {
        if (facingRight) {
            gc->draw_line(x, y,
                          x + std::cos(aim)*chlen,
                          y - std::sin(aim)*chlen,
                          CL_Color::black);
        } else {
            gc->draw_line(x, y,
                          x - std::cos(aim)*chlen,
                          y - std::sin(aim)*chlen,
                          CL_Color::black);
        }
    }
}