This repository has been archived by the owner on Nov 6, 2022. It is now read-only.
forked from sknjpn/SyLife
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEggState.cpp
78 lines (63 loc) · 2.15 KB
/
EggState.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
#include "EggState.h"
#include "CellAsset.h"
#include "CellState.h"
#include "PartAsset.h"
#include "PartConfig.h"
#include "Part_BodyAsset.h"
#include "World.h"
EggState::EggState(const std::shared_ptr<CellAsset>& cellAsset)
: m_cellAsset(cellAsset)
, m_timer(m_cellAsset->getBornTime()) {
setRadius(cellAsset->getRadius());
setMass(cellAsset->getMass());
setInertia(cellAsset->getInertia());
}
void EggState::updateEgg() {
m_timer -= DeltaTime;
// 衝突処理
{
auto result = World::GetInstance()->getEggStateKDTree().knnSearch(2, getPosition());
if (result.size() == 2) {
auto& t = World::GetInstance()->getEggStates()[result[1]];
if (t->getPosition() != getPosition() && (getRadius() + t->getRadius() - (t->getPosition() - getPosition()).length()) > 0) {
auto f = -1000.0 * (t->getPosition() - getPosition()).normalized() * (getRadius() + t->getRadius() - (t->getPosition() - getPosition()).length());
addForceInWorld(f, getPosition());
t->addForceInWorld(-f, t->getPosition());
}
}
}
// 孵化
if (m_timer <= 0) {
destroy();
const auto& c = World::GetInstance()->addCellState(getCellAsset());
c->setPosition(getPosition());
c->setRotation(getRotation());
}
}
void EggState::draw1() {
const double stage = 1.0 - m_timer / m_cellAsset->getBornTime();
m_cellAsset->getCellAssetTexture()
.scaled(1.0 / GeneralSetting::GetInstance().m_textureScale)
.scaled(Math::Lerp(0.125, 0.15, stage))
.rotated(getRotation())
.drawAt(getPosition(), ColorF(1.0, 0.5));
}
void EggState::draw2() {
const double stage = 1.0 - m_timer / m_cellAsset->getBornTime();
Circle(getPosition(), getRadius() * 2.0 * Math::Lerp(0.125, 0.25, stage))
.draw(ColorF(Palette::Lightblue, 0.25));
}
void EggState::load(Deserializer<BinaryReader>& reader) {
Rigidbody::load(reader);
{
String cellAssetName;
reader >> cellAssetName;
m_cellAsset = World::GetAsset<CellAsset>(cellAssetName);
}
reader >> m_timer;
}
void EggState::save(Serializer<MemoryWriter>& writer) const {
Rigidbody::save(writer);
writer << m_cellAsset->getName();
writer << m_timer;
}