-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJackpot.cpp
More file actions
172 lines (136 loc) · 3.67 KB
/
Jackpot.cpp
File metadata and controls
172 lines (136 loc) · 3.67 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
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
#include "../Include/defGameEngine.hpp"
#include <array>
class Jackpot : public def::GameEngine
{
public:
Jackpot()
{
GetWindow()->SetTitle("Jackpot");
}
def::Vector2i machinePos = { 30, 60 };
def::Vector2i machineCellSize = { 16, 16 };
enum CellType : uint8_t
{
RED,
BLUE,
GREEN
};
template <size_t itemsPerCol>
struct Line
{
float offset = 0.0f;
float maxOffset = 0.0f;
std::array<uint8_t, itemsPerCol> content;
};
std::vector<Line<3>> lines;
def::Graphic* field;
int yBorder = 0;
int score = 0;
protected:
int RandInt(int rmin, int rmax)
{
return rmin + rand() % (rmax - rmin);
}
bool OnUserCreate() override
{
srand(time(0));
lines.resize(3);
for (auto& l : lines)
{
l.offset = (float)RandInt(0, 3);
l.maxOffset = (float)RandInt(15, 20);
for (int i = 0; i < l.content.size(); i++) l.content[i] = i;
}
field = new def::Graphic({ 3 * machineCellSize.x + 1, 3 * machineCellSize.y + 1 });
return true;
}
bool OnUserUpdate(float dt) override
{
// Spin wheels again if they stopped
if (lines.back().offset >= lines.back().maxOffset)
{
if (GetInput()->GetKeyState(def::Key::SPACE).pressed)
{
for (auto& l : lines)
{
l.offset = float((int)l.offset % l.content.size());
l.maxOffset = (float)RandInt(15, 20);
}
}
else
return true;
}
for (auto& l : lines)
{
// Update current line offset
// until it reaches maximum
if (l.offset <= l.maxOffset)
{
l.offset += 5.0f * dt;
break;
}
}
yBorder = machineCellSize.y * 3;
SetDrawTarget(field);
Clear(def::BROWN);
def::Vector2i p;
for (auto& l : lines)
{
for (p.y = 0; p.y < l.content.size(); p.y++)
{
def::Pixel col;
switch (l.content[p.y])
{
case RED: col = def::RED; break;
case BLUE: col = def::BLUE; break;
case GREEN: col = def::GREEN; break;
}
def::Vector2i pos = (p + def::Vector2i(0.0f, l.offset)) * machineCellSize;
FillRectangle(pos, machineCellSize, col);
DrawRectangle(pos, machineCellSize, def::BROWN);
}
p.x++;
}
for (int i = 0; i < 3; i++)
DrawRectangle({ machineCellSize.x * i, machineCellSize.y }, machineCellSize, def::YELLOW);
SetDrawTarget(nullptr);
yBorder = GetWindow()->GetScreenHeight();
if (lines.back().offset >= lines.back().maxOffset)
{
def::Pixel p0 = field->sprite->GetPixel({ machineCellSize.x * 0 + 1, machineCellSize.y + 1 });
def::Pixel p1 = field->sprite->GetPixel({ machineCellSize.x * 1 + 1, machineCellSize.y + 1 });
def::Pixel p2 = field->sprite->GetPixel({ machineCellSize.x * 2 + 1, machineCellSize.y + 1 });
if (p0 == p1 && p1 == p2)
{
if (p0 == def::RED) score += 20;
if (p0 == def::BLUE) score += 40;
if (p0 == def::GREEN) score += 100;
}
}
Clear(def::DARK_CYAN);
DrawString(machinePos - def::Vector2i(0, 12), "Score: " + std::to_string(score));
DrawSprite(machinePos, field->sprite);
int y = 0;
FillRectangle(machinePos + def::Vector2i(3 * machineCellSize.x + 6, y), { 5, 5 }, def::RED);
DrawString(machinePos + def::Vector2i(3 * machineCellSize.x + 14, y), "20");
y += 12;
FillRectangle(machinePos + def::Vector2i(3 * machineCellSize.x + 6, y), { 5, 5 }, def::BLUE);
DrawString(machinePos + def::Vector2i(3 * machineCellSize.x + 14, y), "40");
y += 12;
FillRectangle(machinePos + def::Vector2i(3 * machineCellSize.x + 6, y), { 5, 5 }, def::GREEN);
DrawString(machinePos + def::Vector2i(3 * machineCellSize.x + 14, y), "100");
y += 12;
return true;
}
virtual bool Draw(int32_t x, int32_t y, const def::Pixel& p) override
{
return def::GameEngine::Draw(x, y % yBorder, p);
}
};
int main()
{
Jackpot demo;
demo.Construct(256, 240, 4, 4);
demo.Run();
return 0;
}