summary refs log tree commit diff stats
path: root/src/components.h
blob: 687eab110f152c37d1ff80bae59df5d726baaf3d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#ifndef COMPONENTS_H
#define COMPONENTS_H

#include "entity.h"
#include <utility>
#include <list>
#include "map.h"

class UserMovementComponent : public Component {
  public:
    UserMovementComponent(Entity& parent) : Component(parent) {};
    void input(int key, int action);
      
  private:
    bool holdingLeft = false;
    bool holdingRight = false;
};

class PhysicsBodyComponent : public Component, public Collidable, public Locatable {
  public:
    PhysicsBodyComponent(Entity& parent) : Component(parent) {};
    void receive(message_t msg);
    void tick();
    void detectCollision(Entity& player, Locatable& physics, std::pair<double, double> old_position);
};

class PlayerSpriteComponent : public Component {
  public:
    PlayerSpriteComponent(Entity& parent, Locatable& physics);
    ~PlayerSpriteComponent();
    void render(Texture* buffer);
    void receive(message_t msg);
    void tick();
    
  private:
    Locatable& physics;
    Texture* sprite;
    int animFrame = 0;
    bool facingLeft = false;
    bool isMoving = false;
};

class PlayerPhysicsComponent : public Component, public Locatable {
  public:
    PlayerPhysicsComponent(Entity& parent);
    void tick();
    void receive(message_t msg);
    
  private:
    double jump_velocity;
    double jump_gravity;
    double jump_gravity_short;
    int direction = 0;
    bool canDrop = false;
};

class MapRenderComponent : public Component {
  public:
    MapRenderComponent(Entity& parent, Map* map);
    ~MapRenderComponent();
    void render(Texture* buffer);
    
  private:
    Texture* screen;
};

enum direction_t {
  up, left, down, right
};

typedef struct {
  int axis;
  int lower;
  int upper;
  int type;
} collision_t;

class MapCollisionComponent : public Component, public Collidable {
  public:
    MapCollisionComponent(Entity& parent, Map* map);
    void detectCollision(Entity& player, Locatable& physics, std::pair<double, double> old_position);
    
  private:
    void add_collision(int axis, int lower, int upper, direction_t dir, int type);
    
    std::list<collision_t> left_collisions;
    std::list<collision_t> right_collisions;
    std::list<collision_t> up_collisions;
    std::list<collision_t> down_collisions;
    Map* map;
};

#endif