-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
124 lines (93 loc) · 3.07 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
#include "raylib.h"
#include <iostream>
#include "raymath.h"
#include <random>
std::random_device rd;
std::default_random_engine eng(rd());
#define SCREEN_WIDTH 920
#define SCREEN_HEIGHT 800
#define ENTITY_AMOUNT 8000
#define ENTITY_RADIUS 7
#define ENTITY_SPEED 100.0f
Vector2 operator+(Vector2 lhs, Vector2 rhs){
return Vector2{lhs.x+rhs.x, lhs.y+rhs.y};
}
Vector2 operator-(Vector2 lhs, Vector2 rhs){
return Vector2{lhs.x-rhs.x, lhs.y-rhs.y};
}
Vector2 operator*(Vector2 lhs, float rhs){
return Vector2{lhs.x*rhs, lhs.y * rhs};
}
Vector2 operator/(Vector2 lhs, float rhs){
return Vector2{lhs.x/rhs, lhs.y/rhs};
}
Vector2 GetRandomVector2(){
std::uniform_real_distribution<float> distrX(0, SCREEN_WIDTH-ENTITY_RADIUS);
float x = distrX(eng);
std::uniform_real_distribution<float> distrY(0, SCREEN_HEIGHT-ENTITY_RADIUS);
float y = distrY(eng);
return Vector2{(float)x,(float)y};
}
class Entity;
Vector2 g_mousePos;
bool g_isHoldingLMB;
std::vector<Entity> g_entities(ENTITY_AMOUNT);
class Entity{
public:
Vector2 position;
Vector2 velocity;
Color color = BLACK;
Entity(){
this->position = GetRandomVector2();
}
void Move(){
Vector2 distanceVec = g_mousePos-this->position;
Vector2 dir = Vector2Normalize(distanceVec);
float distance = Vector2Length(distanceVec);
if(g_isHoldingLMB){
distance = -distance;
}
if(abs(distance) > 2.0f){
//blue gradient value mapping
float c = ((distance)/1500) * 255;
unsigned char b = 255-(int)c;
color = Color{0,0,b,255};
velocity = (dir / distance) * ENTITY_SPEED;
position = position + velocity;
}
}
private:
};
int main(void)
{
for(int i = 0; i < ENTITY_AMOUNT; i++){
g_entities[i] = Entity();
}
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Particle attraction");
SetTargetFPS(60);
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
g_mousePos = GetMousePosition();
g_isHoldingLMB = IsMouseButtonDown(MOUSE_BUTTON_LEFT);
for(int i = 0; i < ENTITY_AMOUNT; i++){
DrawCircleV(g_entities[i].position, ENTITY_RADIUS, g_entities[i].color);
g_entities[i].Move();
}
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}