NppClone/src/player.cpp

175 lines
3.3 KiB
C++

#include "player.h"
#include <num/num.h>
#include <gint/rtc.h>
#include "background.h"
/*
extern bopti_image_t img_walking;
extern bopti_image_t img_running;
extern bopti_image_t img_static;
*/
extern bopti_image_t img_rectangle;
static uint32_t framecounter = 0;
extern Background MyBackground;
Player::Player( int16_t _x, int16_t _y )
{
x = libnum::num(_x);
y = libnum::num(_y);
width = 8;
height = 8;
speed = 10;
xmin = (int) x - width;
xmax = (int) x + width;
ymin = (int) y - height;
ymax = (int) y + height;
last_tick = rtc_ticks();
}
Player::~Player()
{
}
void Player::Update( float dt, libnum::num ax, libnum::num ay )
{
libnum::num DeltaTime = libnum::num( dt / 60000.0f );
vx += ax * DeltaTime;
vy += ay * DeltaTime;
libnum::num xtemp = x + vx * DeltaTime;
libnum::num ytemp = y + vy * DeltaTime;
if (MyBackground.CanGo( this ))
{
x = xtemp;
y = ytemp;
}
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_rectangle, DIMAGE_NONE);
}
void Player::Set_Speed( uint8_t _sp )
{
speed = _sp;
}
void Player::Walk_Left( float dt )
{
libnum::num a = libnum::num( dt / 60000.0f );
x -= a * libnum::num( speed/3 );
this->direction = LEFT;
this->action = WALK;
}
void Player::Walk_Right( float dt )
{
libnum::num a = libnum::num( dt / 60000.0f );
x += a * libnum::num( speed/3 );
this->direction = RIGHT;
this->action = WALK;
}
void Player::Run_Left( float dt )
{
libnum::num a = libnum::num( dt / 60000.0f );
x -= a * libnum::num( speed );
this->direction = LEFT;
this->action = RUN;
}
void Player::Run_Right( float dt )
{
libnum::num a = libnum::num( dt / 60000.0f );
x += a * libnum::num( speed );
this->direction = RIGHT;
this->action = RUN;
}
void Player::Jump( float dt )
{
if (this->action==WALK)
{
//this->vx = speed / 3;
this->vy = -speed / 2;
}
else if (this->action==RUN)
{
//this->vx = speed;
this->vy = -speed;
}
else
{
//this->vx = 0;
this->vy = -speed /2;
}
this->direction = LEFT;
this->action = JUMP;
if (this->direction==LEFT)
{
this->vx = -this->vx;
}
}
void Player::No_Order( float dt )
{
if (this->action == RUN || this->action == WALK)
{
this->action == BREAK;
}
}