summaryrefslogtreecommitdiff
path: root/noncore/games/sfcave-sdl/player.cpp
blob: b491e533cf394541275bba0d929e9240be16a216 (plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <SDL/SDL.h>
#include <SDL/SDL_gfxPrimitives.h>

#include "constants.h"
#include "player.h"
#include "random.h"
#include "animatedimage.h"

Player :: Player( int w, int h )
{
	sWidth = w;
	sHeight = h;

	thrust = 0.4;
	gravity = 0.6;
	maxUpSpeed = 4.0;
	maxDownSpeed = 5.0;

	explosion = new AnimatedImage( IMAGES_PATH "explosion.bmp", 15 );
	init();
}

Player :: ~Player()
{
	if ( explosion )
		delete explosion;
}

void Player :: init()
{
	// Set player position
	pos.x( 50 );
	pos.y( sWidth/2 );
	pos.h( 2 );
	pos.w( 4 );
	currentThrust = 0;
	crashing = false;
	crashLineLength = 0;
	crashed = false;
	explosion->reset();
	allFaded = false;
	expNextFrame = false;

	// Reset Trail
	for ( int i = 0 ; i < TRAILSIZE ; ++i )
	{
		trail[i].x( -1 );
		trail[i].y( 0 );
		trail[i].w( 2 );
		trail[i].h( 2 );
	}
}

void Player :: draw( SDL_Surface *screen )
{
	if ( !crashing )
	{
		// Draw Player
//		ellipseRGBA( screen, pos.x(), pos.y(), pos.x()+ pos.width(), pos.y()+pos.height(), 0, 255, 255, 255 );
		filledEllipseRGBA( screen, pos.x() + pos.w(), pos.y(), pos.w(), pos.h(), 0, 255, 255, 255 );

		// Draw Trail
		drawTrails( screen );
	}
	else
	{
		drawTrails( screen );

		if ( !crashed )
			explosion->draw( screen, pos.x(), pos.y() );
	}
}

void Player :: drawTrails( SDL_Surface *screen )
{
	if ( allFaded && crashing )
		return;

	for ( int i = 0 ; i < TRAILSIZE ; ++i )
	{
		if ( trail[i].x() >= 0 )
		{
			int c = (int)((150.0/50) * (50.0 - (pos.x() - trail[i].x() ) ));
			boxRGBA( screen, trail[i].x(), trail[i].y(), trail[i].x() + 2, trail[i].y() + 2, 255, (int)(1.5*c), 0, c );
		}
	}
}

void Player :: move( bool up )
{
    // Find enpty trail and move others
	moveTrails();

	if ( up )
		currentThrust -= thrust;
	else
		currentThrust += gravity;

	if ( currentThrust > maxDownSpeed )
		currentThrust = maxDownSpeed;
	else if ( currentThrust < -maxUpSpeed )
		currentThrust = -maxUpSpeed;

	pos.moveBy( 0, (int)(currentThrust) );
}

void Player :: moveTrails()
{
    bool done = false;
	bool stillVisible = false;

	// Dont do anything here if all faded when were crashing
	if ( allFaded && crashing )
		return;

	for ( int i = 0 ; i < TRAILSIZE ; ++i )
    {
        if ( trail[i].x() < 0 )
        {
			stillVisible = true;
            if ( !crashing && !done )
            {
                trail[i].x( pos.x() - 5 );
                trail[i].y( pos.y() );
                done = true;
            }
        }
        else
            trail[i].x( trail[i].x() - 1 );
    }

	if ( !stillVisible )
		allFaded = true;
}

bool Player :: updateCrashing()
{
	crashing = true;

	moveTrails();
	if ( expNextFrame )
	{
		expNextFrame = false;
		crashed = !explosion->nextFrame();
	}
	else
		expNextFrame = true;

	return crashed;
}

void Player :: setMovementInfo( double up, double grav, double maxUp, double maxDown )
{
	thrust = up;
	gravity = grav;
	maxUpSpeed = maxUp;
	maxDownSpeed = maxDown;
}


void Player :: incValue( int valueType )
{
    switch( valueType )
    {
        case PLAYER_THRUST:
            thrust += 0.1;
            break;
        case PLAYER_GRAVITY:
            gravity += 0.1;
            break;
        case PLAYER_MAX_SPEED_UP:
            maxUpSpeed += 0.1;
             break;
       case PLAYER_MAX_SPEED_DOWN:
            maxDownSpeed += 0.1;
            break;
    }
}

void Player :: decValue( int valueType )
{
    switch( valueType )
    {
        case PLAYER_THRUST:
            thrust -= 0.1;
            break;
        case PLAYER_GRAVITY:
            gravity -= 0.1;
            break;
       case PLAYER_MAX_SPEED_UP:
            maxUpSpeed -= 0.1;
            break;
        case PLAYER_MAX_SPEED_DOWN:
            maxDownSpeed -= 0.1;
            break;
    }
}

void Player :: setValue( int valueType, double val )
{    
    switch( valueType )
    {
        case PLAYER_THRUST:
            thrust = val;
            break;
        case PLAYER_GRAVITY:
            gravity = val;
            break;
       case PLAYER_MAX_SPEED_UP:
            maxUpSpeed = val;
            break;
        case PLAYER_MAX_SPEED_DOWN:
            maxDownSpeed = val;
            break;
    }
}

double Player :: getValue( int valueType )
{
    double val;
    switch( valueType )
    {
        case PLAYER_THRUST:
            val = thrust;
            break;
        case PLAYER_GRAVITY:
            val = gravity;
            break;
        case PLAYER_MAX_SPEED_UP:
            val = maxUpSpeed;
            break;
        case PLAYER_MAX_SPEED_DOWN:
            val = maxDownSpeed;
            break;
    }
    
    return val;
}

string Player :: getValueTypeString( int valueType )
{
    string val;
    switch( valueType )
    {
        case PLAYER_THRUST:
            val = "thrust";
            break;
        case PLAYER_GRAVITY:
            val = "gravity";
            break;
        case PLAYER_MAX_SPEED_UP:
            val = "maxupspeed";
            break;
        case PLAYER_MAX_SPEED_DOWN:
            val = "maxdownspeed";
            break;
    }

    return val;
}

string Player :: getValueString( int valueType )
{
    char val[50];
    switch( valueType )
    {
        case PLAYER_THRUST:
            sprintf( val, "Thrust - %lf", thrust );
            break;
        case PLAYER_GRAVITY:
            sprintf( val, "Gravity - %lf", gravity );
            break;
        case PLAYER_MAX_SPEED_UP:
            sprintf( val, "Max Speed Up - %lf", maxUpSpeed );
            break;
        case PLAYER_MAX_SPEED_DOWN:
            sprintf( val, "Max Speed Down - %lf", maxDownSpeed );
            break;
    }
    
    string ret = val;
    return ret;
}