-
Notifications
You must be signed in to change notification settings - Fork 0
/
particle.c
75 lines (63 loc) · 1.67 KB
/
particle.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
#include "particle.h"
#include "snake.h"
void SnakeParticle_delete(AbstractEntity * ae);
void SnakeParticle_render(AbstractEntity * ae,int layer);
int SnakeParticle_onUpdate(AbstractEntity * ae,ANZ_SpriteMap*levelMap);
typedef struct
{
Snake*segment;
int lifetime;
int isHead;
}SnakeParticle;
AbstractEntity* SnakeParticle_create(void*snakeSegment,int isHead, AbstractEntity*parent)
{
AbstractEntity*result=AbstractEntity_create(parent);
result->deleteEntity=SnakeParticle_delete;
result->onUpdate=SnakeParticle_onUpdate;
result->render=SnakeParticle_render;
SnakeParticle*particle=(SnakeParticle*)malloc(sizeof(SnakeParticle));
particle->segment=(Snake*)snakeSegment;
particle->lifetime=rand()%11+10;
particle->isHead=isHead;
result->entity=particle;
result->posX=&particle->segment->posX;
result->posY=&particle->segment->posY;
return result;
}
void SnakeParticle_delete(AbstractEntity * ae)
{
SnakeParticle*particle=(SnakeParticle*)(ae->entity);
if(particle->segment!=0)
{
free(particle->segment);
}
free(particle);
}
void SnakeParticle_render(AbstractEntity * ae,int layer)
{
if(layer==0)
{
SnakeParticle*particle=(SnakeParticle*)(ae->entity);
if(particle->isHead)
{
Snake_renderHead(particle->segment,1);
}
else
{
Snake_renderSegment(particle->segment,1);
}
}
}
int SnakeParticle_onUpdate(AbstractEntity * ae,ANZ_SpriteMap*levelMap)
{
SnakeParticle*particle=(SnakeParticle*)(ae->entity);
if(particle->lifetime>0)
{
particle->lifetime--;
}
else
{
return 1;
}
return 0;
}