NppClone/src/player.cpp

95 lines
2.2 KiB
C++
Raw Permalink Normal View History

#include "parameters.h"
2023-04-22 12:50:03 +02:00
#include "level.h"
2023-05-26 18:14:13 +02:00
#include "player.h"
#include "utilities.h"
2023-05-26 18:14:13 +02:00
#include <gint/rtc.h>
#include <num/num.h>
2023-05-26 18:14:13 +02:00
#include <num/num.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-26 18:14:13 +02:00
#define SPEEDRUN 9.0f
2023-05-03 22:50:46 +02:00
#define SPEEDJUMP 9.0f
#define MAXFALLSPEED 15.0f
#define MAXRUNSPEED 10.0f
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
Player::Player(int16_t _x, int16_t _y) {
this->currx = libnum::num(_x); // initial position
this->curry = libnum::num(_y);
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
this->vx = libnum::num(0.0f); // initial velocity
this->vy = libnum::num(0.0f);
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
this->ax = libnum::num(
0.0f); // initial acceleration (ie. gravity chosen at 1 unit - be careful
// of the sign, y axis oriented to the bottom )
this->ay = libnum::num(0.0f);
2023-04-22 12:50:03 +02:00
2023-05-26 18:14:13 +02:00
this->tileX = (int)this->currx / 16;
this->tileY = (int)this->curry / 16;
2023-04-22 12:50:03 +02:00
2023-05-26 18:14:13 +02:00
last_tick = rtc_ticks();
2023-04-21 20:27:02 +02:00
}
2023-05-26 18:14:13 +02:00
Player::~Player() {}
2023-05-26 18:14:13 +02:00
void Player::Update(float dt) {
libnum::num DeltaTime = libnum::num(dt / 100.0f);
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
this->vy += this->ay * DeltaTime;
this->vx += this->ax * DeltaTime;
2023-04-22 12:50:03 +02:00
2023-05-26 18:14:13 +02:00
// if (this->vx >= MAXRUNSPEED) this->vx = MAXRUNSPEED;
// if (this->vy >= MAXFALLSPEED) this->vy = MAXFALLSPEED;
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
this->nextx = this->currx + this->vx * DeltaTime;
this->nexty = this->curry + this->vy * DeltaTime;
2023-05-26 18:14:13 +02:00
if (MyLevel.CanGo()) {
this->currx = this->nextx;
this->curry = this->nexty;
}
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
this->tileX = ((int)this->currx) / 16;
this->tileY = ((int)this->curry) / 16;
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
MyLevel.UpdateBorders();
2023-04-21 20:27:02 +02:00
}
2023-05-26 18:14:13 +02:00
void Player::Render(void) {
uint32_t temptick = rtc_ticks();
if (temptick - last_tick >= 10) {
last_tick = temptick;
framecounter++;
}
2023-05-26 18:14:13 +02:00
azrp_image_p8_effect((int)(this->currx * 16.0f), (int)(this->curry * 16.0f),
&img_circle, DIMAGE_NONE);
}
2023-05-26 18:14:13 +02:00
void Player::Left(float dt) { this->vx += LEFT; }
2023-05-26 18:14:13 +02:00
void Player::Right(float dt) { this->vx += RIGHT; }
2023-05-26 18:14:13 +02:00
void Player::Up(float dt) { this->vy += TOP; }
2023-05-26 18:14:13 +02:00
void Player::Down(float dt) { this->vy += BOTTOM; }
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
void Player::Jump(float dt) { this->vy -= SPEEDJUMP; }
2023-05-26 18:14:13 +02:00
void Player::Nothing(float dt) {
this->vx *= libnum::num(0.95);
this->vy *= libnum::num(0.95);
2023-04-21 20:27:02 +02:00
}