-
Notifications
You must be signed in to change notification settings - Fork 0
/
MovingObject.cpp
164 lines (135 loc) · 4.07 KB
/
MovingObject.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
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
/**************************************************************************
** Qt Creator license header template
** Special keywords: dpinol 06/03/2014 2014
** Environment variables:
** To protect a percent sign, use '%'.
**************************************************************************/
#define _USE_MATH_DEFINES
#include <math.h>
#include "MovingObject.h"
#include "TextureManager.h"
#include "Game.h"
#include <utils/utils.h>
#include <utils/Effect.h>
#include <numeric>
#include <SDL2/SDL_render.h>
#include <SDL_image.h>
MovingObject::MovingObject(const std::string &imgFilename, int totalTimeMs, bool center, float rotateSpeed)
: m_done(false),
m_totalTimeMs(totalTimeMs),
m_center(center),
m_rotateSpeed(rotateSpeed),
m_angle(0),
m_trajectoryIndex(0)
{
m_effects->addChild(m_alpha);
m_effects->addChild(m_scale);
TheTextureManager::Instance()->load(imgFilename, "spark");
int access;
Uint32 format;
SDL_Texture *texture = TheTextureManager::Instance()->getTextureMap()["spark"];
SDL_QueryTexture(texture, &format, &access, &m_width, &m_height);
/* if (m_maxOscilllationPerc != 0)
{
for (int i =0; i < 10; i++)
{
//1 - ratio + i * 2 * ratio = 1 - ratio * (-1 + 2*i)
float r = 1 - (m_maxOscilllationPerc / 100.0) * ( -1 + 2* i / 10.0);
TheTextureManager::Instance()->load(imgFilename, "spark" + dani::toString(i), NULL, r * m_width, r* m_height);
}
}*/
}
MovingObject::~MovingObject()
{
}
void MovingObject::setRandomSize(float amplitudeRatio)
{
m_scale.setMean(1, amplitudeRatio);
}
void MovingObject::setAlphaOscillation(float minAlphaRatio)
{
//m_minAlphaPerc = minAlphaRatio;
//m_alphaDegree = 0;
m_alpha.setRange( 255 * minAlphaRatio, 255);
}
void MovingObject::setTrajectory(Trajectory &trajectory)
{
m_trajectory = std::move(trajectory);
m_trajectoryIndex = 0;
m_pixel = m_trajectory[0];
Vector2D lastPoint;
m_totalDistance = std::accumulate(m_trajectory.begin(), m_trajectory.end(), 0.0,
[&](float acc, Vector2D const &p)
{
if (!lastPoint.isValid())
{
lastPoint = p;
return 0.0f;
}
else
{
return (p - lastPoint).length();
}
});
}
// load from file - int x, int y, int width, int height, std::string textureID, int numFrames, int callbackID = 0, int animSpeed = 0
void MovingObject::load(std::unique_ptr<LoaderParams> const &pParams)
{
}
// draw the object
void MovingObject::draw()
{
float scale = m_scale.get();
Uint32 x = m_pixel.getX() - m_width * scale / 2;
Uint32 y = m_pixel.getY() - m_height * scale / 2;
TextureManager::Instance()->draw("spark", x, y,
m_width, m_height,
NULL, m_angle, m_alpha.get(), scale);
}
// do update stuff
void MovingObject::update()
{
if (m_done)
return;
m_effects->update();
float dist = (m_pixel - m_trajectory[m_trajectoryIndex]).length();
if (dist < 2)
{
m_trajectoryIndex++;
if (m_trajectoryIndex == m_trajectory.size())
{
m_done = true;
return;
}
m_stepSpeed = (m_trajectory[m_trajectoryIndex] - m_trajectory[m_trajectoryIndex - 1]);
m_angle = m_stepSpeed.angle() * 180.0 / M_PI;
float stepTimeMs = m_totalTimeMs * m_stepSpeed.length() / m_totalDistance;
//m_stepSpeed.normalize();
//pix/frame = pix / (frame/ sec) / s
m_stepSpeed /= TheGame::Instance()->getFPS() * (stepTimeMs / 1000.0);
}
m_pixel += m_stepSpeed;
/*
if (m_minAlphaPerc != 100)
{
m_alphaDegree += 5 * 2.0 * M_PI / TheGame::Instance()->getFPS();
if (m_alphaDegree > 2 * M_PI)
m_alphaDegree = 0;
}*/
/* float scale = 1;
if (m_maxOscilllationPerc != 0)
{
//sizeRatio = 1 - (m_minAlpha / 100 + m_deltaIndex
float randPerc = rand() / (float) RAND_MAX;
scale = 1 + (m_maxOscilllationPerc / 100.0) * (- 1 + 2 * randPerc);
}
// float scale = 1 - (m_maxOscilllationPerc / 100.0) * ( -1 + 2* i / 10.0);
for(dani::Effect* dist: m_Effects)
{
dist->run();
}*/
}
// remove anything that needs to be deleted
void MovingObject::clean()
{
}