-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
223 lines (187 loc) · 6.55 KB
/
Main.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
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
214
215
216
217
218
219
220
221
222
223
# include <Siv3D.hpp>
# include <HamFramework.hpp>
struct Smoke : IEffect
{
Vec2 m_pos, m_speed;
double m_scale;
Smoke(const Vec2& pos, Vec2 dir, double scale)
: m_pos(pos)
, m_speed(dir.setLength(20).rotated(Random(-0.3, 0.3) * scale))
, m_scale(scale) {}
bool update(double t) override
{
TextureAsset(L"Particle").scale((0.5 + 4 * t) * m_scale * 0.1).drawAt(m_pos + m_speed * t, AlphaF(Pow(1.0 - t / 0.4, 4)));
return t < 0.4;
}
};
void Main()
{
Window::SetTitle(L"Lunar Excursion | CONTROL: [Q][Z][Space][M][O] | RESET: [Enter]");
const ColorF skyColorTop(0.08, 0.04, 0.08), skyColorBottom(0.35, 0.25, 0.35);
Graphics::SetBackground(skyColorTop);
Window::Resize(1280, 720);
ScalableWindow::Setup(1280, 720);
TextureAsset::Register(L"Particle", L"Particle.png", TextureDesc::Mipped);
Effect effect;
Array<Circle> stars;
for (int32 i = 0; i < 200; ++i)
{
stars.emplace_back(RandomVec2(Rect(-80, -1, 160, 80)), Random(0.05, 0.2));
}
PerlinNoise noise;
Array<Vec2> groundPoints;
for (int32 i = -220; i <= 220; ++i)
{
const double h = Abs(i) < 6 ? 0.0 : noise.octaveNoise(i / 8.0, 4);
groundPoints.emplace_back(i, h);
}
PhysicsWorld world(Vec2(0, -0.5));
auto ground = world.createLineString(Vec2(0, 0), groundPoints, none, none, PhysicsBodyType::Static);
std::reverse(groundPoints.begin(), groundPoints.end());
groundPoints.emplace_back(-220, -20);
groundPoints.emplace_back(220, -20);
const Polygon groundPolygon(groundPoints);
const Polygon polygon({ { -0.8, 2 },{ -1.6, -0.2 },{ -1.6, -1.0 },
{ -2.2, -0.2 },{ -2.8, -3 },{ -2.6, -3.4 },{ -2.0, -1 },
{ -1.6, -1.4 },{ -1.6, -2 },{ 1.6, -2 },{ 1.6, -1.4 },
{ 2.0, -1 },{ 2.6, -3.4 },{ 2.8, -3 },{ 2.2, -0.2 },
{ 1.6, -1.0 },{ 1.6, -0.2 },{ 0.8, 2 }
});
PhysicsBody rocket = world.createPolygon(Vec2(-8, 14), polygon)
.addCircle(Circle(-2.7, -3.2, 0.4))
.addCircle(Circle(2.7, -3.2, 0.4));
CameraBox2D camera;
CameraBox2D cameraStars;
const Font fontTitle(90, Typeface::Thin),fontDistance(50, Typeface::Light), fontAngle(40, Typeface::Light);
Vec2 previousSpeed;
double damage = 0.0, fuel = 100.0;
bool onLine = false, launched = false;
while (System::Update())
{
if (System::FrameCount() == 1 || Input::KeyEnter.clicked)
{
rocket.setPos(-160, Random(46.0, 54.0));
rocket.setAngle(Random(-30_deg, 30_deg));
rocket.setAngularVelocity(0.0);
rocket.setVelocity(previousSpeed = Vec2(Random(6.0, 6.6), 0));
camera = CameraBox2D(rocket.getPos(), 8.0);
cameraStars = CameraBox2D();
fuel = 100.0;
damage = 0.0;
onLine = launched = false;
}
const Vec2 rocketPos = rocket.getPos();
const double rocketAngle = rocket.getAngle();
if (fuel > 0.0 || rocketPos.y > 100)
{
if (Input::KeyQ.pressed)
{
const Vec2 pos = rocketPos + Vec2(-0.8, 2).rotate(rocketAngle);
const Vec2 dir = Vec2(2, -0.5).rotate(rocketAngle);
rocket.applyForceAt(Vec2(2, -0.5).rotate(rocketAngle), pos);
effect.add<Smoke>(pos, -dir, 0.5);
fuel = Max(fuel - 0.005, 0.0);
}
if (Input::KeyZ.pressed)
{
const Vec2 pos = rocketPos + Vec2(-1.6, -2).rotate(rocketAngle);
const Vec2 dir = Vec2(2, -0.5).rotate(rocketAngle);
rocket.applyForceAt(Vec2(2, -0.5).rotate(rocketAngle), pos);
effect.add<Smoke>(pos, -dir, 0.5);
fuel = Max(fuel - 0.005, 0.0);
}
if (Input::KeySpace.pressed)
{
const Vec2 pos = rocketPos - Vec2(0, 2).rotate(rocketAngle);
const Vec2 dir = Vec2(0, 2).rotate(rocketAngle);
rocket.applyForce(Vec2(0, 20).rotate(rocketAngle));
effect.add<Smoke>(pos, -dir, 1.0);
fuel = Max(fuel - 0.04, 0.0);
}
if (Input::KeyM.pressed)
{
const Vec2 pos = rocketPos + Vec2(1.6, -2).rotate(rocketAngle);
const Vec2 dir = Vec2(-2, -0.5).rotate(rocketAngle);
rocket.applyForceAt(Vec2(-2, -0.5).rotate(rocketAngle), pos);
effect.add<Smoke>(pos, -dir, 0.5);
fuel = Max(fuel - 0.005, 0.0);
}
if (Input::KeyO.pressed)
{
const Vec2 pos = rocketPos + Vec2(0.8, 2).rotate(rocketAngle);
const Vec2 dir = Vec2(-2, -0.5).rotate(rocketAngle);
rocket.applyForceAt(Vec2(-2, -0.5).rotate(rocketAngle), pos);
effect.add<Smoke>(pos, -dir, 0.5);
fuel = Max(fuel - 0.005, 0.0);
}
}
world.update();
// ダメージ計算
const double delta = rocket.getVelocity().distanceFrom(std::exchange(previousSpeed, rocket.getVelocity()));
if (delta >= 0.05)
{
const double a = Abs(rocketAngle);
damage = Min(damage + delta * (a < 2_deg ? 0.75 : a < 30_deg ? 1.0 : a < 50_deg ? 2.0 : 5.0), 99.9);
}
camera.setPos(Vec2(rocketPos.x + 20, 2 + Max(14.0, rocketPos.y * 0.55)));
camera.setScale(16.0 - Min(rocketPos.y * 0.1, 8.0));
cameraStars.setPos(Vec2(rocketPos.x * 0.02, 37));
cameraStars.setScale(10);
{
const auto t1 = cameraStars.createTransformer();
const auto t2 = ScalableWindow::CreateTransformer();
// 宇宙
Rect(-200, -4, 400, 24).draw({ skyColorBottom, skyColorBottom, skyColorTop, skyColorTop });
// 星
for (const auto& star : stars)
{
star.draw();
}
}
{
const auto t1 = camera.createTransformer();
const auto t2 = ScalableWindow::CreateTransformer();
const Polygon rocketPolygon = rocket.shapeAs<B2Polygon>(0)->getPolygon();
// 距離ライン
for (int32 i = -3; i <= 3; ++i)
{
const double a = (0.75 - Pow(Abs(i) * 0.3, 0.4) * 0.75) * Min(1.0, 4.0 - rocketPos.y * 0.05);
const Line line(i * 20, -400, i * 20, 400);
if (i == 0)
{
onLine = line.intersects(rocketPolygon);
line.draw(0.8, onLine ? HSV(System::FrameCount(), 0.5, 1).toColorF(a) : AlphaF(a));
}
else
{
line.draw(0.8, AlphaF(a));
}
}
// 月面
ground.draw();
groundPolygon.draw(Color(200, 200, 160));
// 機体
effect.update();
rocketPolygon.drawFrame(0.2, Color(200, 220, 250));
rocket.draw(Lerp(Palette::Skyblue, Color(60), damage/100.0));
}
{
const auto t2 = ScalableWindow::CreateTransformer();
fontDistance(L"{:.1f}m"_fmt, rocketPos.x * -4).draw(780, 40, Alpha(80));
fontAngle(L"{:.1f}°"_fmt, Degrees(-rocketAngle)).draw(1080, 55, Alpha(80));
fontAngle(L"FUEL: {:.1f}%"_fmt, fuel).draw(780, 140, Alpha(80));
fontAngle(L"DAMAGE: {:.1f}%"_fmt, damage).draw(780, 200, Alpha(80));
}
if (onLine && rocketPos.y > 100)
{
launched = true;
}
if(launched)
{
const double alpha = Min((rocketPos.y - 100.0) / 10.0, 1.0) * 0.75;
fontTitle(L"Lunar").drawCenter(Window::BaseCenter().x, Window::BaseCenter().y + 40 , ColorF(1, 1, 0, alpha));
fontTitle(L"Excursion").drawCenter(Window::BaseCenter().x, Window::BaseCenter().y + 200, ColorF(1, 1, 0, alpha));
}
ScalableWindow::DrawBlackBars();
}
}