-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcorpse.c
137 lines (108 loc) · 3.66 KB
/
corpse.c
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
#include <stdio.h>
#include <stdlib.h>
#include "engine.h"
#include "vector2d.h"
#include "level.h"
#include "physObj.h"
#include "corpse.h"
SCorpse* CorpseCreate (float x, float y, ubyte w, ubyte h, float timeToRemove, SDL_Texture* texture)
{
SCorpse* corpse = (SCorpse*) malloc (sizeof(SCorpse));
corpse->physBody = PhysObjectCreate (x, y, w, h, PHYSOBJ_COLLISION_WITH_LEVEL);
ListAddElement (physObjects, corpse->physBody);
corpse->timeToRemove = timeToRemove;
corpse->_timeToRemove = 0.0f;
corpse->texture = texture;
return corpse;
}
void CorpseDestroy (SCorpse** corpse)
{
if (corpse == NULL || *corpse == NULL)
return;
if ((*corpse)->physBody != NULL)
ListDeleteElementByValue (physObjects, (*corpse)->physBody);
free (*corpse);
*corpse = NULL;
}
void CorpseClearAll ()
{
for (SListElement* element = corpses->first; element; element = element->next)
{
if (element->value != NULL)
CorpseDestroy((SCorpse**) &element->value);
}
ListClear (corpses);
}
void CorpseGetSdlRect (SCorpse* corpse, SDL_Rect* rect)
{
if (corpse == NULL || corpse->physBody == NULL || rect == NULL)
return;
SPhysObject* physBody = corpse->physBody;
rect->x = (int)(physBody->pos.x - cameraPos.x);
rect->y = (int)(physBody->pos.y - cameraPos.y);
rect->w = physBody->w;
rect->h = physBody->h;
}
SDL_Texture* CorpseGetTexture (SCorpse* corpse)
{
if (corpse == NULL)
return NULL;
return corpse->texture;
}
void CorpsesUpdateAndRender ()
{
if (corpses->first == NULL)
return;
SCorpse* corpse;
SListElement* element = corpses->first;
while (element != NULL)
{
corpse = NULL;
if (element)
corpse = (SCorpse*) element->value;
if (corpse != NULL)
{
SPhysObject* physBody = corpse->physBody;
if (physBody == NULL)
{
SListElement* nextElement = element->next;
ListDeleteElementByValue (corpses, corpse);
element = nextElement;
continue;
}
// check edges of level
short xPos = (short)(physBody->pos.x + (physBody->w >> 1)) / BLOCK_SIZE;
short yPos = (short)(physBody->pos.y + (physBody->h >> 1)) / BLOCK_SIZE;
if (xPos < 0 || xPos > (LEVEL_WIDTH - 1) ||
yPos < 0 || yPos > (LEVEL_HEIGHT - 1))
{
SListElement* nextElement = element->next;
ListDeleteElementByValue (physObjects, physBody);
ListDeleteElementByValue (corpses, corpse);
element = nextElement;
continue;
}
corpse->_timeToRemove += deltaTime;
// remove if time out
if (corpse->_timeToRemove >= corpse->timeToRemove)
{
SListElement* nextElement = element->next;
ListDeleteElementByValue (physObjects, physBody);
ListDeleteElementByValue (corpses, corpse);
element = nextElement;
continue;
}
// now draw
if (corpse->texture != NULL)
{
SDL_Rect rect;
CorpseGetSdlRect(corpse, &rect);
// if rect in screen range
if ((rect.x + rect.w) > 0 &&
(rect.x <= WINDOW_WIDTH))
EngineRenderImage (corpse->texture, &rect, false);
}
}
element = element->next;
}
}