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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
#include <stdint.h>
#include <string.h>
#include "vga.h"
#include "map.h"
#include "data.h"
#include "entities.h"
#include "player.h"
#include "effect.h"
#define MAX_ENTITY 20
static Entity entities[MAX_ENTITY];
static uint8_t last;
static uint8_t sort_cnt;
static Entity *sort[MAX_ENTITY + 1];
static Entity player_ent;
void entities_init()
{
memset(entities, 0, sizeof(Entity) * MAX_ENTITY);
last = 0;
memset(&player_ent, 0, sizeof(Entity));
player_ent.used = USED_PLAYER;
sort_cnt = 0;
}
Entity *entities_new()
{
for (uint8_t i = 0; i < MAX_ENTITY; i++)
{
if (entities[i].used == USED_FREE)
{
memset(&entities[i], 0, sizeof(Entity));
entities[i].used = USED_FG;
if (i >= last)
last = i + 1;
return &entities[i];
}
}
return NULL;
}
void entities_update()
{
for (uint8_t i = 0; i < last; i++)
if (entities[i].used)
{
entities[i].ox = entities[i].x;
entities[i].oy = entities[i].y;
entities[i].update(&entities[i]);
}
}
void entities_sort()
{
sort_cnt = 0;
for (uint8_t i = 0; i < last; i++)
if (entities[i].used)
sort[sort_cnt++] = &entities[i];
player_ent.y = player_y();
sort[sort_cnt++] = &player_ent;
/* using insertion sort;
*
* The sorting criteria is the y coordinate unless the entities
* share a 16px area in which we check the value of used that defines 3
* layers: bg, fb and player.
* */
Entity *t;
uint8_t i = 1;
while (i < sort_cnt)
{
uint8_t j = i;
while (j > 0 && (
((sort[j - 1]->y >> 4) == (sort[j]->y >> 4)
&& sort[j - 1]->used > sort[j]->used)
|| sort[j - 1]->y > sort[j]->y
))
{
t = sort[j];
sort[j] = sort[j - 1];
sort[j - 1] = t;
j--;
}
i++;
}
}
void entities_draw()
{
Rect dst = { 0, 0, 16, 16 };
/* in case we need to clean collected gold */
map_erase();
player_erase();
for (uint8_t i = 0; i < last; i++)
{
if (entities[i].erase)
{
dst.x = entities[i].ox;
dst.y = entities[i].oy + MAP_OFFS_Y;
blit_copy16(&dst);
entities[i].erase = 0;
}
}
/* draw in order to avoid flickering */
for (uint8_t i = 0; i < sort_cnt; i++)
switch (sort[i]->used)
{
case USED_FREE:
break;
case USED_FG:
case USED_BG:
dst.x = sort[i]->x;
dst.y = sort[i]->y + MAP_OFFS_Y;
blitrc(binary_sprites_start, &sort[i]->frames[sort[i]->dir * 4 + sort[i]->frame], &dst);
sort[i]->erase = 1;
break;
case USED_PLAYER:
player_draw();
break;
}
}
void entities_warp_out_all()
{
Entity *e = entities;
for (uint8_t i = 0; i < last; i++, e++)
if (e->used)
effect_out_init(e);
}
|