-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticles.c
178 lines (131 loc) · 4.31 KB
/
particles.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "particles.h"
#include <tari/actorhandler.h>
#include <tari/animation.h>
#include <tari/physicshandler.h>
#include <tari/math.h>
#include <tari/log.h>
#include <tari/system.h>
#include "stage.h"
// TODO: find a nicer way to write this
enum { MAX_PARTICLE_AMOUNT = (int)(1000*PERFORMANCE_FACTOR) };
typedef struct {
int mActive;
int mAnimation;
int mPhysics;
Duration mNow;
Duration mDuration;
Color mColor;
double mRotation;
double mRotationDelta;
Position mCenter;
} Particle;
static struct {
TextureData mBlood;
int mHead;
int mAmount;
Particle mParticles[MAX_PARTICLE_AMOUNT];
int mIsReal;
} gData;
void loadParticles() {
gData.mBlood = loadTexture("assets/particles/blood.pkg");
gData.mAmount = 0;
gData.mHead = 0;
int i;
for (i = 0; i < MAX_PARTICLE_AMOUNT; i++) {
gData.mParticles[i].mActive = 0;
}
gData.mIsReal = 0;
}
static void unloadParticle(Particle* e) {
drawBloodOnStage(*getHandledPhysicsPositionReference(e->mPhysics), e->mColor);
removeHandledAnimation(e->mAnimation);
removeFromPhysicsHandler(e->mPhysics);
e->mActive = 0;
gData.mAmount--;
}
static void updateParticle(Particle* e) {
e->mRotation += e->mRotationDelta;
setAnimationRotationZ(e->mAnimation, e->mRotation, e->mCenter);
if (handleDurationAndCheckIfOver(&e->mNow, e->mDuration)) {
unloadParticle(e);
}
}
void updateParticles() {
int i;
for (i = 0; i < MAX_PARTICLE_AMOUNT; i++) {
if (!gData.mParticles[i].mActive) continue;
updateParticle(&gData.mParticles[i]);
}
}
void setParticlesReal() {
int i;
for (i = 0; i < MAX_PARTICLE_AMOUNT; i++) {
if (!gData.mParticles[i].mActive) continue;
Particle* e = &gData.mParticles[i];
setAnimationColorType(e->mAnimation, COLOR_WHITE);
}
gData.mIsReal = 1;
}
void setParticlesUnreal() {
int i;
for (i = 0; i < MAX_PARTICLE_AMOUNT; i++) {
if (!gData.mParticles[i].mActive) continue;
Particle* e = &gData.mParticles[i];
setAnimationColorType(e->mAnimation, e->mColor);
}
gData.mIsReal = 0;
}
void invertParticleReality()
{
if (gData.mIsReal) setParticlesUnreal();
else setParticlesReal();
}
static Particle* getNextFreeParticle() {
int i;
for (i = 0; i < MAX_PARTICLE_AMOUNT; i++) {
if (!gData.mParticles[gData.mHead].mActive) return &gData.mParticles[gData.mHead];
gData.mHead = (gData.mHead + 1) % MAX_PARTICLE_AMOUNT;
}
logError("Unable to find free particle.");
logErrorInteger(gData.mAmount);
abortSystem();
return NULL;
}
static void addParticle(int tAmount, Position pos, Color tColor, TextureData* tTexture, GeoRectangle tPosRange, GeoRectangle tVelRange, double tMinRotationDelta, double tMaxRotationDelta, Duration tDuration) {
int i;
for (i = 0; i < tAmount; i++) {
if (gData.mAmount >= MAX_PARTICLE_AMOUNT) return;
double rx = randfrom(tPosRange.mTopLeft.x, tPosRange.mBottomRight.x);
double ry = randfrom(tPosRange.mTopLeft.y, tPosRange.mBottomRight.y);
double vx = randfrom(tVelRange.mTopLeft.x, tVelRange.mBottomRight.x);
double vy = randfrom(tVelRange.mTopLeft.y, tVelRange.mBottomRight.y);
Particle* p = getNextFreeParticle();
p->mPhysics = addToPhysicsHandler(vecAdd(pos, makePosition(rx, ry, 0)));
addAccelerationToHandledPhysics(p->mPhysics, makePosition(vx, vy, 0));
setHandledPhysicsGravity(p->mPhysics, makePosition(0, 0.1, 0));
p->mRotation = randfrom(-1, 1);
p->mRotationDelta = randfrom(tMinRotationDelta, tMaxRotationDelta);
p->mAnimation = playAnimationLoop(makePosition(0,0,0), tTexture, createOneFrameAnimation(), makeRectangleFromTexture(tTexture[0]));
setAnimationBasePositionReference(p->mAnimation, getHandledPhysicsPositionReference(p->mPhysics));
setAnimationScreenPositionReference(p->mAnimation, getStagePositionReference());
if (gData.mIsReal) {
setAnimationColorType(p->mAnimation, COLOR_WHITE);
}
else {
setAnimationColorType(p->mAnimation, tColor);
}
p->mCenter = makePosition(1, 1, 0);
setAnimationRotationZ(p->mAnimation, p->mRotation, p->mCenter);
setAnimationSize(p->mAnimation, makePosition(2, 2, 1), makePosition(0,0,0));
p->mNow = 0;
p->mDuration = tDuration + randfromInteger(-5 , 5);
p->mColor = tColor;
p->mActive = 1;
gData.mAmount++;
}
}
void addBloodParticles(Position p, Color c){
p.z = 5;
int amount = 5 ;
addParticle(amount, p, c, &gData.mBlood, makeGeoRectangle(-8 - 4, -8 - 4, -8 + 4, -8 + 4), makeGeoRectangle(-1, -2, 2, 1), -1, 1, 40);
}