-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathObjectAnimation.cpp
More file actions
202 lines (185 loc) · 6.57 KB
/
ObjectAnimation.cpp
File metadata and controls
202 lines (185 loc) · 6.57 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
void Program::LoadAnimations(const std::string &name)
{
LoadAnimationsProject(name + "Classic.scproj");
LoadAnimationsProject(name + "Next.scproj");
}
void Program::LoadAnimationsProject(const std::string &name)
{
std::shared_ptr<ObjectAnimation> anim = std::make_shared<ObjectAnimation>();
anim->Name = name;
std::string animFileName = "./res/animations/" + name;
BinaryStreamReader f(animFileName.c_str());
f.ReadString(); //пропуск номера версии проекта
//спрайты
anim->Sprites.clear();
int32_t spr_count = f.ReadInt32();
for (int32_t i = 0; i < spr_count; i++)
{
std::shared_ptr<ObjectAnimation::Sprite> s = std::make_shared<ObjectAnimation::Sprite>();
s->Name = f.ReadString();
s->Type = f.ReadByte();
s->Width = f.ReadInt32();
s->Height = f.ReadInt32();
//монохромный спрайт с маской
if (s->Type == 0)
{
s->ClassicLayer = AllocateDynamicArray<int32_t>(s->Height, s->Width);
for (int32_t y = 0; y < s->Height; y++)
{
for (int32_t x = 0; x < s->Width; x++)
{
s->ClassicLayer[x][y] = f.ReadInt32();
}
}
}
//255-цветный спрайт в палитре Next
if (s->Type == 1)
{
s->NextLayer = AllocateDynamicArray<SdlDotNetCompat::Drawing::Color>(s->Height, s->Width);
for (int32_t y = 0; y < s->Height; y++)
{
for (int32_t x = 0; x < s->Width; x++)
{
s->NextLayer[x][y] = Color::FromArgb(f.ReadInt32());
}
}
}
anim->Sprites.push_back(s);
}
//анимации
anim->Animations.clear();
int32_t anim_count = f.ReadInt32();
for (int32_t i = 0; i < anim_count; i++)
{
std::shared_ptr<ObjectAnimation::Animation> a = std::make_shared<ObjectAnimation::Animation>();
a->Name = f.ReadString();
//кадры
int32_t frame_count = f.ReadInt32();
a->Frames = std::vector<std::shared_ptr<ObjectAnimation::Frame>>();
for (int32_t j = 0; j < frame_count; j++)
{
std::shared_ptr<ObjectAnimation::Frame> fr = std::make_shared<ObjectAnimation::Frame>();
//композиции
int32_t comp_count = f.ReadInt32();
fr->Composition = std::vector<std::shared_ptr<ObjectAnimation::CompositionElement>>();
for (int32_t k = 0; k < comp_count; k++)
{
std::shared_ptr<ObjectAnimation::CompositionElement> c = std::make_shared<ObjectAnimation::CompositionElement>();
c->ElemSprite = FindSprite(f.ReadString(), anim);
c->XOffset = f.ReadInt32();
c->YOffset = f.ReadInt32();
f.ReadBoolean(); //пропуск свойста View композиции
fr->Composition.push_back(c);
}
fr->DX1 = f.ReadInt32();
fr->DY1 = f.ReadInt32();
fr->DX2 = f.ReadInt32();
fr->DY2 = f.ReadInt32();
fr->HX1 = f.ReadInt32();
fr->HY1 = f.ReadInt32();
fr->HX2 = f.ReadInt32();
fr->HY2 = f.ReadInt32();
fr->Time = f.ReadInt32();
fr->Event = f.ReadString();
a->Frames.push_back(fr);
}
a->Loop = f.ReadBoolean();
anim->Animations.push_back(a);
}
ObjectAnimations.push_back(anim);
}
std::shared_ptr<Program::ObjectAnimation::Sprite> Program::FindSprite(const std::string &name, const std::shared_ptr<ObjectAnimation> &anim)
{
for (const auto &s : anim->Sprites)
{
if (s->Name == name)
{
return s;
}
}
return nullptr;
}
void Program::InitAnimation(const std::shared_ptr<GameObject> &obj, const std::string &name, bool hard_enable)
{
int32_t i = 0;
for (const auto &a : obj->Animations[sprite_mode]->Animations)
{
if (a->Name == name && (obj->CurrentAnimation != i || hard_enable))
{
obj->CurrentAnimation = static_cast<uint8_t>(i);
obj->Frame = 0;
obj->AnimCounter = static_cast<uint8_t>(obj->Animations[sprite_mode]->Animations[i]->Frames[0]->Time);
obj->AnimationPlayed = true;
GetObjSize(obj);
break;
}
i++;
}
}
void Program::InitAnimation(const std::shared_ptr<GameObject> &obj, uint8_t id_anim, bool hard_enable)
{
if (obj->CurrentAnimation != id_anim || hard_enable)
{
obj->CurrentAnimation = id_anim;
obj->Frame = 0;
obj->AnimCounter = static_cast<uint8_t>(obj->Animations[sprite_mode]->Animations[id_anim]->Frames[0]->Time);
obj->AnimationPlayed = true;
GetObjSize(obj);
}
}
void Program::PlayAnimation(const std::shared_ptr<GameObject> &obj)
{
if (!obj->AnimationPlayed || obj->NoVisible)
{
obj->NoInterruptAnim = false;
return;
}
obj->AnimCounter--;
if (obj->AnimCounter != 0)
{
return;
}
obj->Frame++;
if (obj->Frame == obj->Animations[sprite_mode]->Animations[obj->CurrentAnimation]->Frames.size())
{
if (obj->Animations[sprite_mode]->Animations[obj->CurrentAnimation]->Loop)
{
obj->Frame = 0;
}
else
{
obj->Frame--;
obj->AnimationPlayed = false;
return;
}
}
obj->AnimCounter = static_cast<uint8_t>(obj->Animations[sprite_mode]->Animations[obj->CurrentAnimation]->Frames[obj->Frame]->Time);
std::string e = obj->Animations[sprite_mode]->Animations[obj->CurrentAnimation]->Frames[obj->Frame]->Event;
GetObjSize(obj);
//обработка событий
DoEvent(obj, e);
}
void Program::GetObjSize(const std::shared_ptr<Program::GameObject> &obj)
{
obj->Width = obj->Animations[sprite_mode]->Animations[obj->CurrentAnimation]->Frames[obj->Frame]->DX2;
obj->Height = obj->Animations[sprite_mode]->Animations[obj->CurrentAnimation]->Frames[obj->Frame]->DY1;
}
std::vector<std::shared_ptr<Program::ObjectAnimation>> Program::FindObjectAnimation(const std::string &name)
{
std::vector<std::shared_ptr<ObjectAnimation>> anim(2);
for (const auto &a : ObjectAnimations)
{
if (a->Name == name + "Classic.scproj")
{
anim[0] = a;
}
}
for (const auto &a : ObjectAnimations)
{
if (a->Name == name + "Next.scproj")
{
anim[1] = a;
}
}
return anim;
}