This commit is contained in:
Sylvain PILLOT 2023-05-03 22:50:46 +02:00
parent 923450ac4b
commit f705226e1e
9 changed files with 238 additions and 244 deletions

View File

@ -31,6 +31,7 @@ add_custom_command(
assets-cg/levels/level2.tmx)
set(SOURCES
src/vector2D.cpp
src/main.cpp
src/extrakeyboard.cpp
src/utilities.cpp

View File

@ -15,6 +15,9 @@ extern struct Map map_level2;
extern struct Map map_level3;
extern bool drawbackground;
extern bool textbacktile;
extern bool textforetile;
struct Map *map_level;
@ -56,10 +59,12 @@ void Level::UpdateDataMap( Player *MyPlayer )
uint16_t currentTile = map_level->layers[1][ index ];
if (currentTile==32)
{
MyPlayer->x = (float) (i*16+8);
MyPlayer->y = (float) (j*16);
MyPlayer->currx = (float) (i) + 0.5f;
MyPlayer->curry = (float) (j) + 0.5f;
MyPlayer->vx = 0.0f;
MyPlayer->vy = 0.0f;
MyPlayer->Update( 0.0f );
}
}
@ -81,32 +86,21 @@ void Level::Render( void )
uint16_t xtile = ((currentTile % map_level->tileset_size)-1) * 16;
uint16_t ytile = (currentTile / map_level->tileset_size) * 16;
azrp_subimage_p8( i*16, j*16, map_level->tileset, xtile, ytile, 16, 16, DIMAGE_NONE );
// TODO :
// the last column of tile is not fully drawn cause 4 pixels are missing
// to upgrade the
/*
if (u==0) Azur_draw_text( i*16+1, j*16+1, "%d", GetTileBackgroundINT( i, j ) );
else if (u==1) Azur_draw_text( i*16+1, j*16+1, "%d", GetTileForegroundINT( i, j ) );
*/
if (textbacktile) Azur_draw_text( i*16, j*16, "%d", GetTileBackgroundINT( i, j ) );
if (textforetile) Azur_draw_text( i*16+8, j*16+8, "%d", GetTileForegroundINT( i, j ) );
}
}
}
}
}
void Level::RenderSelected( void )
void Level::RenderSelected( uint8_t i, uint8_t j )
{
for(int i=tileXmin; i<=tileXmax; i++)
{
for(int j=tileYmin; j<=tileYmax; j++)
{
azrp_image_p8( i*16, j*16, &img_selected, DIMAGE_NONE );
}
}
azrp_image_p8( i*16, j*16, &img_selected, DIMAGE_NONE );
Azur_draw_text( i*16+1, j*16+1, "B=%d", GetTileBackgroundINT( i, j ) );
Azur_draw_text( i*16+1, j*16+1, "F=%d", GetTileForegroundINT( i, j ) );
}
@ -115,7 +109,8 @@ void Level::Update( float dt )
}
/*RETURN the type of tile located in the background for the point x, y using player coordinates (x=[0..25], y=[0..14]) */
/*the x and y correspond to hte integer part of MyPlayer.x and MyPlayer.y*/
int Level::GetTileBackgroundINT( uint8_t x, uint8_t y )
{
uint16_t index = y * map_level->w + x;
@ -123,7 +118,8 @@ int Level::GetTileBackgroundINT( uint8_t x, uint8_t y )
return currentTile;
}
/*RETURN the type of tile located in the foreground for the point x, y using player coordinates (x=[0..25], y=[0..14]) */
/*the x and y correspond to hte integer part of MyPlayer.x and MyPlayer.y*/
int Level::GetTileForegroundINT( uint8_t x, uint8_t y )
{
uint16_t index = y * map_level->w + x;
@ -131,65 +127,46 @@ int Level::GetTileForegroundINT( uint8_t x, uint8_t y )
return currentTile;
}
int Level::GetTileBackground( uint8_t x, uint8_t y )
{
uint8_t tileX = x / 16;
uint8_t tileY = y / 16;
/*RETURN the type of tile located in the background for the point x, y using screen coordinates (x=[0..396], y=[0..223]) */
int Level::GetTileBackground( uint16_t x, uint16_t y )
{
uint8_t tileX = x >> 4;
uint8_t tileY = y >> 4;
uint16_t index = tileY * map_level->w + tileX;
uint16_t currentTile = map_level->layers[0][ index ];
return currentTile;
}
int Level::GetTileForeground( uint8_t x, uint8_t y )
/*RETURN the type of tile located in the foreground for the point x, y using screen coordinates (x=[0..396], y=[0..223]) */
int Level::GetTileForeground( uint16_t x, uint16_t y )
{
uint8_t tileX = x / 16;
uint8_t tileY = y / 16;
uint8_t tileX = x >> 4;
uint8_t tileY = y >> 4 ;
uint16_t index = tileY * map_level->w + tileX;
uint16_t currentTile = map_level->layers[1][ index ];
return currentTile;
}
/*RETURN true if the player can go in the target position*/
bool Level::CanGo( Player *MyPlayer )
{
int tileX, tileY;
int tileIndex, tileValue;
uint16_t targetTile = this->GetTileBackgroundINT( (int) MyPlayer->nextx, (int) MyPlayer->nexty );
int xmin = (int) MyPlayer->nextx - 8;
int ymin = (int) MyPlayer->nexty - 8;
int xmax = (int) MyPlayer->nextx + 8;
int ymax = (int) MyPlayer->nexty + 8;
/*
tileXmin = xmin / 16;
tileXmax = xmax / 16;
tileYmin = ymin / 16;
tileYmax = ymax / 16;
XinTile = xmin % 16;
YinTile = ymin % 16;
*/
/*
if ((MyPlayer->action == RUN || MyPlayer->action == WALK) && MyPlayer->direction == LEFT)
{
if( GetTileBackground( xmin, ymin ) != 0 || GetTileBackground( xmin, ymax ) != 0) return false;
}
else if ((MyPlayer->action == RUN || MyPlayer->action == WALK) && MyPlayer->direction == RIGHT)
{
if( GetTileBackground( xmax, ymax ) != 0 || GetTileBackground( xmin, ymax ) != 0) return false;
}
else if (MyPlayer->action == JUMP)
{
if( GetTileBackground( xmin, ymin ) != 0 || GetTileBackground( xmax, ymin ) != 0) return false;
}
else if (MyPlayer->action == FALL)
{
if( GetTileBackground( xmin, ymax ) != 0 || GetTileBackground( xmax, ymax ) != 0) return false;
}
*/
if (targetTile!=0) return false;
return true;
}
/*RETURN true if the player is above a solid tile*/
/*TO DO : TO BE IMPROVED, THIS IS REALLY DIRTY !!!!*/
bool Level::IsOnGround( Player *MyPlayer )
{
if (this->GetTileBackgroundINT( (uint8_t) MyPlayer->currx, (uint8_t) MyPlayer->curry +1 ) !=0)
{
return true;
}
return false;
}

View File

@ -33,16 +33,17 @@ class Level
void Update( float dt );
void Render( void );
void RenderSelected( void );
void RenderSelected( uint8_t i, uint8_t j );
void ChangeMap( int level, Player *MyPlayer );
void UpdateDataMap( Player *MyPlayer );
bool CanGo( Player *MyPlayer );
bool IsOnGround( Player *MyPlayer );
private:
int GetTileBackground( uint8_t x, uint8_t y );
int GetTileForeground( uint8_t x, uint8_t y );
int GetTileBackground( uint16_t x, uint16_t y );
int GetTileForeground( uint16_t x, uint16_t y );
int GetTileBackgroundINT( uint8_t x, uint8_t y );
int GetTileForegroundINT( uint8_t x, uint8_t y );
};

View File

@ -34,8 +34,10 @@ bool screenshot = false;
bool record = false;
bool textoutput = false;
bool exitToOS = false;
uint8_t texttodraw=0;
uint8_t texttodraw=0;
bool textbacktile = false;
bool textforetile = false;
#define SCALE_PIXEL 1
#define X_RESOL (DWIDTH / SCALE_PIXEL)
@ -134,41 +136,28 @@ static void get_inputs( float dt )
if( MyKeyboard.IsKeyPressed(MYKEY_OPTN) && MyKeyboard.IsKeyPressedEvent(MYKEY_VARS) ) { drawbackground = !drawbackground; }
if( MyKeyboard.IsKeyPressed(MYKEY_OPTN) && MyKeyboard.IsKeyPressedEvent(MYKEY_F1) ) { texttodraw = 0; }
if( MyKeyboard.IsKeyPressed(MYKEY_OPTN) && MyKeyboard.IsKeyPressedEvent(MYKEY_F2) ) { texttodraw = 1; }
if( MyKeyboard.IsKeyPressed(MYKEY_OPTN) && MyKeyboard.IsKeyPressedEvent(MYKEY_F3) ) { texttodraw = 2; }
if( MyKeyboard.IsKeyPressed(MYKEY_OPTN) && MyKeyboard.IsKeyPressedEvent(MYKEY_F4) ) { texttodraw = 3; }
if( MyKeyboard.IsKeyPressed(MYKEY_OPTN) && MyKeyboard.IsKeyPressedEvent(MYKEY_0) ) { texttodraw = 0; }
if( MyKeyboard.IsKeyPressed(MYKEY_OPTN) && MyKeyboard.IsKeyPressedEvent(MYKEY_1) ) { texttodraw = 1; }
if( MyKeyboard.IsKeyPressed(MYKEY_OPTN) && MyKeyboard.IsKeyPressedEvent(MYKEY_2) ) { texttodraw = 2; }
if( MyKeyboard.IsKeyPressed(MYKEY_OPTN) && MyKeyboard.IsKeyPressedEvent(MYKEY_3) ) { texttodraw = 3; }
if( MyKeyboard.IsKeyPressed(MYKEY_OPTN) && MyKeyboard.IsKeyPressedEvent(MYKEY_4) ) { textbacktile = !textbacktile; }
if( MyKeyboard.IsKeyPressed(MYKEY_OPTN) && MyKeyboard.IsKeyPressedEvent(MYKEY_5) ) { textforetile = !textforetile; }
if( MyKeyboard.IsKeyPressedEvent(MYKEY_F1) ) { MyLevel.ChangeMap(1, &MyPlayer); }
if( MyKeyboard.IsKeyPressedEvent(MYKEY_F2) ) { MyLevel.ChangeMap(2, &MyPlayer); }
if( MyKeyboard.IsKeyPressedEvent(MYKEY_F3) ) { MyLevel.ChangeMap(3, &MyPlayer); }
if( MyKeyboard.IsKeyPressedEvent(MYKEY_F4) ) { MyLevel.ChangeMap(4, &MyPlayer); }
if( MyKeyboard.IsKeyPressedEvent(MYKEY_F5) ) { MyLevel.ChangeMap(5, &MyPlayer); }
if( MyKeyboard.IsKeyPressedEvent(MYKEY_F6) ) { MyLevel.ChangeMap(6, &MyPlayer); }
#endif
/*
if(MyKeyboard.IsKeyPressed(MYKEY_ALPHA) && MyKeyboard.IsKeyPressed(MYKEY_LEFT))
{
MyPlayer.Walk_Left( dt );
}
else if(MyKeyboard.IsKeyPressed(MYKEY_ALPHA) && MyKeyboard.IsKeyPressed(MYKEY_RIGHT))
{
MyPlayer.Walk_Right( dt );
}
else
*/
if(MyKeyboard.IsKeyPressed(MYKEY_LEFT))
{
MyPlayer.Run_Left( dt );
}
else if(MyKeyboard.IsKeyPressed(MYKEY_RIGHT))
{
MyPlayer.Run_Right( dt );
}
else
{
MyPlayer.No_Order( dt );
}
/* we can have either LEFT or RIGHT or NONE OF THEM pressed for the direction */
if(MyKeyboard.IsKeyPressed(MYKEY_LEFT)) MyPlayer.Left( dt );
else if(MyKeyboard.IsKeyPressed(MYKEY_RIGHT)) MyPlayer.Right( dt );
else MyPlayer.Nothing( dt );
/* JUMP is */
if(MyKeyboard.IsKeyPressedEvent(MYKEY_SHIFT))
{
MyPlayer.Jump( dt );
@ -268,7 +257,6 @@ int main(void)
{
// all the stuff to be update should be put here
MyKeyboard.Update( elapsedTime );
get_inputs( elapsedTime );
update( elapsedTime );
@ -287,7 +275,21 @@ int main(void)
{
// all the stuff to be rendered should be put here
azrp_clear( C_GREEN );
render();
/*
azrp_clear( C_BLACK );
Azur_draw_text(1,01, "FPS = %.0f", (float) (1000.0f / elapsedTime) );
azrp_line( 10, 10, 100, 50, C_WHITE );
azrp_line( 100, 10, 100, 50, C_RED );
azrp_line( 10, 100, 100, 150, C_GREEN );
azrp_line( 100, 150, 200, 150, C_WHITE );
azrp_line( 200, 150, 300, 100, C_BLUE )
azrp_line( 10, 100, 350, 100, C_RED );
*/
azrp_update();
}

View File

@ -175,8 +175,8 @@ void Player::Run_Left( float dt )
{
this->vx -= 1.0f;
if ( this->vx <= SPEEDRUN*-1.0f )
this->vx = -1.0f*SPEEDRUN;
if ( this->vx <= -SPEEDRUN )
this->vx = -SPEEDRUN;
this->direction = LEFT;
this->action = RUN;
@ -194,7 +194,6 @@ void Player::Run_Right( float dt )
if ( this->vx >= SPEEDRUN )
this->vx = SPEEDRUN;
this->direction = RIGHT;
this->action = RUN;
}
@ -207,11 +206,14 @@ void Player::Jump( float dt )
// check if we are already jumping or not
if (this->action != JUMP && this->action != FALL) // not in a jump or in a fall
{
/*
if ( this->action == WALK )
{
this->vy -= SPEEDJUMPWALK;
}
else if ( this->action == RUN )
else
*/
if ( this->action == RUN )
{
this->vy -= SPEEDJUMPRUN;
}

View File

@ -11,41 +11,29 @@ extern bopti_image_t img_static;
*/
extern bopti_image_t img_circle;
static uint32_t framecounter = 0;
extern Level MyLevel;
#define SPEEDWALK 6.0f
#define SPEEDRUN 12.0f
#define SPEEDSTATIC 0.0f
#define SPEEDJUMPWALK 3.0f
#define SPEEDJUMPRUN 9.0f
#define SPEEDJUMPSTATIC 6.0f
#define SPEEDRUN 9.0f
#define SPEEDJUMP 9.0f
#define MAXFALLSPEED 15.0f
Player::Player( int16_t _x, int16_t _y )
{
this->x = _x;
this->y = _y;
this->currx = _x; // initial position
this->curry = _y;
this->vx = 0.0f;
this->vx = 0.0f; // initial velocity
this->vy = 0.0f;
this->ax = 0.0f;
this->ay = 1.0f;
this->ax = 0.0f; // initial acceleration (ie. gravity chosen at 1 unit - be careful of the sign, y axis oriented to the bottom )
this->ay = 1.0f;
this->width = 8;
this->height = 8;
this->xmin = (int) this->x - this->width;
this->xmax = (int) this->x + this->width;
this->ymin = (int) this->y - this->height;
this->ymax = (int) this->y + this->height;
last_tick = rtc_ticks();
this->action = STATIC;
@ -61,45 +49,21 @@ Player::~Player()
void Player::Update( float dt )
{
float DeltaTime = dt / 100.0f ;
this->vy += this->ay * DeltaTime;
if (this->vy >= MAXFALLSPEED) this->vy = MAXFALLSPEED;
// we were jumping and are reaching the top of the curve or even starting falling
if (this->action == JUMP && this->vy >= 0)
{
this->action = FALL;
}
this->nextx = this->x + this->vx * DeltaTime;
this->nexty = this->y + this->vy * DeltaTime;
this->nextx = this->currx + this->vx * DeltaTime;
this->nexty = this->curry + this->vy * DeltaTime;
bool testdestination = MyLevel.CanGo( this ); // true if we can go to the planed position
if (testdestination)
{
this->x = this->nextx;
this->y = this->nexty;
}
else if(this->action == JUMP)
{
this->vx = 0;
this->vy = 0;
this->action = FALL;
}
else
{
this->vx = 0;
this->vy = 0;
this->action = STATIC;
}
xmin = (int) x - width;
xmax = (int) x + width;
ymin = (int) y - height;
ymax = (int) y + height;
}
@ -112,104 +76,44 @@ void Player::Render( void )
framecounter++;
}
azrp_image_p8_effect(xmin, ymin, &img_circle, DIMAGE_NONE);
azrp_image_p8_effect((int) (this->currx*16.0f), (int) (this->curry*16.0f), &img_circle, DIMAGE_NONE);
if (this->action==STATIC) Azur_draw_text( this->xmin+4, this->ymin+4, "S" );
else if (this->action==DRAFT) Azur_draw_text( this->xmin+4, this->ymin+4, "D" );
else if (this->action==WALK) Azur_draw_text( this->xmin+4, this->ymin+4, "W" );
else if (this->action==RUN) Azur_draw_text( this->xmin+4, this->ymin+4, "R" );
else if (this->action==JUMP) Azur_draw_text( this->xmin+4, this->ymin+4, "J" );
else if (this->action==FALL) Azur_draw_text( this->xmin+4, this->ymin+4, "F" );
else Azur_draw_text( this->xmin+4, this->ymin+4, "X" );
if (this->action==STATIC) Azur_draw_text( (int) (this->currx*16.0f+4.0f), (int) (this->curry*16.0f+4.0f) , "S" );
else if (this->action==DRAFT) Azur_draw_text( (int) (this->currx*16.0f+4.0f), (int) (this->curry*16.0f+4.0f) , "D" );
else if (this->action==WALK) Azur_draw_text( (int) (this->currx*16.0f+4.0f), (int) (this->curry*16.0f+4.0f) , "W" );
else if (this->action==RUN) Azur_draw_text( (int) (this->currx*16.0f+4.0f), (int) (this->curry*16.0f+4.0f) , "R" );
else if (this->action==JUMP) Azur_draw_text( (int) (this->currx*16.0f+4.0f), (int) (this->curry*16.0f+4.0f) , "J" );
else if (this->action==FALL) Azur_draw_text( (int) (this->currx*16.0f+4.0f), (int) (this->curry*16.0f+4.0f) , "F" );
else Azur_draw_text( (int) (this->currx*16.0f+4.0f), (int) (this->curry*16.0f+4.0f) , "X" );
}
void Player::Run_Left( float dt )
{
if ( this->action == STATIC )
{
this->vx -= 1.0f;
this->action = RUN;
if ( this->vx <= -SPEEDRUN ) this->vx = -SPEEDRUN;
}
else if ( this->action == JUMP || this->action == FALL )
{
this->vx -= 1.0f;
if ( this->vx <= -SPEEDRUN ) this->vx = -SPEEDRUN;
}
else if ( this->action == RUN || this->action == WALK )
{
this->vx -= 1.0f;
this->action = RUN;
if ( this->vx <= -SPEEDRUN ) this->vx = -SPEEDRUN;
}
void Player::Left( float dt )
{
this->vx += LEFT;
this->direction = LEFT;
this->action = RUN;
}
void Player::Run_Right( float dt )
void Player::Right( float dt )
{
if ( this->action == STATIC )
{
this->vx += 1.0f;
this->action = RUN;
if ( this->vx >= SPEEDRUN ) this->vx = SPEEDRUN;
}
else if ( this->action == JUMP || this->action == FALL )
{
this->vx += 1.0f;
if ( this->vx >= SPEEDRUN ) this->vx = SPEEDRUN;
}
else if ( this->action == RUN || this->action == WALK )
{
this->vx += 1.0f;
this->action = RUN;
if ( this->vx >= SPEEDRUN ) this->vx = SPEEDRUN;
}
this->vx += RIGHT;
this->direction = RIGHT;
this->action = RUN;
}
void Player::Jump( float dt )
{
if ( this->action == STATIC )
{
this->vy = -SPEEDJUMPSTATIC;
}
else if (this->action != JUMP && this->action != FALL) // not in a jump or in a fall
{
this->vy -= SPEEDJUMPRUN;
}
else if ( this->action == FALL) // descending phase (FALL) of a jump, we can start a new small jump
{
this->vy -= SPEEDJUMPSTATIC;
}
else if ( this->action == JUMP) // descending phase (FALL) of a jump, we can start a new small jump
{
this->vy *= 0.9f;
}
this->vy -= SPEEDJUMP;
this->action = JUMP;
}
void Player::No_Order( float dt )
void Player::Nothing( float dt )
{
if (this->action == RUN || this->action == WALK)
{
this->action = DRAFT;
this->vx *= 0.9;
if (ABS(this->vx) <= 0.1 && ABS(this->vy) <= 0.1)
{
this->vx = 0;
this->action = STATIC;
this->direction = STATIC;
}
}
this->vx *= 0.98;
}

View File

@ -35,35 +35,30 @@ class Player
void Update( float dt );
void Render( );
//void Walk_Left( float dt );
//void Walk_Right( float dt );
void Run_Left( float dt );
void Run_Right( float dt );
void No_Order( float dt );
void Left( float dt );
void Right( float dt );
void Nothing( float dt );
void Jump( float dt );
float x, y; // center position of the player
float currx, curry; // center position of the player
float nextx, nexty; // interim x and y position (to be tested to check if they can be actually achieved or not while moving)
float vx, vy; // speed of the player as per x and y coordinates
float ax, ay; // acceleration of the player as per x an y coordinates
uint8_t width, height; // width and height - for the hitbox
int16_t xmin, xmax, ymin, ymax; // square hitbox (to speed up the impact calculations)
uint16_t xscreen, yscreen;
uint8_t width, height; // width and height - for the hitbox
uint32_t last_tick;
bool playeronground = true;
float currentspeed = 0.0f;
int8_t direction = STATIC;
int8_t action = STATIC;
};
#endif

35
src/vector2D.cpp Normal file
View File

@ -0,0 +1,35 @@
#include "vector2D.h"
#include <num/num.h>
Vector2D::Vector2D()
{
this->x = 0.0;
this->y = 0.0;
}
Vector2D::Vector2D( float x, float y )
{
this->x = libnum::num32(x);
this->y = libnum::num32(y);
}
Vector2D::Vector2D( libnum::num32 x, libnum::num32 y )
{
this->x = x;
this->y = y;
}
Vector2D::~Vector2D()
{
this->x = 0.0;
this->y = 0.0;
}
Vector2D Vector2D::AddVectors( Vector2D a, Vector2D b )
{
this->x = a.x + b.x;
this->y = a.y + b.y;
return *this;
}

77
src/vector2D.h Normal file
View File

@ -0,0 +1,77 @@
#ifndef VECTOR2D_H
#define VECTOR2D_H
#include <num/num.h>
class Vector2D
{
public:
Vector2D();
Vector2D( float x, float y );
Vector2D( libnum::num32 x, libnum::num32 y );
~Vector2D();
Vector2D AddVectors( Vector2D a, Vector2D b );
inline constexpr Vector2D &operator+=( Vector2D const &other )
{
this->x += other.x;
this->y += other.y;
return *this;
}
inline constexpr Vector2D &operator-=( Vector2D const &other )
{
this->x -= other.x;
this->y -= other.y;
return *this;
}
inline constexpr Vector2D &operator*=( float scale )
{
this->x *= libnum::num32(scale);
this->y *= libnum::num32(scale);
return *this;
}
inline constexpr Vector2D &operator/=( float scale )
{
this->x /= libnum::num32(scale);
this->y /= libnum::num32(scale);
return *this;
}
inline friend Vector2D operator*(float scale, Vector2D const &v) {
Vector2D r;
r.x = v.x * libnum::num32( scale );
r.y = v.y * libnum::num32( scale );
return r;
}
inline friend Vector2D operator*(Vector2D const &v, float scale) {
Vector2D r;
r.x = v.x * libnum::num32( scale );
r.y = v.y * libnum::num32( scale );
return r;
}
inline friend Vector2D operator/(Vector2D const &v, float scale) {
Vector2D r;
r.x = v.x / libnum::num32( scale );
r.y = v.y / libnum::num32( scale );
return r;
}
libnum::num32 x;
libnum::num32 y;
};
#endif