]> git.lizzy.rs Git - dungeon_game.git/blob - plugins/game/game.h
7ae93448e743513429deacb41663579db029b510
[dungeon_game.git] / plugins / game / game.h
1 #ifndef _GAME_H_
2 #define _GAME_H_
3
4 #include <stdbool.h>
5 #include <sys/ioctl.h>
6 #include <stddef.h>
7 #define MAP_HEIGHT 1000
8 #define MAP_WIDTH 1000
9 #define LIGHT 10
10
11 /* Type definitions */
12
13 struct color
14 {
15         unsigned char r, g, b;
16 };
17
18 struct material
19 {
20         bool solid;
21         struct color color;
22 };
23
24 struct node
25 {
26         struct material *material;
27 };
28
29 struct entity_step_data
30 {
31         double dtime;
32         int dx;
33         int dy;
34         bool visible;
35 };
36
37 struct entity
38 {
39         char *name;
40         int x, y;
41         struct color color;
42         bool use_color;
43         char *texture;
44         bool remove;
45         void *meta;
46         int health;
47         int max_health;
48         bool collide_with_entities;
49
50         void (*on_step)(struct entity *self, struct entity_step_data stepdata);
51         void (*on_collide)(struct entity *self, int x, int y);
52         void (*on_collide_with_entity)(struct entity *self, struct entity *other);
53         void (*on_spawn)(struct entity *self, void *data);
54         void (*on_remove)(struct entity *self);
55         void (*on_death)(struct entity *self);
56         void (*on_damage)(struct entity *self, int damage);
57 };
58
59 struct list
60 {
61         void *element;
62         struct list *next;
63 };
64
65 struct generator_function
66 {
67         int chance;
68         void (*callback)(int x, int y);
69 };
70
71 struct input_handler
72 {
73         bool run_if_dead;
74         void (*callback)();
75 };
76
77 struct globalstep
78 {
79         bool run_if_dead;
80         void (*callback)(double dtime);
81 };
82
83 enum direction
84 {
85         UP,
86         LEFT,
87         DOWN,
88         RIGHT,
89 };
90
91 extern struct color black;
92
93 extern struct material wall;
94 extern struct material air;
95 extern struct material outside;
96
97 extern struct node map[MAP_WIDTH][MAP_HEIGHT];
98
99 extern struct entity player;
100 extern struct list *entities;
101
102 extern struct entity *entity_collision_map[MAP_WIDTH][MAP_HEIGHT];
103
104 struct color get_color(const char *str);
105 void set_color(struct color color, bool bg);
106 void light_color(struct color *color, double light);
107 void mix_color(struct color *color, struct color other, double ratio);
108 void dir_to_xy(enum direction dir, int *x, int *y);
109 struct list *add_element(struct list *list, void *element);
110 int clamp(int v, int max, int min);
111 int max(int a, int b);
112 int min(int a, int b);
113 void *make_buffer(void *ptr, size_t size);
114
115 void quit();
116 bool player_dead();
117
118 struct node get_node(int x, int y);
119 bool is_outside(int x, int y);
120 bool is_solid(int x, int y);
121
122 bool spawn(struct entity def, int x, int y, void *data);
123 bool move(struct entity *entity, int xoff, int yoff);
124 void add_health(struct entity *entity, int health);
125
126 void register_air_function(struct generator_function func);
127 void register_input_handler(unsigned char c, struct input_handler handler);
128 void register_render_component(void (*callback)(struct winsize ws));
129 void register_globalstep(struct globalstep step);
130
131 #endif