NppClone/src/player.cpp

240 lines
4.9 KiB
C++

#include "player.h"
#include <num/num.h>
#include <gint/rtc.h>
#include "level.h"
/*
extern bopti_image_t img_walking;
extern bopti_image_t img_running;
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 4.0f
#define SPEEDJUMPRUN 12.0f
#define SPEEDJUMPSTATIC 8.0f
#define MAXFALLSPEED 20.0f
Player::Player( int16_t _x, int16_t _y )
{
this->x = _x;
this->y = _y;
this->vx = 0.0f;
this->vy = 0.0f;
this->ax = 0.0f;
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();
}
Player::~Player()
{
}
void Player::Update( float dt )
{
float DeltaTime = dt / 100.0f ;
//this->vx += this->ax * DeltaTime; // horizontal acceleration not considered (yet)
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;
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;
}
void Player::Render( void )
{
uint32_t temptick = rtc_ticks();
if (temptick-last_tick>=10)
{
last_tick = temptick;
framecounter++;
}
/*
if (action==RUN)
{
uint8_t frameinternal = framecounter % 8;
azrp_subimage_p8_effect(xmin, ymin, &img_running, frameinternal*16, 0, 16, 16, direction==RIGHT ? DIMAGE_NONE : IMAGE_HFLIP );
}
else if (action==WALK)
{
uint8_t frameinternal = framecounter % 7;
azrp_subimage_p8_effect(xmin, ymin, &img_walking, frameinternal*16, 0, 16, 16, direction==RIGHT ? DIMAGE_NONE : IMAGE_HFLIP );
}
else if (action==STATIC)
{
uint8_t frameinternal = framecounter % 8;
azrp_subimage_p8_effect(xmin, ymin, &img_static, frameinternal*16, 0, 16, 16, DIMAGE_NONE);
}
*/
azrp_image_p8_effect(xmin, ymin, &img_circle, DIMAGE_NONE);
}
void Player::Walk_Left( float dt )
{
this->vx = SPEEDWALK * -1.0f;
this->direction = LEFT;
this->action = WALK;
this->Update( dt );
}
void Player::Walk_Right( float dt )
{
this->vx = SPEEDWALK * 1.0f;
this->direction = RIGHT;
this->action = WALK;
this->Update( dt );
}
void Player::Run_Left( float dt )
{
this->vx = SPEEDRUN * -1.0f;
this->direction = LEFT;
this->action = RUN;
this->Update( dt );
}
void Player::Run_Right( float dt )
{
this->vx = SPEEDRUN * 1.0f;
this->direction = RIGHT;
this->action = RUN;
this->Update( dt );
}
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 * -1.0f;
}
else if (this->action==RUN)
{
this->vy = SPEEDJUMPRUN * -1.0f;
}
else
{
this->vy = SPEEDJUMPSTATIC * -1.0f;
}
}
else if (this->action == FALL) // descending phase (FALL) of a jump, we can start a new small jump
{
this->vy = SPEEDJUMPSTATIC * -1.0f;
}
else
{
}
this->Update( dt );
}
void Player::No_Order( float dt )
{
/*
if (this->action == RUN || this->action == WALK)
{
this->action = BREAK;
}
if (this->direction == LEFT) // we were going LEFT and we stop moving to the left so we decrease speed till reaching static
{
this->vx += 1;
if (vx>=0)
{
this->vx = 0;
this->action = STATIC;
this->direction = STATIC;
}
}
else if (this->direction == RIGHT) // we were going RIGHT and we stop moving to the left so we decrease speed till reaching static
{
this->vx -= 1;
if (vx<=0)
{
this->vx = 0;
this->action = STATIC;
this->direction = STATIC;
}
}
this->Update( 0.0f, 0.0, 0.0 );
*/
this->Update( dt );
}