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

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

class Game;
class Map;
class Entity;
class Component;

class Message {
  public:
    enum class Type {
      walkLeft,
      walkRight,
      stopWalking,
      stopMovingHorizontally,
      stopMovingVertically,
      collision,
      jump,
      stopJump,
      drop,
      canDrop,
      cantDrop,
      die,
      stopDying,
      hitTheGround
    };
    
    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, double dt);
    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&, double) {}
    virtual void input(Game&, Entity&, int, int) {}
    virtual void detectCollision(Game&, Entity&, Entity&, std::pair<double, double>) {}
};

#endif