aboutsummaryrefslogtreecommitdiff
path: root/src/entities.c
blob: df8fb4ef4898ce0a1095ca67ac1aedae67daf9a8 (plain)
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
#include <stdint.h>
#include <string.h>

#include "vga.h"
#include "map.h"
#include "data.h"
#include "entities.h"

#include "effect.h"

#define MAX_ENTITY 32

static Entity entities[MAX_ENTITY];
static uint8_t last;

void entities_init()
{
    memset(entities, 0, sizeof(Entity) * MAX_ENTITY);
    last = 0;
}

Entity *entities_new()
{
    for (uint8_t i = 0; i < MAX_ENTITY; i++)
    {
        if (entities[i].used == 0)
        {
            memset(&entities[i], 0, sizeof(Entity));
            entities[i].used = 1;

            if (i >= last)
                last = i + 1;

            return &entities[i];
        }
    }

    return NULL;
}

void entities_update()
{
    Entity *e = entities;

    for (uint8_t i = 0; i < last; i++, e++)
        if (e->used)
            e->update(e);
}

void entities_erase()
{
    Entity *e = entities + last - 1;

    for (uint8_t i = 0; i < last; i++, e--)
        if (e->used)
        {
            Rect dst = { e->x, e->y + MAP_OFFS_Y, 16, 16 };
            blit(e->bg, &dst);
        }
}

void entities_draw()
{
    Entity *e = entities;

    for (uint8_t i = 0; i < last; i++, e++)
        if (e->used)
        {
            Rect dst = { e->x, e->y + MAP_OFFS_Y, 16, 16 };
            read_buffer(e->bg, &dst);
            blitrc(binary_sprites_start, &e->frames[e->dir * 4 + e->frame], &dst);
        }
}

void entities_warp_out_all()
{
    Entity *e = entities;

    for (uint8_t i = 0; i < last; i++, e++)
        if (e->used)
            effect_out_init(e);
}