summaryrefslogtreecommitdiff
path: root/src/projectileUtils.c
blob: 79154830c060d03b358348aa07fcc6444c1500e6 (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 "entity.h"
#include "definitions.h"
#include "vram.h"
#include "room.h"
#include "color.h"

extern const ProjectileDefinition gProjectileDefinitions[];

const ProjectileDefinition* GetProjectileDefinition(Entity*);
bool32 LoadProjectileSprite(Entity*, const ProjectileDefinition*);

const ProjectileDefinition* GetProjectileDefinition(Entity* this) {
    const ProjectileDefinition* definition = &gProjectileDefinitions[this->id];
    if (definition->gfx == 0xffff) {
        definition = &definition->ptr.definition[this->type];
    }
    return definition;
}

bool32 ProjectileInit(Entity* this) {
    if ((this->flags & ENT_DID_INIT) == 0) {
        const ProjectileDefinition* definition = GetProjectileDefinition(this);
        if (LoadProjectileSprite(this, definition) == FALSE) {
            return FALSE;
        }
        this->flags |= ENT_DID_INIT;
        if (definition->spriteFlags.collision != 0) {
            COLLISION_ON(this);
        }
        this->spriteIndex = definition->spriteIndex;
        if (this->spriteSettings.draw == 0) {
            this->spriteSettings.draw = definition->spriteFlags.draw;
        }
        this->spritePriority.b1 = definition->spriteFlags.spritePriority;
        this->spriteSettings.shadow = definition->spriteFlags.shadow;
        if (this->speed == 0) {
            this->speed = definition->speed;
        }
        this->collisionFlags = (definition->field0x3c << 4) | 7;
        this->collisionMask = definition->collisionMask;
        this->hitType = definition->damageType;
        this->hurtType = definition->field0x40;
        this->health = 0xff;
        this->hitbox = (Hitbox*)definition->ptr.hitbox;
        UpdateSpriteForCollisionLayer(this);
    }
    return TRUE;
}

bool32 LoadProjectileSprite(Entity* this, const ProjectileDefinition* definition) {
    bool32 result;
    if (definition->gfx != 0) {
        if ((definition->gfx & 0x8000) != 0) {
            this->spriteVramOffset = definition->gfx & 0x3ff;
        } else {
            if ((definition->gfx & 0x4000) != 0) {
                result = LoadSwapGFX(this, (u8)(definition->gfx >> 4), 0);
            } else {
                result = LoadFixedGFX(this, definition->gfx);
            }
            if (result == FALSE) {
                return FALSE;
            }
        }
    }
    LoadObjPalette(this, definition->paletteIndex);
    return TRUE;
}

bool32 IsProjectileOffScreen(Entity* this) {
    if (((u32)this->x.HALF.HI - gRoomControls.origin_x > gRoomControls.width) ||
        ((u32)this->y.HALF.HI - gRoomControls.origin_y > gRoomControls.height)) {
        return TRUE;
    } else {
        return FALSE;
    }
}

Entity* CreateProjectile(u32 id) {
    Entity* entity = GetEmptyEntity();
    if (entity != NULL) {
        entity->kind = PROJECTILE;
        entity->id = id;
        AppendEntityToList(entity, 5);
    }
    return entity;
}