aboutsummaryrefslogtreecommitdiff
path: root/src/tmonster.c
blob: 9f3c61870daa8b23925f08ab707f259782aabaea (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
#include <stdint.h>
#include <stdlib.h>

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

#include "player.h"

#include "tmonster.h"

#define WAIT 120
#define MOVE_TIME 90

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

    /* not used */
    { 0, 0, 144, 144 },
    { 0, 0, 144, 144 },

    /* left */
    { 96, 16, 144, 144 },
    { 96, 48, 144, 144 },

    /* not used */
    { 0, 0, 144, 144 },
    { 0, 0, 144, 144 }
};

void tmonster_init(Entity *e)
{
    e->ox = e->y = 16;
    e->oy = e->x = 32 + (rand() % 256);
    e->dir = e->x > 160 ? DIR_LEFT : DIR_RIGHT;
    e->frames = (const Rect *)frames;
    e->update = tmonster_wait_update;
}

void tmonster_wait_update(Entity *e)
{
    e->delay++;
    e->frame = (e->delay & 2) ? 1 : 0;

    if (e->delay == WAIT)
    {
        e->delay = 0;
        e->frame = 1;
        e->update = tmonster_update;
    }
}

void tmonster_update(Entity *e)
{
    e->delay++;
    if (e->delay == WAIT * 2)
        e->delay = 0;

    if (e->delay < MOVE_TIME)
    {
        if (e->x > player_x() && e->x > 0)
        {
            e->dir = DIR_LEFT;
            e->x--;
        }
        if (e->x < player_x() && e->x < (MAP_W * MAP_TILE_W) - 16)
        {
            e->dir = DIR_RIGHT;
            e->x++;
        }
        if (e->y > player_y() && e->y > 0)
            e->y--;
        if (e->y < player_y() && e->y < (MAP_H * MAP_TILE_H) - 16)
            e->y++;
    }

    if (player_collision(e))
    {
        player_hit();
        reset_time();
    }
}