summaryrefslogtreecommitdiff
path: root/noncore/games/sfcave-sdl/player.cpp
Unidiff
Diffstat (limited to 'noncore/games/sfcave-sdl/player.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/sfcave-sdl/player.cpp162
1 files changed, 162 insertions, 0 deletions
diff --git a/noncore/games/sfcave-sdl/player.cpp b/noncore/games/sfcave-sdl/player.cpp
new file mode 100644
index 0000000..830ee78
--- a/dev/null
+++ b/noncore/games/sfcave-sdl/player.cpp
@@ -0,0 +1,162 @@
1#include <SDL.h>
2#include "SDL_gfxPrimitives.h"
3
4#include "constants.h"
5#include "player.h"
6#include "random.h"
7#include "animatedimage.h"
8
9Player :: Player( int w, int h )
10{
11 sWidth = w;
12 sHeight = h;
13
14 thrustUp = 0.4;
15 thrustDown = 0.6;
16 maxUpSpeed = 4.0;
17 maxDownSpeed = 5.0;
18
19 explosion = new AnimatedImage( IMAGES_PATH "explosion.bmp", 15 );
20 init();
21}
22
23Player :: ~Player()
24{
25 if ( explosion )
26 delete explosion;
27}
28
29void Player :: init()
30{
31 // Set player position
32 pos.x( 50 );
33 pos.y( sWidth/2 );
34 pos.h( 2 );
35 pos.w( 4 );
36 thrust = 0;
37 crashing = false;
38 crashLineLength = 0;
39 crashed = false;
40 explosion->reset();
41 allFaded = false;
42 expNextFrame = false;
43
44 // Reset Trail
45 for ( int i = 0 ; i < TRAILSIZE ; ++i )
46 {
47 trail[i].x( -1 );
48 trail[i].y( 0 );
49 trail[i].w( 2 );
50 trail[i].h( 2 );
51 }
52}
53
54void Player :: draw( SDL_Surface *screen )
55{
56 if ( !crashing )
57 {
58 // Draw Player
59 // ellipseRGBA( screen, pos.x(), pos.y(), pos.x()+ pos.width(), pos.y()+pos.height(), 0, 255, 255, 255 );
60 filledEllipseRGBA( screen, pos.x() + pos.w(), pos.y(), pos.w(), pos.h(), 0, 255, 255, 255 );
61
62 // Draw Trail
63 drawTrails( screen );
64 }
65 else
66 {
67 drawTrails( screen );
68
69 if ( !crashed )
70 explosion->draw( screen, pos.x(), pos.y() );
71 }
72}
73
74void Player :: drawTrails( SDL_Surface *screen )
75{
76 if ( allFaded && crashing )
77 return;
78
79 for ( int i = 0 ; i < TRAILSIZE ; ++i )
80 {
81 if ( trail[i].x() >= 0 )
82 {
83 // int r = (int) ((255.0/pos.x()) * (trail[i].x));
84 // int g = (int) ((150.0/pos.x()) * (trail[i].x));
85 int c = (int)((150.0/50) * (50.0 - (pos.x() - trail[i].x() ) ));
86 // SDL_FillRect( screen, &trail[i], SDL_MapRGBA( screen->format, r, g, 0, 0 ) ); //(int)(1.5*c), 0, 255 ) );
87 boxRGBA( screen, trail[i].x(), trail[i].y(), trail[i].x() + 2, trail[i].y() + 2, 255, (int)(1.5*c), 0, c );
88 }
89 }
90}
91
92void Player :: move( bool up )
93{
94 // Find enpty trail and move others
95 moveTrails();
96
97 if ( up )
98 thrust -= thrustUp;
99 else
100 thrust += thrustDown;
101
102 if ( thrust > maxDownSpeed )
103 thrust = maxDownSpeed;
104 else if ( thrust < -maxUpSpeed )
105 thrust = -maxUpSpeed;
106
107 pos.moveBy( 0, (int)(thrust) );
108}
109
110void Player :: moveTrails()
111{
112 bool done = false;
113 bool stillVisible = false;
114
115 // Dont do anything here if all faded when were crashing
116 if ( allFaded && crashing )
117 return;
118
119 for ( int i = 0 ; i < TRAILSIZE ; ++i )
120 {
121 if ( trail[i].x() < 0 )
122 {
123 stillVisible = true;
124 if ( !crashing && !done )
125 {
126 trail[i].x( pos.x() - 5 );
127 trail[i].y( pos.y() );
128 done = true;
129 }
130 }
131 else
132 trail[i].x( trail[i].x() - 1 );
133 }
134
135 if ( !stillVisible )
136 allFaded = true;
137}
138
139bool Player :: updateCrashing()
140{
141 crashing = true;
142
143 moveTrails();
144 if ( expNextFrame )
145 {
146 expNextFrame = false;
147 crashed = !explosion->nextFrame();
148 }
149 else
150 expNextFrame = true;
151
152 return crashed;
153}
154
155void Player :: setMovementInfo( double up, double down, double maxUp, double maxDown )
156{
157 thrustUp = up;
158 thrustDown = down;
159 maxUpSpeed = maxUp;
160 maxDownSpeed = maxDown;
161}
162