NppClone/src/player.cpp

120 lines
2.9 KiB
C++
Raw Normal View History

2023-04-21 20:27:02 +02:00
#include "player.h"
#include <num/num.h>
#include <gint/rtc.h>
2023-04-22 12:50:03 +02:00
#include "level.h"
#include "utilities.h"
2023-04-21 20:27:02 +02:00
/*
extern bopti_image_t img_walking;
extern bopti_image_t img_running;
extern bopti_image_t img_static;
*/
2023-04-22 12:50:03 +02:00
extern bopti_image_t img_circle;
2023-04-21 20:27:02 +02:00
static uint32_t framecounter = 0;
2023-04-22 12:50:03 +02:00
extern Level MyLevel;
2023-05-03 22:50:46 +02:00
#define SPEEDRUN 9.0f
#define SPEEDJUMP 9.0f
#define MAXFALLSPEED 15.0f
2023-04-21 20:27:02 +02:00
Player::Player( int16_t _x, int16_t _y )
{
2023-05-03 22:50:46 +02:00
this->currx = _x; // initial position
this->curry = _y;
2023-04-21 20:27:02 +02:00
2023-05-03 22:50:46 +02:00
this->vx = 0.0f; // initial velocity
2023-04-22 12:50:03 +02:00
this->vy = 0.0f;
2023-04-21 20:27:02 +02:00
2023-05-03 22:50:46 +02:00
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;
2023-04-22 12:50:03 +02:00
this->width = 8;
this->height = 8;
2023-04-21 20:27:02 +02:00
last_tick = rtc_ticks();
this->action = STATIC;
this->direction = STATIC;
2023-04-21 20:27:02 +02:00
}
2023-04-21 20:27:02 +02:00
Player::~Player()
{
}
2023-04-22 12:50:03 +02:00
void Player::Update( float dt )
2023-04-21 20:27:02 +02:00
{
2023-04-22 12:50:03 +02:00
float DeltaTime = dt / 100.0f ;
2023-05-03 22:50:46 +02:00
2023-04-22 12:50:03 +02:00
this->vy += this->ay * DeltaTime;
if (this->vy >= MAXFALLSPEED) this->vy = MAXFALLSPEED;
2023-05-03 22:50:46 +02:00
2023-04-22 12:50:03 +02:00
// we were jumping and are reaching the top of the curve or even starting falling
if (this->action == JUMP && this->vy >= 0)
2023-04-21 20:27:02 +02:00
{
2023-04-22 12:50:03 +02:00
this->action = FALL;
2023-04-21 20:27:02 +02:00
}
2023-04-22 12:50:03 +02:00
2023-05-03 22:50:46 +02:00
this->nextx = this->currx + this->vx * DeltaTime;
this->nexty = this->curry + this->vy * DeltaTime;
2023-04-21 20:27:02 +02:00
}
2023-04-21 20:27:02 +02:00
void Player::Render( void )
{
uint32_t temptick = rtc_ticks();
if (temptick-last_tick>=10)
{
last_tick = temptick;
framecounter++;
}
2023-05-03 22:50:46 +02:00
azrp_image_p8_effect((int) (this->currx*16.0f), (int) (this->curry*16.0f), &img_circle, DIMAGE_NONE);
2023-04-22 12:50:03 +02:00
2023-05-03 22:50:46 +02:00
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" );
2023-04-22 12:50:03 +02:00
2023-04-21 20:27:02 +02:00
}
2023-05-03 22:50:46 +02:00
void Player::Left( float dt )
{
this->vx += LEFT;
this->direction = LEFT;
2023-05-03 22:50:46 +02:00
this->action = RUN;
2023-04-21 20:27:02 +02:00
}
2023-05-03 22:50:46 +02:00
void Player::Right( float dt )
2023-04-21 20:27:02 +02:00
{
2023-05-03 22:50:46 +02:00
this->vx += RIGHT;
2023-04-21 20:27:02 +02:00
this->direction = RIGHT;
2023-05-03 22:50:46 +02:00
this->action = RUN;
2023-04-21 20:27:02 +02:00
}
2023-04-21 20:27:02 +02:00
void Player::Jump( float dt )
{
2023-05-03 22:50:46 +02:00
this->vy -= SPEEDJUMP;
this->action = JUMP;
2023-04-21 20:27:02 +02:00
}
2023-05-03 22:50:46 +02:00
void Player::Nothing( float dt )
2023-04-21 20:27:02 +02:00
{
2023-05-03 22:50:46 +02:00
this->vx *= 0.98;
2023-04-21 20:27:02 +02:00
}