-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathMOSParticle.cpp
More file actions
213 lines (174 loc) · 5.86 KB
/
MOSParticle.cpp
File metadata and controls
213 lines (174 loc) · 5.86 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "MOSParticle.h"
#include "Atom.h"
#include "PostProcessMan.h"
#include "Draw.h"
#include "FrameMan.h"
#include <array>
using namespace RTE;
ConcreteClassInfo(MOSParticle, MovableObject, 1000);
MOSParticle::MOSParticle() {
Clear();
}
MOSParticle::~MOSParticle() {
Destroy(true);
}
void MOSParticle::Clear() {
m_Atom = nullptr;
m_SpriteAnimMode = OVERLIFETIME;
m_PostEffectEnabled = true; // Default to true for backwards compatibility reasons
}
int MOSParticle::Create() {
if (MOSprite::Create() < 0) {
return -1;
}
if (!m_Atom) {
m_Atom = new Atom();
}
return 0;
}
int MOSParticle::Create(const MOSParticle& reference) {
MOSprite::Create(reference);
m_Atom = new Atom(*(reference.m_Atom));
m_Atom->SetOwner(this);
return 0;
}
int MOSParticle::ReadProperty(const std::string_view& propName, Reader& reader) {
StartPropertyList(return MOSprite::ReadProperty(propName, reader));
MatchProperty("Atom", {
if (!m_Atom) {
m_Atom = new Atom;
}
reader >> *m_Atom;
m_Atom->SetOwner(this);
});
EndPropertyList;
}
int MOSParticle::Save(Writer& writer) const {
MOSprite::Save(writer);
// TODO: Make proper save system that knows not to save redundant data!
/*
writer.NewProperty("Atom");
writer << m_Atom;
*/
return 0;
}
void MOSParticle::Destroy(bool notInherited) {
delete m_Atom;
if (!notInherited) {
MOSprite::Destroy();
}
Clear();
}
int MOSParticle::GetDrawPriority() const { return m_Atom->GetMaterial()->GetPriority(); }
const Material* MOSParticle::GetMaterial() const { return m_Atom->GetMaterial(); }
void MOSParticle::SetAtom(Atom* newAtom) {
delete m_Atom;
m_Atom = newAtom;
m_Atom->SetOwner(this);
}
void MOSParticle::RestDetection() {
MOSprite::RestDetection();
// If we seem to be about to settle, make sure we're not still flying in the air
if ((m_ToSettle || IsAtRest()) && g_SceneMan.OverAltitude(m_Pos, (m_aSprite[m_Frame]->h / 2) + 3, 2)) {
m_VelOscillations = 0;
m_RestTimer.Reset();
m_ToSettle = false;
}
}
void MOSParticle::Travel() {
MOSprite::Travel();
if (m_PinStrength) {
return;
}
float deltaTime = g_TimerMan.GetDeltaTimeSecs();
float velMag = m_Vel.GetMagnitude();
// Set the atom to ignore a certain MO, if set and applicable.
if (m_HitsMOs && m_pMOToNotHit && g_MovableMan.ValidMO(m_pMOToNotHit) && !m_MOIgnoreTimer.IsPastSimTimeLimit()) {
std::vector<MOID> MOIDsNotToHit;
m_pMOToNotHit->GetMOIDs(MOIDsNotToHit);
for (const MOID& MOIDNotToHit: MOIDsNotToHit) {
m_Atom->AddMOIDToIgnore(MOIDNotToHit);
}
}
// Do static particle bounce calculations.
int hitCount = 0;
if (!IsTooFast()) {
m_Atom->Travel(g_TimerMan.GetDeltaTimeSecs(), true);
}
m_Atom->ClearMOIDIgnoreList();
if (m_SpriteAnimMode == ONCOLLIDE) {
// TODO: Rework this so it's less incomprehensible black magic math and not driven by AngularVel.
double newFrame = m_Rotation.GetRadAngle();
newFrame -= std::floor(m_Rotation.GetRadAngle() / (2.0F * c_PI)) * (2.0F * c_PI);
newFrame /= (2.0F * c_PI);
newFrame *= m_FrameCount;
m_Frame = std::floor(newFrame);
m_Rotation += m_AngularVel * deltaTime;
if (m_Frame >= m_FrameCount) {
m_Frame = m_FrameCount - 1;
}
}
}
void MOSParticle::Update() {
MOSprite::Update();
}
void MOSParticle::Draw(BITMAP* targetBitmap, const Vector& targetPos, DrawMode mode, bool onlyPhysical) const {
RTEAssert(!m_aSprite.empty(), "No sprite bitmaps loaded to draw " + GetPresetName());
RTEAssert(m_Frame >= 0 && m_Frame < m_FrameCount, "Frame is out of bounds for " + GetPresetName());
if (mode == g_DrawMOID && m_MOID == g_NoMOID) {
return;
}
Vector spritePos(m_Pos + m_SpriteOffset - targetPos);
// TODO I think this is an array with 4 elements to account for Y wrapping. Y wrapping is not really handled in this game, so this can probably be knocked down to 2 elements. Also, I'm sure this code can be simplified.
std::array<Vector, 4> drawPositions = {spritePos};
int drawPasses = 1;
bool needsWrap = mode != g_DrawMOID;
if (needsWrap && g_SceneMan.SceneWrapsX()) {
if (targetPos.IsZero() && m_WrapDoubleDraw) {
if (spritePos.GetFloorIntX() < m_aSprite[m_Frame]->w) {
drawPositions.at(drawPasses) = spritePos;
drawPositions.at(drawPasses).m_X += static_cast<float>(targetBitmap->w);
drawPasses++;
} else if (spritePos.GetFloorIntX() > targetBitmap->w - m_aSprite[m_Frame]->w) {
drawPositions.at(drawPasses) = spritePos;
drawPositions.at(drawPasses).m_X -= static_cast<float>(targetBitmap->w);
drawPasses++;
}
} else if (m_WrapDoubleDraw) {
if (targetPos.m_X < 0) {
drawPositions.at(drawPasses) = drawPositions[0];
drawPositions.at(drawPasses).m_X -= static_cast<float>(g_SceneMan.GetSceneWidth());
drawPasses++;
}
if (targetPos.GetFloorIntX() + targetBitmap->w > g_SceneMan.GetSceneWidth()) {
drawPositions.at(drawPasses) = drawPositions[0];
drawPositions.at(drawPasses).m_X += static_cast<float>(g_SceneMan.GetSceneWidth());
drawPasses++;
}
}
}
for (int i = 0; i < drawPasses; ++i) {
int spriteX = drawPositions.at(i).GetFloorIntX();
int spriteY = drawPositions.at(i).GetFloorIntY();
switch (mode) {
case g_DrawMaterial:
draw_character_ex(targetBitmap, m_aSprite[m_Frame], spriteX, spriteY, m_SettleMaterialDisabled ? GetMaterial()->GetIndex() : GetMaterial()->GetSettleMaterial(), -1);
break;
case g_DrawWhite:
draw_character_ex(targetBitmap, m_aSprite[m_Frame], spriteX, spriteY, g_WhiteColor, -1);
break;
case g_DrawTrans:
DrawTexture(m_aSprite[m_Frame], spriteX, spriteY, {255, 255, 255, g_FrameMan.GetCurrentAlpha()});
break;
case g_DrawAlpha:
DrawTexture(m_aSprite[m_Frame], spriteX, spriteY, {255, 255, 255, 255});
break;
case g_DrawMOID:
break;
default:
draw_sprite(targetBitmap, m_aSprite[m_Frame], spriteX, spriteY);
break;
}
g_SceneMan.RegisterDrawing(targetBitmap, m_MOID, spriteX, spriteY, spriteX + m_aSprite[m_Frame]->w, spriteY + m_aSprite[m_Frame]->h);
}
}