Animation and fps
This commit is contained in:
parent
0432d6932c
commit
366bdf7d20
4 changed files with 22 additions and 3 deletions
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
Rectangle Pacman::getTextureRect() const{
|
||||
Rectangle result;
|
||||
result.x = 0;
|
||||
result.x = 16*frame;
|
||||
result.y = facing*16;
|
||||
result.height = 16;
|
||||
result.width = 16;
|
||||
|
|
@ -17,6 +17,7 @@ Pacman::Pacman() {
|
|||
this->texture = LoadTexture("assets/sprites/pacman.png");
|
||||
this->facing = 0;
|
||||
this->position = {0.,0.};
|
||||
this->frame = 0;
|
||||
}
|
||||
Pacman::Pacman(Vector2 position) {
|
||||
this->texture = LoadTexture("assets/sprites/pacman.png");
|
||||
|
|
@ -53,6 +54,18 @@ void Pacman::tick() {
|
|||
}
|
||||
}
|
||||
|
||||
void Pacman::process() {
|
||||
this->time += GetFrameTime();
|
||||
if (this->time >= 1.0/this->fps){
|
||||
this->time = 0.;
|
||||
this->animation_tick();
|
||||
}
|
||||
}
|
||||
|
||||
void Pacman::animation_tick() {
|
||||
this->frame = (this->frame+1)%frame_count;
|
||||
}
|
||||
|
||||
void Pacman::draw() const {
|
||||
// Drawing
|
||||
DrawTextureRec(this->texture, this->getTextureRect(), this->position, WHITE);
|
||||
|
|
|
|||
|
|
@ -19,16 +19,22 @@ class Entity{
|
|||
|
||||
class Pacman : public Entity{
|
||||
const int speed = 16;
|
||||
const int frame_count = 7;
|
||||
const int fps = 24;
|
||||
private:
|
||||
Texture2D texture;
|
||||
int facing;
|
||||
|
||||
Rectangle getTextureRect() const;
|
||||
public:
|
||||
unsigned char frame;
|
||||
float time;
|
||||
|
||||
Pacman();
|
||||
Pacman(Vector2 position);
|
||||
~Pacman();
|
||||
void tick() override;
|
||||
void process() override;
|
||||
void animation_tick();
|
||||
void draw() const override;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ int main() {
|
|||
|
||||
SetTargetFPS(60);
|
||||
|
||||
world = create_world_with(.25);
|
||||
world = create_world_with(1./10.);
|
||||
|
||||
world.entities.push_back(new Pacman({SCREEN_WIDTH/2,SCREEN_HEIGHT/2}));
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue