Repository navigation
Expand file tree
/
Copy pathdriver.cpp
More file actions
144 lines (115 loc) · 2.93 KB
/
Copy pathdriver.cpp
File metadata and controls
144 lines (115 loc) · 2.93 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
/* Driver program using olc::PixelGameEngine
TODO
*/
#define OLC_PGE_APPLICATION
#include "collision.h"
#include "particles.h"
#include "PixelGameEngine/olcPixelGameEngine.h"
//track key presses
enum KeyDown {
SandDown,
WaterDown,
SolidDown,
};
class Engine : public olc::PixelGameEngine
{
//vector of particles, holds all instances on screen
//shared_ptr inorder to use Polymorphism
std::vector<std::shared_ptr<Particle>> particles;
//Colors
olc::Pixel sandColor;
olc::Pixel waterColor;
olc::Pixel solidColor;
//Tracking which particle to instantiate
//with key pressed
unsigned
int CurrentKeyDown;
public:
Engine(){ sAppName = "SandGame"; }
public:
bool OnUserCreate() override
{
//seed
srand(time(0));
//set colors
sandColor.r = 255;
sandColor.g = 230;
sandColor.b = 140;
waterColor = olc::BLUE;
solidColor = olc::DARK_GREY;
//Set Default KeyState
CurrentKeyDown = SandDown;
//instantiating a global CollisionBoard object
//passed into Particle Class
Particle::collision = new CollisionBoard(
ScreenHeight(), ScreenWidth());
return true;
}
bool OnUserUpdate(float fElapsedTime) override
{
Particle::dt = fElapsedTime * 1000;
Clear(olc::Pixel(40, 40, 40));
//If 'C' Pressed, Clear all particles on screen
if (GetKey(olc::C).bPressed)
{
Particle::collision->clear(); // board
particles.clear(); // particles
}
//Update settings - Which particle to instantiate
if (GetKey(olc::K1).bPressed) CurrentKeyDown = SandDown;
if (GetKey(olc::K2).bPressed) CurrentKeyDown = WaterDown;
if (GetKey(olc::K3).bPressed) CurrentKeyDown = SolidDown;
//When mouse held , instantiate particles
if (GetMouse(0).bHeld)
{
switch (CurrentKeyDown)
{
case SandDown:
particles.push_back(
std::make_shared<Sand>(GetMouseX(), GetMouseY()));
break;
case WaterDown:
particles.push_back(
std::make_shared<Water>(GetMouseX(), GetMouseY()));
break;
case SolidDown:
particles.push_back(
std::make_shared<Solid>(GetMouseX(), GetMouseY()));
break;
}
}
//Functionality for each particle in vector
for (auto particle: particles)
{
// check bounds and idle status
if (particle->y < ScreenHeight() - 1 && !particle->idle)
particle->updateParticle(); //update
//draw a pixel depending on the particle's type
switch (particle->getDrawType())
{
case DrawSand:
Draw(particle->x, particle->y, sandColor);
break;
case DrawWater:
Draw(particle->x, particle->y, waterColor);
break;
case DrawSolid:
Draw(particle->x, particle->y, solidColor);
break;
}
}
return true; //for olc::game engine
}
bool onUserDestroy()
{
//free memory
delete Particle::collision;
}
};
int main()
{
Engine game;
if (game.Construct(150, 150, 6, 6)) /* FEEL FREE TO MESS AROUND WITH DIMENSIONS! */
game.Start();
return 0;
}