aboutsummaryrefslogtreecommitdiff
path: root/src/entities.c
blob: 0471fbc4386b47576c8f6d673a1650b350c62a47 (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
83
84
85
86
87
#include <stdint.h>
#include <string.h>

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

#include "effect.h"

#define MAX_ENTITY 16

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->ox = e->x;
            e->oy = e->y;
            e->update(e);
        }
}

void entities_draw()
{
    Rect dst = { 0, 0, 16, 16 };
    Entity *e = entities;

    for (uint8_t i = 0; i < last; i++, e++)
        if (e->erase)
        {
            dst.x = e->ox;
            dst.y = e->oy + MAP_OFFS_Y;
            blit_copy16x16(&dst);
            e->erase = 0;
        }

    e = entities;

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

void entities_warp_out_all()
{
    Entity *e = entities;

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