-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlab.cpp
148 lines (133 loc) · 4.66 KB
/
lab.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
#include "lab.hpp"
#include "bot_specs.hpp"
#include "button.hpp"
#include "inst.hpp"
#include "rend.hpp"
#include "research_item.hpp"
#include "screen.hpp"
#include "text_button.hpp"
#include "ui.hpp"
#include "world.hpp"
#include <coeff/coeff_reg.hpp>
#include <shade/library.hpp>
#include <shade/obj.hpp>
#include <shade/shader_program.hpp>
#include <shade/text.hpp>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/transform.hpp>
COEFF(LabFontSize, 40.0f);
static std::vector<glm::vec3> getPlaneMesh()
{
std::vector<glm::vec3> res;
res.push_back(glm::vec3(0.0f, 0.0f, 0.0f));
res.push_back(glm::vec3(1.0f, 1.0f, 0.0f));
res.push_back(glm::vec3(1.0f, 0.0f, 0.0f));
res.push_back(glm::vec3(0.0f, 0.0f, 0.0f));
res.push_back(glm::vec3(0.0f, 1.0f, 0.0f));
res.push_back(glm::vec3(1.0f, 1.0f, 0.0f));
return res;
}
class BgPlane: public Widget
{
public:
BgPlane(Rend &aRend, int x, int y, int w, int h)
: Widget(x, y, w, h), rend(&aRend), planeMesh(getPlaneMesh(), 0), title(*rend->lib, "font")
{
title.setText("Research");
}
void draw() override
{
rend->mvp = glm::translate(glm::vec3(x, y, -2.0f)) * glm::scale(glm::vec3(w, h, 1.0f));
rend->time = SDL_GetTicks();
rend->planeShad->use();
planeMesh.activate();
glDrawArrays(GL_TRIANGLES, 0, 6);
rend->textShad->use();
rend->mvp =
glm::translate(glm::vec3((ScreenWidth - title.getWidth() * 30) / 2.0f, y - 30, -2.0f)) *
glm::scale(glm::vec3(30, 30, 30.0f));
rend->mvp.update();
title.draw();
}
private:
Rend* rend;
ArrayBuffer planeMesh;
Text title;
};
static const int DialogY = 75;
static const int DialogWidth = 360;
Lab::Lab(Library &lib, Inst &inst, Ui &aUi, BotSpecs &specs)
: crossIcon(lib.getObj("cross")),
closeBtn(std::make_unique<Button>((ScreenWidth - DialogWidth) / 2 - 20, DialogY + 5 - 20, 40, 40)),
bgPlane(std::make_unique<BgPlane>(*inst.rend,
(ScreenWidth - DialogWidth) / 2,
DialogY + 5,
DialogWidth,
ScreenHeight - 180 - DialogY)),
ui(&aUi)
{
closeBtn->onDraw = [this, &inst](bool pressed) {
inst.rend->mvp = glm::translate(glm::vec3((1.0f * ScreenWidth - DialogWidth) / 2.0f, 1.0f * DialogY + 5.0f, 20.0f)) *
glm::scale(glm::vec3(20.0f, 20.0f, 20.0f) * (pressed ? 1.3f : 1.0f));
inst.rend->uiShad->use();
crossIcon->draw();
};
closeBtn->onClick = [this]() { hide(); };
int y = DialogY;
auto const BtnHeight = TextButton::FontSize * 2 * 110 / 100;
auto createResearchItem = [&](int &value,
const std::string &description,
float k,
const std::string &unit,
float improvement = 5.0f) {
researchItems.push_back(std::make_pair(
std::make_unique<ResearchItem>(value, description, k, unit, improvement),
std::make_unique<TextButton>(
*inst.rend, lib, (ScreenWidth - DialogWidth) / 2, y, DialogWidth, BtnHeight)));
auto &&item = researchItems.back().first.get();
auto &&btn = researchItems.back().second.get();
btn->setText(item->getText());
btn->onClick = [item, &inst, btn, &specs]() {
if (inst.world->money < item->cost)
return;
inst.world->money-= item->cost;
item->research();
btn->setText(item->getText());
specs.botPrice = specs.botPrice * 1010 / 1000;
};
y += BtnHeight;
};
createResearchItem(specs.lifeSpan, "Increase bot's robustness", 1.0f / 100.0f, " s");
createResearchItem(specs.batteryCapacity, "Increase battery capacity", 5.0f / 1000.0f, " Ah");
createResearchItem(specs.chargeRate, "Better solar panels", 0.25f, " mA");
createResearchItem(specs.speed, "Increase bot's speed", 3.0f / 1100.0f, " MPH");
createResearchItem(specs.buildTime, "Reduce build time", 1.0f / 100.0f, " s", -5.0f);
createResearchItem(specs.o2PlantLifeSpan, "Build stronger O2 plant", 1.0f / 100.0f, " s");
createResearchItem(specs.h2OPlantLifeSpan, "Build stronger H2O plant", 1.0f / 100.0f, " s");
createResearchItem(specs.o2PlantProdRate, "Make plants produce more O2", 0.01f, " t/s");
createResearchItem(specs.h2OPlantProdRate, "Make plants produce more H2O", 0.01f, " t/s");
}
void Lab::show()
{
ui->add(*bgPlane);
ui->add(*closeBtn);
for (auto &&research : researchItems)
ui->add(*research.second);
visible = true;
}
void Lab::hide()
{
ui->remove(*bgPlane);
ui->remove(*closeBtn);
for (auto &&research : researchItems)
ui->remove(*research.second);
visible = false;
}
Lab::~Lab()
{
hide();
}
bool Lab::isVisible() const
{
return visible;
}