src/Player.cc
author ekku
Sat, 06 Dec 2008 21:39:52 +0000
changeset 231 5085764e1dbb
parent 229 355e46effa41
child 233 ff4ecea83cf5
permissions -rw-r--r--
Rope length changing possible
218
86d22fe82b30 add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents: 217
diff changeset
     1
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
     2
#include "Engine.hh"
198
8698cbb101df ja viel? lis??
ekku
parents:
diff changeset
     3
#include "Player.hh" 
8698cbb101df ja viel? lis??
ekku
parents:
diff changeset
     4
209
68cc4248a508 sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents: 208
diff changeset
     5
#include <cstdlib>
215
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
     6
#include <ClanLib/display.h>
216
a384bf0634f0 a bit better image_drawing
nireco
parents: 215
diff changeset
     7
#include <string>
218
86d22fe82b30 add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents: 217
diff changeset
     8
#include <cassert>
215
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
     9
220
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
    10
// player static state
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
    11
bool Player::skin_loaded = false;
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
    12
CL_Surface Player::skin_surface;
215
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
    13
220
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
    14
// XXX: give these better names and move elsewhere
219
ec472f8ac482 fixed walkingspeed and improved drawing
nireco
parents: 218
diff changeset
    15
const int img_num_aim = 9;
ec472f8ac482 fixed walkingspeed and improved drawing
nireco
parents: 218
diff changeset
    16
const int img_num_step = 4;
ec472f8ac482 fixed walkingspeed and improved drawing
nireco
parents: 218
diff changeset
    17
const int img_height = 9;
ec472f8ac482 fixed walkingspeed and improved drawing
nireco
parents: 218
diff changeset
    18
const int img_width = 10;
216
a384bf0634f0 a bit better image_drawing
nireco
parents: 215
diff changeset
    19
198
8698cbb101df ja viel? lis??
ekku
parents:
diff changeset
    20
Player::Player(GameState &state, Vector position, bool visible) : 
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
    21
    PhysicsObject(state.world, PLAYER_MASS, position, Vector(0, 0)), state(state), visible(visible), arsenal(),
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
    22
    selectedWeapon(0), changing(false), animation_step(0), rope(*this) 
222
293ddf4c067d reorganize PhysicsObject/Player/Projectile lists so that PhysicsObject doesn't need to know about its subclasses anymore, and PhysicsWorld doesn't need to know about GameState
ekku
parents: 221
diff changeset
    23
{
212
4389c1e6b9b8 Weapon.cc&hh
nireco
parents: 211
diff changeset
    24
    // TODO: arsenal's size should be affected by some value
4389c1e6b9b8 Weapon.cc&hh
nireco
parents: 211
diff changeset
    25
    // and weapons should be loaded from somewhere, not generated here
218
86d22fe82b30 add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents: 217
diff changeset
    26
    for (int i = 0; i < 5; i++) {
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    27
        arsenal.push_back(Weapon(state, 10000, (5 - i) * 40 + 30, i * 6 + 5, i * 100 + 50, "asdf"));
212
4389c1e6b9b8 Weapon.cc&hh
nireco
parents: 211
diff changeset
    28
    }
218
86d22fe82b30 add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents: 217
diff changeset
    29
    
220
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
    30
    // build the player's shape
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
    31
    // XXX: these dimensions are incorrect...
198
8698cbb101df ja viel? lis??
ekku
parents:
diff changeset
    32
    std::vector<Vector> shape(4);
8698cbb101df ja viel? lis??
ekku
parents:
diff changeset
    33
    shape[0] = Vector(0,-9);
8698cbb101df ja viel? lis??
ekku
parents:
diff changeset
    34
    shape[1] = Vector(6,0);
8698cbb101df ja viel? lis??
ekku
parents:
diff changeset
    35
    shape[2] = Vector(0,9); 
8698cbb101df ja viel? lis??
ekku
parents:
diff changeset
    36
    shape[3] = Vector(-6,0);
220
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
    37
198
8698cbb101df ja viel? lis??
ekku
parents:
diff changeset
    38
    // Initialize the shape of the player (salmiakki shape)
8698cbb101df ja viel? lis??
ekku
parents:
diff changeset
    39
    setShape(shape);
220
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
    40
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
    41
    // XXX: this should be a PhysicsObject constructor arg
198
8698cbb101df ja viel? lis??
ekku
parents:
diff changeset
    42
    collision_elasticity = PLAYER_COLLISION_ELASTICITY;
8698cbb101df ja viel? lis??
ekku
parents:
diff changeset
    43
}
8698cbb101df ja viel? lis??
ekku
parents:
diff changeset
    44
 
209
68cc4248a508 sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents: 208
diff changeset
    45
void Player::handleDig (Vector position, float radius) {
68cc4248a508 sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents: 208
diff changeset
    46
    Vector unitVectorAim = facingRight ? Vector(std::cos(aim), -std::sin(aim)) : 
68cc4248a508 sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents: 208
diff changeset
    47
            Vector(-std::cos(aim), -std::sin(aim));
231
5085764e1dbb Rope length changing possible
ekku
parents: 229
diff changeset
    48
    float shotspeed = 1;//0*PHYSICS_TICK_MS;
209
68cc4248a508 sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents: 208
diff changeset
    49
    Vector shotVelocity = unitVectorAim*shotspeed;
216
a384bf0634f0 a bit better image_drawing
nireco
parents: 215
diff changeset
    50
    new Projectile(this->state, this->position, shotVelocity, false, radius, 1);
209
68cc4248a508 sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents: 208
diff changeset
    51
68cc4248a508 sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents: 208
diff changeset
    52
}
68cc4248a508 sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents: 208
diff changeset
    53
223
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    54
void Player::handleCreateProjectile (Weapon &weapon, Vector position, Vector velocity) {
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    55
    new Projectile(state, position, velocity, true, weapon.getExplosionRadius());
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
    56
}
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
    57
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    58
void Player::printDebugInfo (void) {
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    59
    Engine::log(DEBUG, "layer.debug") << "In air: " << this->inAir;
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    60
}
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    61
        
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    62
void Player::tick (TimeMS dt) {
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    63
    // let PhysicsObject execute
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    64
    PhysicsObject::tick(dt);
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    65
    
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    66
    // tick current weapon reload
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    67
    getWeapon().tickReload(dt);
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    68
}
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
    69
223
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    70
void LocalPlayer::fireWeapon (Weapon &weapon) {
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    71
    // update reload timer
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    72
    weapon.reload();
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    73
    
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    74
    // calculate unit vector
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    75
    Vector unitVectorAim = (facingRight ? 
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    76
        Vector(std::cos(aim), -std::sin(aim)) : 
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    77
        Vector(-std::cos(aim), -std::sin(aim))
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    78
    );
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    79
    
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    80
    // XXX: what does the PHYSICS_TICK_MS stuff mean?
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    81
    float shotspeed = weapon.getVelocity() * PHYSICS_TICK_MS / 2;
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    82
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    83
    Vector shotRelativeVelocity = unitVectorAim * shotspeed;
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    84
    Vector shotVelocity = this->velocity + shotRelativeVelocity;
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    85
    Vector shotPosition = this->position + unitVectorAim * 10;
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    86
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    87
    // execute
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    88
    handleCreateProjectile(weapon, shotPosition, shotVelocity);
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    89
}
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    90
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    91
void LocalPlayer::handleInput (PlayerInput input) {
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    92
    // Movement force, vertical is always zero
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    93
    Vector move_force = Vector(0, 0); 
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
    94
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    95
    // Crosshair angle change
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    96
    float aim_delta = 0; 
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
    97
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    98
    // handle movement left/right by applying a horizontal force, but limit the player's speed
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    99
    if ((input & INPUT_MOVE_LEFT) && (velocity.x > -PLAYER_MAX_SPEED)) {
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   100
        setFacing(false);
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   101
        move_force.x -= PLAYER_MOVE_FORCE;
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   102
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   103
    }
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   104
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   105
    if ((input & INPUT_MOVE_RIGHT) && (velocity.x < PLAYER_MAX_SPEED)) {
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   106
        setFacing(true);
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   107
        move_force.x += PLAYER_MOVE_FORCE;
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   108
    }
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   109
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   110
    // handle aim by creating a aim angle delta
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   111
    if (input & INPUT_AIM_UP)
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   112
        aim_delta += CROSSHAIR_ANGLE_SPEED;
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   113
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   114
    if (input & INPUT_AIM_DOWN)
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   115
        aim_delta -= CROSSHAIR_ANGLE_SPEED;
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   116
    
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   117
    // handle jumping by invoking the jump method
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   118
    // XXX: the direction should ideally be given using some other method
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   119
    if (input & INPUT_JUMP) {
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   120
        if (input & INPUT_MOVE_LEFT)
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
   121
            jump(-1);
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   122
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   123
        else if (input & INPUT_MOVE_RIGHT)
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
   124
            jump(1);
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   125
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
   126
        else
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
   127
            jump(0);
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
   128
    }
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   129
    
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   130
    // outsource digging to Player::handleDig, since this modifies the Terrain and Network needs to know
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   131
    if (input & INPUT_DIG) {
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   132
        handleDig(position, 15);
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
   133
201
135616467a0a rename Shot -> Projectile
terom
parents: 200
diff changeset
   134
        // Should create Projectile which destroys ground, but also should be destroyed then,
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
   135
        // but it doesn't.
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
   136
        // But this now just segfaults
201
135616467a0a rename Shot -> Projectile
terom
parents: 200
diff changeset
   137
//        world.addObject(new Projectile(state, position, true));
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
   138
    }
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   139
    
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   140
    // XXX: currently not network safe
212
4389c1e6b9b8 Weapon.cc&hh
nireco
parents: 211
diff changeset
   141
    if (input & INPUT_CHANGE) {
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   142
        if (changing) {
213
5bd5afab4673 some stuff to Weapon and shoot
nireco
parents: 212
diff changeset
   143
5bd5afab4673 some stuff to Weapon and shoot
nireco
parents: 212
diff changeset
   144
        } else {
5bd5afab4673 some stuff to Weapon and shoot
nireco
parents: 212
diff changeset
   145
            changing = true;
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   146
            selectedWeapon = (selectedWeapon + 1) % arsenal.size();
213
5bd5afab4673 some stuff to Weapon and shoot
nireco
parents: 212
diff changeset
   147
            Engine::log(DEBUG, "Player.cc:input ") << "changed weapon " << selectedWeapon;
5bd5afab4673 some stuff to Weapon and shoot
nireco
parents: 212
diff changeset
   148
        }
5bd5afab4673 some stuff to Weapon and shoot
nireco
parents: 212
diff changeset
   149
    } else {
5bd5afab4673 some stuff to Weapon and shoot
nireco
parents: 212
diff changeset
   150
        changing = false;
212
4389c1e6b9b8 Weapon.cc&hh
nireco
parents: 211
diff changeset
   151
    }
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   152
    
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   153
    // validate shoot events, and then outsource to handleShoot so Network can intercept it
223
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
   154
    if (input & INPUT_SHOOT && getWeapon().canShoot())
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
   155
        fireWeapon(getWeapon());
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   156
    
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   157
    if (input & INPUT_ROPE)
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   158
        rope.shoot();
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   159
229
355e46effa41 Rope works
ekku
parents: 225
diff changeset
   160
    if (input & INPUT_UNROPE)
355e46effa41 Rope works
ekku
parents: 225
diff changeset
   161
        rope.release();
355e46effa41 Rope works
ekku
parents: 225
diff changeset
   162
231
5085764e1dbb Rope length changing possible
ekku
parents: 229
diff changeset
   163
    if (input & INPUT_ROPE_UP)
5085764e1dbb Rope length changing possible
ekku
parents: 229
diff changeset
   164
        rope.changeLength(true);
5085764e1dbb Rope length changing possible
ekku
parents: 229
diff changeset
   165
5085764e1dbb Rope length changing possible
ekku
parents: 229
diff changeset
   166
    if (input & INPUT_ROPE_DOWN)
5085764e1dbb Rope length changing possible
ekku
parents: 229
diff changeset
   167
        rope.changeLength(false);
5085764e1dbb Rope length changing possible
ekku
parents: 229
diff changeset
   168
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   169
    // XXX: how should this be written?
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   170
    if (move_force.x != 0) 
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   171
        animation_step = (animation_step + 1) % img_num_step;
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
   172
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   173
    // apply aim delta
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   174
    if (aim_delta)
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   175
        changeAim(aim_delta);
219
ec472f8ac482 fixed walkingspeed and improved drawing
nireco
parents: 218
diff changeset
   176
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   177
    // apply force
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   178
    if (!move_force.zero())
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   179
        applyForce(move_force);
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
   180
}
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
   181
212
4389c1e6b9b8 Weapon.cc&hh
nireco
parents: 211
diff changeset
   182
Weapon& Player::getWeapon() {
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   183
    return arsenal[selectedWeapon % arsenal.size()];
212
4389c1e6b9b8 Weapon.cc&hh
nireco
parents: 211
diff changeset
   184
}
215
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   185
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   186
void Player::draw (CL_GraphicContext *gc) {
216
a384bf0634f0 a bit better image_drawing
nireco
parents: 215
diff changeset
   187
    int aim_img_idx = (int)((1 - (getAim()+KG_PI/2)/KG_PI)*img_num_aim);
219
ec472f8ac482 fixed walkingspeed and improved drawing
nireco
parents: 218
diff changeset
   188
    int step_img_idx = animation_step%img_num_step;
217
de56d9d16705 and yet better image drawing
nireco
parents: 216
diff changeset
   189
220
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
   190
    // load skin image if not yet loaded
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
   191
    if (!skin_loaded) {
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
   192
        skin_surface = CL_Surface(PLAYER_SKIN_PATH);
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
   193
        skin_loaded = true;
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
   194
    }
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   195
    
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   196
    // XXX: this logic looks weird
218
86d22fe82b30 add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents: 217
diff changeset
   197
    CL_Rectf destination(position.x - 4, position.y - 4, position.x + 5, position.y + 4);
86d22fe82b30 add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents: 217
diff changeset
   198
86d22fe82b30 add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents: 217
diff changeset
   199
    if (!getFacing()) {
86d22fe82b30 add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents: 217
diff changeset
   200
        destination = CL_Rect(position.x + 5, position.y - 4, position.x - 4, position.y + 4);
217
de56d9d16705 and yet better image drawing
nireco
parents: 216
diff changeset
   201
    }
de56d9d16705 and yet better image drawing
nireco
parents: 216
diff changeset
   202
220
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
   203
    skin_surface.draw_subpixel(
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   204
            CL_Rectf(
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   205
                1 + step_img_idx * img_width, 
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   206
                aim_img_idx * img_height + 1, 
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   207
                1 + (1 + step_img_idx) * img_width, 
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   208
                (aim_img_idx + 1) * img_height + 1
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   209
            ), 
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   210
            destination, gc
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   211
    );
217
de56d9d16705 and yet better image drawing
nireco
parents: 216
diff changeset
   212
    
215
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   213
    const uint16_t chlen = 10;
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   214
    uint16_t x = position.x;
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   215
    uint16_t y = position.y;
218
86d22fe82b30 add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents: 217
diff changeset
   216
    
86d22fe82b30 add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents: 217
diff changeset
   217
    // draw "crosshair"
215
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   218
    if (facingRight) {
216
a384bf0634f0 a bit better image_drawing
nireco
parents: 215
diff changeset
   219
        gc->draw_line(x + std::cos(aim)*chlen/2, 
a384bf0634f0 a bit better image_drawing
nireco
parents: 215
diff changeset
   220
                      y - std::sin(aim)*chlen/2,
215
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   221
                      x + std::cos(aim)*chlen,
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   222
                      y - std::sin(aim)*chlen,
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   223
                      CL_Color::black);
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   224
    } else {
216
a384bf0634f0 a bit better image_drawing
nireco
parents: 215
diff changeset
   225
        gc->draw_line(x - std::cos(aim)*chlen/2, 
a384bf0634f0 a bit better image_drawing
nireco
parents: 215
diff changeset
   226
                      y - std::sin(aim)*chlen/2,
215
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   227
                      x - std::cos(aim)*chlen,
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   228
                      y - std::sin(aim)*chlen,
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   229
                      CL_Color::black);
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   230
    }
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   231
}
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   232
219
ec472f8ac482 fixed walkingspeed and improved drawing
nireco
parents: 218
diff changeset
   233