File: Player.cpp

package info (click to toggle)
between 6%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, stretch
  • size: 3,532 kB
  • sloc: cpp: 28,110; php: 718; ansic: 638; objc: 245; sh: 236; makefile: 99; perl: 67
file content (152 lines) | stat: -rw-r--r-- 3,688 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "Player.h"

#include "common.h"



Player::Player()
        : mShiftingPosition( false ) {

    const char *imageFileName = "player.tga";
    

    mSprite = new Sprite( imageFileName, true, 16, 4 );
    
    Image *spriteImage = readTGA( imageFileName );

    
    Image *whiteImage = spriteImage->copy();
    Image *brownImage = spriteImage->copy();
    
    // change all black pixels to white or light brown
    Color black( 0, 0, 0 );
    Color white( 1, 1, 1 );
    Color lightBrown( 191.0/255, 136.0/255, 0 );
    
    
    // change all dark grey pixels to light gray or dark brown 
    Color darkGrey( 54.0/255, 54.0/255, 54.0/255 );
    Color lightGrey( 182.0/255, 182.0/255, 182.0/255 );
    Color darkBrown( 141.0/255, 100.0/255, 0 );
    

    int w = spriteImage->getWidth();
    int h = spriteImage->getHeight();
    
    int numPixels = w * h;
    
    for( int i=0; i<numPixels; i++ ) {
        Color c = spriteImage->getColor( i );
        
        if( c.equals( &black ) ) {
            whiteImage->setColor( i, white );
            brownImage->setColor( i, lightBrown );
            }
        else if( c.equals( &darkGrey ) ) {
            whiteImage->setColor( i, lightGrey );
            brownImage->setColor( i, darkBrown );
            }
        }
    mWhiteSprite = new Sprite( whiteImage, true, 16, 4 );
    mBrownSprite = new Sprite( brownImage, true, 16, 4 );
    
    delete whiteImage;
    delete brownImage;
    
    delete spriteImage;
    
    
    mSprites[0] = mSprite;
    mSprites[1] = mWhiteSprite;
    mSprites[2] = mBrownSprite;
    }

        

Player::~Player() {
    delete mSprite;
    delete mWhiteSprite;
    delete mBrownSprite;
    }



void Player::shiftPosition( double inNewX, int inNumSteps ) {
    mShiftingPosition = true;
    mShiftPositionA = mX;
    mShiftPositionB = inNewX;
    mNumShiftPositionSteps = inNumSteps;
    mNumShiftPositionStepsDone = 0;
    }



void Player::step() {
    if( mShiftingPosition ) {
        
        mNumShiftPositionStepsDone ++;
    
        if( mNumShiftPositionStepsDone > mNumShiftPositionSteps ) {
            mShiftingPosition = false;
            }
        else {
            double bBlend = 
                mNumShiftPositionStepsDone / (double)mNumShiftPositionSteps;
            
            bBlend = smoothBlend( bBlend );
            
            double aBlend = 1 - bBlend;

            mX = mShiftPositionA * aBlend + mShiftPositionB * bBlend;
            }
        }
    }



void Player::draw( double inRotation, Vector3D *inPosition, 
                   double inScale,
                   double inFadeFactor,
                   Color *inColor ) {

    
    if( mX > -26.5 && mX < 24.5 ) {
        mSprite->draw( mSpriteFrame, inRotation, inPosition,
                       inScale, inFadeFactor, inColor );
        }

    if( mX < -22.5 ) {
        // fade in white sprite on top
        
        double fade = ( mX - (-22.5) ) / ( -26.5 - (-22.5) );
        
        
        if( fade > 1 ) {
            fade = 1;
            }
        

        mWhiteSprite->draw( mSpriteFrame, inRotation, inPosition,
                            inScale, 
                            fade * inFadeFactor, 
                            inColor );
        }

    if( mX > 20.5 ) {
        // fade in brown sprite on top
        
        double fade = ( mX - 20.5 ) / ( 24.5 - 20.5 );
        
        
        if( fade > 1 ) {
            fade = 1;
            }
        

        mBrownSprite->draw( mSpriteFrame, inRotation, inPosition,
                            inScale, 
                            fade * inFadeFactor, 
                            inColor );
        }
    
    }