-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmitter.cpp
108 lines (87 loc) · 2.09 KB
/
Emitter.cpp
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
#include "ofMain.h"
#include "Emitter.h"
// Create a new Emitter - needs a SpriteSystem
//
Emitter::Emitter(SpriteSystem *spriteSys) {
sys = spriteSys;
lifespan = 3000; // milliseconds
started = false;
lastSpawned = 0;
rate = 1; // sprites/sec
haveChildImage = false;
haveImage = false;
velocity = ofVec3f(100, 100, 0);
drawable = true;
width = 50;
height = 50;
childWidth = 10;
childHeight = 10;
}
// Draw the Emitter if it is drawable. In many cases you would want a hidden emitter
//
//
void Emitter::draw() {
if (drawable) {
if (haveImage) {
image.draw(-image.getWidth() / 2.0 + trans.x, -image.getHeight() / 2.0 + trans.y);
}
else {
ofSetColor(0, 0, 255);
ofDrawRectangle(-width / 2 + trans.x, -height / 2 + trans.y, width, height);
}
}
// draw sprite system
//
sys->draw();
}
// Update the Emitter. If it has been started, spawn new sprites with
// initial velocity, lifespan, birthtime.
//
void Emitter::update() {
if (!started) return;
float time = ofGetElapsedTimeMillis();
if ((time - lastSpawned) > (1000.0 / rate)) {
// spawn a new sprite
Sprite sprite;
if (haveChildImage) sprite.setImage(childImage);
sprite.velocity = velocity;
sprite.lifespan = lifespan;
sprite.setPosition(trans);
sprite.birthtime = time;
sprite.width = childWidth;
sprite.height = childHeight;
sys->add(sprite);
lastSpawned = time;
}
sys->update();
}
// Start/Stop the emitter.
//
void Emitter::start() {
started = true;
lastSpawned = ofGetElapsedTimeMillis();
}
void Emitter::stop() {
started = false;
}
void Emitter::setLifespan(float life) {
lifespan = life;
}
void Emitter::setVelocity(ofVec3f v) {
velocity = v;
}
void Emitter::setChildImage(ofImage img) {
childImage = img;
haveChildImage = true;
childWidth = img.getWidth();
childHeight = img.getHeight();
}
void Emitter::setImage(ofImage img) {
image = img;
}
void Emitter::setRate(float r) {
rate = r;
}
float Emitter::maxDistPerFrame() {
return velocity.length() / ofGetFrameRate();
}