src/Player.cc
author terom
Sun, 07 Dec 2008 00:27:30 +0000
changeset 239 550397d9d479
parent 237 3d5465bcb67d
child 241 e95b1602d836
permissions -rw-r--r--
implement network weapon changes and fix weapon firing
218
86d22fe82b30 add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents: 217
diff changeset
     1
236
0048ba274152 move weapons definition out to Weapons.cc
terom
parents: 235
diff changeset
     2
#include "Player.hh"
0048ba274152 move weapons definition out to Weapons.cc
terom
parents: 235
diff changeset
     3
#include "Weapons.hh"
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
     4
#include "Engine.hh"
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 231
diff changeset
     5
#include "Graphics.hh"
198
8698cbb101df ja viel? lis??
ekku
parents:
diff changeset
     6
209
68cc4248a508 sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents: 208
diff changeset
     7
#include <cstdlib>
215
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
     8
#include <ClanLib/display.h>
216
a384bf0634f0 a bit better image_drawing
nireco
parents: 215
diff changeset
     9
#include <string>
218
86d22fe82b30 add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents: 217
diff changeset
    10
#include <cassert>
215
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
    11
220
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
    12
// player static state
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
    13
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
    14
CL_Surface Player::skin_surface;
215
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
    15
220
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
    16
// XXX: give these better names and move elsewhere
219
ec472f8ac482 fixed walkingspeed and improved drawing
nireco
parents: 218
diff changeset
    17
const int img_num_aim = 9;
ec472f8ac482 fixed walkingspeed and improved drawing
nireco
parents: 218
diff changeset
    18
const int img_num_step = 4;
ec472f8ac482 fixed walkingspeed and improved drawing
nireco
parents: 218
diff changeset
    19
const int img_height = 9;
ec472f8ac482 fixed walkingspeed and improved drawing
nireco
parents: 218
diff changeset
    20
const int img_width = 10;
216
a384bf0634f0 a bit better image_drawing
nireco
parents: 215
diff changeset
    21
198
8698cbb101df ja viel? lis??
ekku
parents:
diff changeset
    22
Player::Player(GameState &state, Vector position, bool visible) : 
236
0048ba274152 move weapons definition out to Weapons.cc
terom
parents: 235
diff changeset
    23
    PhysicsObject(state.world, PLAYER_MASS, position, Vector(0, 0)), state(state), visible(visible),
239
550397d9d479 implement network weapon changes and fix weapon firing
terom
parents: 237
diff changeset
    24
    weapons(buildWeaponsList()), selectedWeapon(0), 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
    25
{
236
0048ba274152 move weapons definition out to Weapons.cc
terom
parents: 235
diff changeset
    26
    // XXX: populate weapons from somewhere else
0048ba274152 move weapons definition out to Weapons.cc
terom
parents: 235
diff changeset
    27
220
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
    28
    // 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
    29
    // XXX: these dimensions are incorrect...
198
8698cbb101df ja viel? lis??
ekku
parents:
diff changeset
    30
    std::vector<Vector> shape(4);
8698cbb101df ja viel? lis??
ekku
parents:
diff changeset
    31
    shape[0] = Vector(0,-9);
8698cbb101df ja viel? lis??
ekku
parents:
diff changeset
    32
    shape[1] = Vector(6,0);
8698cbb101df ja viel? lis??
ekku
parents:
diff changeset
    33
    shape[2] = Vector(0,9); 
8698cbb101df ja viel? lis??
ekku
parents:
diff changeset
    34
    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
    35
198
8698cbb101df ja viel? lis??
ekku
parents:
diff changeset
    36
    // Initialize the shape of the player (salmiakki shape)
8698cbb101df ja viel? lis??
ekku
parents:
diff changeset
    37
    setShape(shape);
220
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
    38
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
    39
    // XXX: this should be a PhysicsObject constructor arg
198
8698cbb101df ja viel? lis??
ekku
parents:
diff changeset
    40
    collision_elasticity = PLAYER_COLLISION_ELASTICITY;
8698cbb101df ja viel? lis??
ekku
parents:
diff changeset
    41
}
236
0048ba274152 move weapons definition out to Weapons.cc
terom
parents: 235
diff changeset
    42
209
68cc4248a508 sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents: 208
diff changeset
    43
void Player::handleDig (Vector position, float radius) {
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    44
    // XXX: clean this bit up
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    45
    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
    46
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    47
    Vector shotVelocity = getDirection() * shotspeed;
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    48
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    49
    new Projectile(state, 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
    50
}
68cc4248a508 sneak in some ugly ugly ugly Multiple Inheritance that hopefully nobody ever notices
terom
parents: 208
diff changeset
    51
236
0048ba274152 move weapons definition out to Weapons.cc
terom
parents: 235
diff changeset
    52
void Player::handleCreateProjectile (Weapon *weapon, Vector position, Vector velocity) {
0048ba274152 move weapons definition out to Weapons.cc
terom
parents: 235
diff changeset
    53
    new Projectile(state, position, velocity, true, weapon->getExplosionRadius());
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
    54
}
237
3d5465bcb67d tweak weapon params and improve weapon changing
terom
parents: 236
diff changeset
    55
        
3d5465bcb67d tweak weapon params and improve weapon changing
terom
parents: 236
diff changeset
    56
void Player::handleChangeWeapon (unsigned int weaponIndex) {
3d5465bcb67d tweak weapon params and improve weapon changing
terom
parents: 236
diff changeset
    57
    assert(weaponIndex < weapons.size());
3d5465bcb67d tweak weapon params and improve weapon changing
terom
parents: 236
diff changeset
    58
3d5465bcb67d tweak weapon params and improve weapon changing
terom
parents: 236
diff changeset
    59
    selectedWeapon = weaponIndex;
3d5465bcb67d tweak weapon params and improve weapon changing
terom
parents: 236
diff changeset
    60
}
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
    61
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    62
void Player::printDebugInfo (void) {
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    63
    Engine::log(DEBUG, "layer.debug") << "In air: " << this->inAir;
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    64
}
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
void Player::tick (TimeMS dt) {
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    67
    // let PhysicsObject execute
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    68
    PhysicsObject::tick(dt);
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    69
    
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    70
    // tick current weapon reload
236
0048ba274152 move weapons definition out to Weapons.cc
terom
parents: 235
diff changeset
    71
    if (getCurrentWeapon())
0048ba274152 move weapons definition out to Weapons.cc
terom
parents: 235
diff changeset
    72
        getCurrentWeapon()->tickReload(dt);
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    73
}
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
    74
236
0048ba274152 move weapons definition out to Weapons.cc
terom
parents: 235
diff changeset
    75
void LocalPlayer::fireWeapon (Weapon *weapon) {
223
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    76
    // update reload timer
236
0048ba274152 move weapons definition out to Weapons.cc
terom
parents: 235
diff changeset
    77
    weapon->reload();
223
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    78
    
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    79
    // calculate new position and velocity
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    80
    Vector shotPosition = position + getDirection() * PROJECTILE_START_DISTANCE;
236
0048ba274152 move weapons definition out to Weapons.cc
terom
parents: 235
diff changeset
    81
    Vector shotVelocity = velocity + getDirection() * weapon->getSpeed();
223
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    82
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    83
    // execute
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    84
    handleCreateProjectile(weapon, shotPosition, shotVelocity);
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    85
}
237
3d5465bcb67d tweak weapon params and improve weapon changing
terom
parents: 236
diff changeset
    86
        
3d5465bcb67d tweak weapon params and improve weapon changing
terom
parents: 236
diff changeset
    87
void LocalPlayer::changeWeapon (int delta) {
3d5465bcb67d tweak weapon params and improve weapon changing
terom
parents: 236
diff changeset
    88
    // need to handle negative deltas
3d5465bcb67d tweak weapon params and improve weapon changing
terom
parents: 236
diff changeset
    89
    handleChangeWeapon((weapons.size() + selectedWeapon + delta) % weapons.size());
3d5465bcb67d tweak weapon params and improve weapon changing
terom
parents: 236
diff changeset
    90
}
223
2fcaf54ed37b basic network-projectiles
terom
parents: 222
diff changeset
    91
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    92
void LocalPlayer::handleInput (PlayerInput input) {
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    93
    // Movement force, vertical is always zero
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    94
    Vector move_force = Vector(0, 0); 
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
    95
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    96
    // Crosshair angle change
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    97
    float aim_delta = 0; 
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
    98
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
    99
    // handle movement left/right by applying a horizontal force, but limit the player's speed
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
   100
    // also update facing if needed
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   101
    if ((input & INPUT_MOVE_LEFT) && (velocity.x > -PLAYER_MAX_SPEED)) {
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   102
        setFacing(false);
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   103
        move_force.x -= PLAYER_MOVE_FORCE;
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
    }
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   106
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   107
    if ((input & INPUT_MOVE_RIGHT) && (velocity.x < PLAYER_MAX_SPEED)) {
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   108
        setFacing(true);
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   109
        move_force.x += PLAYER_MOVE_FORCE;
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   110
    }
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   111
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   112
    // handle aim by creating a aim angle delta
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   113
    if (input & INPUT_AIM_UP)
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   114
        aim_delta += CROSSHAIR_ANGLE_SPEED;
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   115
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   116
    if (input & INPUT_AIM_DOWN)
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   117
        aim_delta -= CROSSHAIR_ANGLE_SPEED;
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   118
    
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   119
    // handle jumping by invoking the jump method
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   120
    // 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
   121
    if (input & INPUT_JUMP) {
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   122
        if (input & INPUT_MOVE_LEFT)
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
   123
            jump(-1);
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   124
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   125
        else if (input & INPUT_MOVE_RIGHT)
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
   126
            jump(1);
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   127
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
   128
        else
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
   129
            jump(0);
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
   130
    }
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   131
    
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   132
    // outsource digging to Player::handleDig, since this modifies the Terrain and Network needs to know
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
   133
    if (input & INPUT_DIG)
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   134
        handleDig(position, 15);
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   135
    
237
3d5465bcb67d tweak weapon params and improve weapon changing
terom
parents: 236
diff changeset
   136
    // change weapon back/forth
3d5465bcb67d tweak weapon params and improve weapon changing
terom
parents: 236
diff changeset
   137
    if (input & INPUT_CHANGE_PREV)
3d5465bcb67d tweak weapon params and improve weapon changing
terom
parents: 236
diff changeset
   138
        changeWeapon(-1);
3d5465bcb67d tweak weapon params and improve weapon changing
terom
parents: 236
diff changeset
   139
3d5465bcb67d tweak weapon params and improve weapon changing
terom
parents: 236
diff changeset
   140
    if (input & INPUT_CHANGE_NEXT)
3d5465bcb67d tweak weapon params and improve weapon changing
terom
parents: 236
diff changeset
   141
        changeWeapon(+1);
213
5bd5afab4673 some stuff to Weapon and shoot
nireco
parents: 212
diff changeset
   142
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   143
    // validate shoot events, and then outsource to handleShoot so Network can intercept it
236
0048ba274152 move weapons definition out to Weapons.cc
terom
parents: 235
diff changeset
   144
    if ((input & INPUT_SHOOT) && getCurrentWeapon()->canShoot())
0048ba274152 move weapons definition out to Weapons.cc
terom
parents: 235
diff changeset
   145
        fireWeapon(getCurrentWeapon());
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   146
    
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
   147
    // rope throw+release+changeLength
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   148
    if (input & INPUT_ROPE)
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
   149
        rope.throwRope();
225
22ecb9cb9245 Rope can be drawn.
ekku
parents: 223
diff changeset
   150
229
355e46effa41 Rope works
ekku
parents: 225
diff changeset
   151
    if (input & INPUT_UNROPE)
355e46effa41 Rope works
ekku
parents: 225
diff changeset
   152
        rope.release();
355e46effa41 Rope works
ekku
parents: 225
diff changeset
   153
231
5085764e1dbb Rope length changing possible
ekku
parents: 229
diff changeset
   154
    if (input & INPUT_ROPE_UP)
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
   155
        rope.changeLength(-ROPE_GROWTH_RATE);
231
5085764e1dbb Rope length changing possible
ekku
parents: 229
diff changeset
   156
5085764e1dbb Rope length changing possible
ekku
parents: 229
diff changeset
   157
    if (input & INPUT_ROPE_DOWN)
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
   158
        rope.changeLength(ROPE_GROWTH_RATE);
231
5085764e1dbb Rope length changing possible
ekku
parents: 229
diff changeset
   159
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
   160
    // XXX: how should this be written? What does this do? Probably broken under network play
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   161
    if (move_force.x != 0) 
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   162
        animation_step = (animation_step + 1) % img_num_step;
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
   163
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   164
    // apply aim delta
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   165
    if (aim_delta)
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   166
        changeAim(aim_delta);
219
ec472f8ac482 fixed walkingspeed and improved drawing
nireco
parents: 218
diff changeset
   167
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   168
    // apply force
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   169
    if (!move_force.zero())
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   170
        applyForce(move_force);
199
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
   171
}
f5c86420facd Jeejee, hirvee hinaus ohi toistaseks.
saiam
parents: 198
diff changeset
   172
236
0048ba274152 move weapons definition out to Weapons.cc
terom
parents: 235
diff changeset
   173
Weapon* Player::getCurrentWeapon() {
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
   174
    return weapons[selectedWeapon % weapons.size()];
212
4389c1e6b9b8 Weapon.cc&hh
nireco
parents: 211
diff changeset
   175
}
215
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   176
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 231
diff changeset
   177
void Player::draw (Graphics *g) {
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 231
diff changeset
   178
    CL_GraphicContext *gc = g->get_gc();
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 231
diff changeset
   179
216
a384bf0634f0 a bit better image_drawing
nireco
parents: 215
diff changeset
   180
    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
   181
    int step_img_idx = animation_step%img_num_step;
217
de56d9d16705 and yet better image drawing
nireco
parents: 216
diff changeset
   182
220
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
   183
    // 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
   184
    if (!skin_loaded) {
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
   185
        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
   186
        skin_loaded = true;
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
   187
    }
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   188
    
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   189
    // XXX: this logic looks weird
218
86d22fe82b30 add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents: 217
diff changeset
   190
    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
   191
86d22fe82b30 add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents: 217
diff changeset
   192
    if (!getFacing()) {
86d22fe82b30 add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents: 217
diff changeset
   193
        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
   194
    }
de56d9d16705 and yet better image drawing
nireco
parents: 216
diff changeset
   195
220
1c92222af6d3 use Player static vars for texture loading, and load from the PROJECT_DATA_DIR path
terom
parents: 219
diff changeset
   196
    skin_surface.draw_subpixel(
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   197
            CL_Rectf(
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   198
                1 + step_img_idx * img_width, 
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   199
                aim_img_idx * img_height + 1, 
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   200
                1 + (1 + step_img_idx) * img_width, 
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   201
                (aim_img_idx + 1) * img_height + 1
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   202
            ), 
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   203
            destination, gc
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 220
diff changeset
   204
    );
217
de56d9d16705 and yet better image drawing
nireco
parents: 216
diff changeset
   205
    
215
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   206
    const uint16_t chlen = 10;
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   207
    uint16_t x = position.x;
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   208
    uint16_t y = position.y;
218
86d22fe82b30 add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents: 217
diff changeset
   209
    
86d22fe82b30 add static Engine::graphicsEnabled function and disable player graphics loading when false
terom
parents: 217
diff changeset
   210
    // draw "crosshair"
215
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   211
    if (facingRight) {
216
a384bf0634f0 a bit better image_drawing
nireco
parents: 215
diff changeset
   212
        gc->draw_line(x + std::cos(aim)*chlen/2, 
a384bf0634f0 a bit better image_drawing
nireco
parents: 215
diff changeset
   213
                      y - std::sin(aim)*chlen/2,
215
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   214
                      x + std::cos(aim)*chlen,
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   215
                      y - std::sin(aim)*chlen,
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   216
                      CL_Color::black);
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   217
    } else {
216
a384bf0634f0 a bit better image_drawing
nireco
parents: 215
diff changeset
   218
        gc->draw_line(x - std::cos(aim)*chlen/2, 
a384bf0634f0 a bit better image_drawing
nireco
parents: 215
diff changeset
   219
                      y - std::sin(aim)*chlen/2,
215
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   220
                      x - std::cos(aim)*chlen,
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   221
                      y - std::sin(aim)*chlen,
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   222
                      CL_Color::black);
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   223
    }
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 231
diff changeset
   224
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 231
diff changeset
   225
    rope.draw(g);
215
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   226
}
69de8d9fcc0a image is working.. well.. run it from build/src folder.
nireco
parents: 213
diff changeset
   227
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 231
diff changeset
   228
void LocalPlayer::draw (Graphics *g, bool displayWeapon) {
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 231
diff changeset
   229
    // superclass draw
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 231
diff changeset
   230
    Player::draw(g);
219
ec472f8ac482 fixed walkingspeed and improved drawing
nireco
parents: 218
diff changeset
   231
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 231
diff changeset
   232
    // display weapon name?
236
0048ba274152 move weapons definition out to Weapons.cc
terom
parents: 235
diff changeset
   233
    if (displayWeapon && getCurrentWeapon()) {
0048ba274152 move weapons definition out to Weapons.cc
terom
parents: 235
diff changeset
   234
        const std::string weaponName = getCurrentWeapon()->getName();
0048ba274152 move weapons definition out to Weapons.cc
terom
parents: 235
diff changeset
   235
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 231
diff changeset
   236
        g->getSimpleFont().draw(
236
0048ba274152 move weapons definition out to Weapons.cc
terom
parents: 235
diff changeset
   237
                position.x - g->getSimpleFont().get_width(weaponName) / 2,
237
3d5465bcb67d tweak weapon params and improve weapon changing
terom
parents: 236
diff changeset
   238
                position.y - 20,
236
0048ba274152 move weapons definition out to Weapons.cc
terom
parents: 235
diff changeset
   239
                weaponName,
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 231
diff changeset
   240
                g->get_gc()
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 231
diff changeset
   241
        );
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 231
diff changeset
   242
    }
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 231
diff changeset
   243
}
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
   244