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
| #include "include/stresso.h"
#include "include/tile.h"
stresso::stresso()
{
//Initialisation des coordonnees
box.x = 0;
box.y = 0;
box.w = 40;
box.h = 64;
//Initialisation de la vitesse
xVel = 0;
yVel = 0;
//Initialize the velocity
//velocity=0;
//les variables d'animation : init
frame=0;
status=right;
}
void stresso::handle_input()
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_UP: yVel -= stresso_HEIGHT/2; break;
case SDLK_DOWN: yVel += stresso_HEIGHT / 2; break;
case SDLK_LEFT: xVel -= stresso_WIDTH / 2; break;
case SDLK_RIGHT: xVel += stresso_WIDTH / 2; break;
}
}
//If a key was released
else if( event.type == SDL_KEYUP )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_UP: yVel += stresso_HEIGHT/2; break;
case SDLK_DOWN: yVel -= stresso_HEIGHT / 2; break;
case SDLK_LEFT: xVel += stresso_WIDTH / 2; break;
case SDLK_RIGHT: xVel -= stresso_WIDTH / 2; break;
}
}
//If a key was pressed
/*
if( event.type == SDL_KEYDOWN )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_UP: yVel -= stresso_HEIGHT / 2; break;
case SDLK_DOWN: yVel += stresso_HEIGHT / 2; break;
case SDLK_LEFT: xVel -= stresso_WIDTH / 4; break;
case SDLK_RIGHT: xVel += stresso_WIDTH / 4; break;
}
}
//If a key was released
else if( event.type == SDL_KEYUP )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_UP: yVel += stresso_HEIGHT / 2 ;break;
case SDLK_DOWN: yVel -= stresso_HEIGHT / 2; break;
case SDLK_LEFT: xVel += stresso_WIDTH / 4; break;
case SDLK_RIGHT: xVel -= stresso_WIDTH / 4; break;
}
}*/
}
//Fonction qui pose probleme !
void stresso::move(tile *tiles[])
{
//Move the Stresso left or right
box.x += xVel;
//printf("\nbox.x = %d\nbox.y = %d\n----------------------\n", box.x, box.y);
//If the dot went too far to the left or right or touched a wall
if( ( box.x < 0 ) || ( box.x + stresso_WIDTH > LEVEL_WIDTH ) || touches_wall( box, tiles ) )
{
//move back
//printf("\nCheckpoint1\n");
/*for(int i =0; i<192; i++)
printf("\ntiles [%d] = %d", i, tiles[i]->get_type());*/
box.x -= xVel;
}
//Move the dot up or down
box.y += yVel;
//If the dot went too far up or down or touched a wall
if( ( box.y < 0 ) || ( box.y + stresso_WIDTH > LEVEL_HEIGHT ) || touches_wall( box, tiles ) )
{
//move back
//printf("\nCheckpoint2");
box.y -= yVel;
}
/*if(( box.y+stresso_HEIGHT>LEVEL_HEIGHT) )
box.y-= yVel;*/
}
int stresso::get_box2()
{
return box.x ;
}
void stresso::show()
{
//If stresso is moving left
if( xVel < 0 )
{
//Set the animation to left
status = left;
//Move to the next frame in the animation
frame++;
}
//If stresso is moving right
else if( xVel > 0 )
{
//Set the animation to right
status = right;
//Move to the next frame in the animation
frame++;
}
//If stresso standing
else
{
//Restart the animation
frame = 0;
}
//Loop the animation
if( frame >= 4 )
{
frame = 0;
}
//Show the stick figure
//perso est la surface de Stresso !
if( status == right )
{
apply_surface( box.x-camera.x, box.y, perso, screen, &clipright[ frame ] );
}
else if( status == left )
{
apply_surface( box.x- camera.x, box.y, perso, screen, &clipleft[ frame ] );
}
}
void stresso::set_camera()
{
//Center the camera over the stresso
//Center the camera over the dot
camera.x = ( box.x + stresso_WIDTH / 2 ) - SCREEN_WIDTH / 2;
camera.y = ( box.y + stresso_HEIGHT / 2 ) - SCREEN_HEIGHT / 2;
//Keep the camera in bounds.
if( camera.x < 0 )
{
camera.x = 0;
}
if( camera.y < 0 )
{
camera.y = 0;
}
if( camera.x > LEVEL_WIDTH - camera.w )
{
camera.x = LEVEL_WIDTH - camera.w;
}
if( camera.y > LEVEL_HEIGHT - camera.h )
{
camera.y = LEVEL_HEIGHT - camera.h;
}
} |
Partager