-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrag_and_wrap.cpp
151 lines (124 loc) · 3.1 KB
/
drag_and_wrap.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
// based on: https://www.khanacademy.org/computer-programming/asteroid-game/1486828627
// I, J, K, L to move, mouse to aim and shoot.
#include "common/sketchbook.hpp"
constexpr float light_speed = 40;
constexpr float drag_factor = 0.1;
float2 drag(float2 velocity, float max_speed = light_speed, float factor = drag_factor)
{
return factor * velocity/max_speed;
}
struct projectile
{
float2 position = float2::zero();
float2 velocity = float2::zero();
float2 acceleration = float2::zero();
void move(Program::duration)
{
velocity += acceleration;
position += velocity;
}
};
struct line : public projectile
{
float width = 1;
rgb color = rgb(0xff00ff_rgb);
void draw(vg::frame& frame)
{
frame.begin_sketch()
.line(position - velocity, position)
.line_width(width)
.outline(color)
;
}
};
struct circle : public projectile
{
float radius = 10;
rgb color = rgb(0xff00ff_rgb);
void draw(vg::frame& frame)
{
frame.begin_sketch()
.ellipse(rect{radius * float2::one(), position, float2::one(0.5)})
.fill(color)
;
}
};
using body = std::variant<line, circle>;
std::vector<body> bodies;
circle* crc = nullptr;
void start(Program& program)
{
program.frametime = framerate<60>::frametime;
if(program.argc > 2)
{
using support::ston;
using seed_t = decltype(tiny_rand());
tiny_rand.seed({ ston<seed_t>(program.argv[1]), ston<seed_t>(program.argv[2]) });
}
std::cout << "seed: " << std::hex << std::showbase << tiny_rand << '\n';
bodies.push_back(circle{float2(program.size/2)});
bodies.reserve(10000);
crc = &std::get<circle>(bodies.back());
program.key_up = [&](scancode code, keycode)
{
switch(code)
{
case scancode::leftbracket:
case scancode::c:
if(pressed(scancode::rctrl) || pressed(scancode::lctrl))
case scancode::escape:
program.end();
break;
default: break;
}
};
program.key_down = [&](scancode code, keycode)
{
switch(code)
{
case scancode::i:
break;
case scancode::j:
break;
case scancode::k:
break;
case scancode::l:
break;
default: break;
}
};
program.mouse_down = [&](float2 position, auto)
{
if(bodies.size() < bodies.capacity())
bodies.push_back(line{crc->position, (position - crc->position) * 0.1f});
};
program.draw_loop = [&](auto frame, auto delta_time)
{
frame.begin_sketch()
.rectangle(rect{frame.size})
.fill(rgb::white(0))
;
crc->acceleration = float2::zero();
if(pressed(scancode::i))
crc->acceleration -= float2::j(0.1);
if(pressed(scancode::k))
crc->acceleration += float2::j(0.1);
if(pressed(scancode::j))
crc->acceleration -= float2::i(0.1);
if(pressed(scancode::l))
crc->acceleration += float2::i(0.1);
for(auto&& body : bodies)
{
std::visit( [&frame, &delta_time] (auto& body)
{
body.move(delta_time);
body.draw(frame);
body.velocity -= drag(body.velocity);
constexpr float padding = 5;
auto fullsize = frame.size + 2 * padding;
body.position = ((fullsize + (body.position + padding)) % fullsize) - padding;
}, body);
}
std::cout << std::dec << "Size: " << bodies.size() << " FPS: " << 1/delta_time.count() << '\n';
};
}