-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParticle.h
162 lines (111 loc) · 3.42 KB
/
Particle.h
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
#pragma once
#include "GameObject.h"
#include "RenderableObject.h"
#include "TickableObject.h"
#include "Animator.h"
#include "RigidBody.h"
#include "Vec2D.h"
#include <QGraphicsPixmapItem>
class Particle : public RigidBody {
protected:
double startlifetime, lifetime, pixscale;
QGraphicsPixmapItem* pm = 0;
KA::Vec2Df customGravity{ 0,9.8 };
public:
Particle(QPointF start, Animatable* textureset, double lifetime = 100, double pixscale = 0.5) : RigidBody(start, QPointF(0,0), textureset->pixmaps[0].width()*pixscale/scalefactor, textureset->pixmaps[0].height()*pixscale / scalefactor) {
this->pixscale = pixscale;
this->lifetime = lifetime; startlifetime = lifetime;
animator->setAnimatable(textureset);
setZValue(5);
}
~Particle() {
}
Particle(const Particle& go) {
this->setX(go.getX());
this->setY(go.getY());
this->setObjectId(go.getObjectId());
this->pixscale = go.pixscale;
this->lifetime = go.lifetime;
this->startlifetime = go.startlifetime;
//this->statepicker = statepicker;
}
Cloneable* clone() const { return new Particle(*this); }
void setCustomGravity(KA::Vec2Df vec) {
customGravity = vec;
}
KA::Vec2Df movement{0, 0};
virtual void render(QGraphicsScene& scene, bool shouldClear = false) {
//RigidBody::render(scene, shouldClear);
//return;
if (shouldDelete(true) || shouldClear) {
if (pm) {
scene.removeItem(pm);
pm = 0;
}
return;
}
if (!pm) {
pm = scene.addPixmap(animator->getCurrentPixmap());
pm->setScale(pixscale * scale);
pm->setZValue(getZValue());
}
pm->setPixmap(animator->getCurrentPixmap());
pm->setPos(Camera::worldToScreen(QPointF(getX(), getY())));
}
bool shouldDelete(bool ignorepm = false) { return (lifetime <= 0) && (ignorepm || !pm); }
virtual void tick(double delta) {
accel = customGravity;
accel.y += movement.y;
accel.x += movement.x;
lifetime -= delta * 1000;
float timeindipendent = (startlifetime - lifetime) / startlifetime;
RigidBody::tick(delta);
animator->tick(delta);
//setX(getX() + (movement.x * pow(M_E, timeindipendent) * delta));
//setY(getY() + (movement.y * delta));
}
virtual QPixmap getTexture() { return animator->getCurrentPixmap(getVelocity().x<0); }
bool* getObjectCharacteristics() override {
bool* characteristics = new bool[6] {
instanceof<TickableObject, GameObject>(this),
instanceof<RenderableObject, GameObject>(this),
0,
0,
0,
instanceof<Particle, GameObject>(this)
};
return characteristics;
}
};
class Projectile : public Particle {
objects::ObjectID* targets = 0;
uint8_t size = 0;
RigidBody* ignoredObject = 0;
public:
Projectile(QPointF pos, KA::Vec2Df vel, Animatable* textureset, objects::ObjectID ids[], uint8_t targetsize = 0, double lifetime = 100, double pixscale = 0.5) : Particle(pos, textureset, lifetime, pixscale) {
movement = vel;
setTargets(ids, targetsize);
}
~Projectile() {
delete[] targets;
}
void setProtectedObject(RigidBody* g) {
ignoredObject = g;
setZValue(g->getZValue()-1);
}
void setTargets(objects::ObjectID ids[], uint8_t targetsize = 0) {
if (targets)
delete[] targets;
this->size = targetsize;
this->targets = new objects::ObjectID[size];
for (int i = 0; i < size; i++)
targets[i] = ids[i];
}
bool isTarget(objects::ObjectID id) {
for (int i = 0; i < size; i++)
if (targets[i]==id)
return true;
return false;
}
void tick(double delta) override;
};