aboutsummaryrefslogtreecommitdiff
path: root/src/old.c
blob: 3451118767ff7416f444a01cca7c0c923ac5c61e (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
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
147
148
149
150
151
152
153
154
155
156
157
158
#include <stdint.h>
#include <stdlib.h>

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

#include "player.h"

#include "old.h"

#define FRAME_STANDING 0
#define FRAME_JUMPING 1
#define WALK_CYCLE_FRAMES 4

#define JUMP_DELAY 2

static const Rect frames[2 * 4] =
{
    /* right */
    { 0, 80, 144, 144 },
    { 16, 80, 144, 144 },
    { 0, 80, 144, 144 },
    { 32, 80, 144, 144 },

    /* left */
    { 48, 80, 144, 144 },
    { 64, 80, 144, 144 },
    { 48, 80, 144, 144 },
    { 80, 80, 144, 144 },
};

void old_init(Entity *e)
{
    e->frames = (const Rect *)frames;
    e->update = old_update;
}

static uint8_t is_bottom_deadly(uint16_t x, uint16_t y)
{
    while (1)
    {
        if (map_is_deadly(x, y))
            return 1;

        if (map_is_blocked(x, y))
            return 0;

        y += 8;
    }
}

void old_update(Entity *e)
{
    if (e->gravity == GRAVITY_OFF
            && !map_is_blocked(e->x + 4, e->y + 16)
            && !map_is_blocked(e->x + 11, e->y + 16))
    {
        e->gravity = GRAVITY_DOWN;
        e->frame = FRAME_JUMPING;
    }

    if (e->gravity != GRAVITY_OFF)
    {
        uint8_t steps = gravity_seq[e->gravity - 1];

        if (e->gravity > GRAVITY_DOWN)
        {
            /* going down! */
            for (uint8_t i = 0; i < steps; i++)
            {
                /* hit the floor */
                if ((map_is_blocked(e->x + 11, e->y + 16)
                        || map_is_blocked(e->x + 4, e->y + 16))
                        && !map_is_blocked(e->x + 4, e->y + 15)
                        && !map_is_blocked(e->x + 11, e->y + 15))
                {
                    e->gravity = GRAVITY_OFF;
                    e->frame = FRAME_STANDING;
                    e->flags = rand() % (JUMP_DELAY * 2);
                    break;
                }
                e->y++;
            }
        }
        else
        {
            /* going up! */
            if (e->y < steps)
                e->y = 0;
            else
                e->y -= steps;
        }

        if (e->gravity != GRAVITY_OFF && e->gravity != GRAVITY_SEQ_LEN)
            e->gravity++;
    }
    else
    {
        if (e->delay & 1)
        {
            if (e->dir == DIR_RIGHT)
            {
                if (map_is_blocked(e->x + 16, e->y + 15)
                        || is_bottom_deadly(e->x + 16, e->y + 16))
                {
                    e->dir = DIR_LEFT;
                    e->flags++;
                }
                else
                    e->x++;

                if (e->flags > JUMP_DELAY
                        && map_is_blocked(e->x + 7, e->y - 1)
                        && !map_is_blocked(e->x + 7, e->y - 9))
                {
                    e->gravity = GRAVITY_UP;
                    e->frame = FRAME_JUMPING;
                }
            }
            else
            {
                /* dir is LEFT */
                if (map_is_blocked(e->x - 1, e->y + 15)
                        || is_bottom_deadly(e->x - 1, e->y + 16))
                {
                    e->dir = DIR_RIGHT;
                    e->flags++;
                }
                else
                    e->x--;

                if (e->flags > JUMP_DELAY
                        && map_is_blocked(e->x + 7, e->y - 1)
                        && !map_is_blocked(e->x + 7, e->y - 9))
                {
                    e->gravity = GRAVITY_UP;
                    e->frame = FRAME_JUMPING;
                }
            }
        }

        if (e->delay++ == WALK_DELAY)
        {
            e->delay = 0;
            e->frame++;
            if (e->frame == WALK_CYCLE_FRAMES)
                e->frame = FRAME_STANDING;
        }
    }

    if (player_collision(e))
    {
        player_hit();
        /* change direction */
        e->dir ^= 1;
    }
}