summary refs log tree commit diff stats
path: root/src/entity.h
blob: 772c98b3aed2c85170676101411b8206450c4634 (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
#ifndef ENTITY_H
#define ENTITY_H

class Entity;
class Component;

#include <list>
#include "renderer.h"
#include "game.h"

class Message {
  public:
    enum class Type {
      walkLeft,
      walkRight,
      stopWalking,
      stopMovingHorizontally,
      stopMovingVertically,
      collision,
      jump,
      stopJump,
      drop,
      canDrop,
      cantDrop,
      die
    };
    
    Message(Type type) : type(type) {}
    
    Type type;
    Entity* collisionEntity;
    int dropAxis;
};

class Entity {
  public:
    void addComponent(std::shared_ptr<Component> c);
    void send(Game& game, const Message& msg);
    void tick(Game& game);
    void input(Game& game, int key, int action);
    void render(Game& game, Texture& buffer);
    void detectCollision(Game& game, Entity& collider, std::pair<double, double> old_position);
    
    std::pair<double, double> position;
    std::pair<int, int> size;
    
  private:
    std::list<std::shared_ptr<Component>> components;
};

class Component {
  public:
    virtual void receive(Game&, Entity&, const Message&) {}
    virtual void render(Game&, Entity&, Texture&) {}
    virtual void tick(Game&, Entity&) {}
    virtual void input(Game&, Entity&, int, int) {}
    virtual void detectCollision(Game&, Entity&, Entity&, std::pair<double, double>) {}
};

#endif