added controls

This commit is contained in:
Jay Robson 2024-01-14 18:35:12 +11:00
parent be0d6cfacb
commit 5c4b8954a0
6 changed files with 106 additions and 3 deletions

View File

@ -43,16 +43,48 @@ int main()
for(int y = 0; y < reactor.height; y++)
{
std::stringstream ss;
ss << *reactor.rods[x][y];
sim::reactor::rod* r = reactor.rods[x][y];
ss << *r;
int px = X + (H - 1) * y;
int py = Y + (W - 1) * x;
display::draw_text(px + 1, py + 2, ss.str().c_str());
display::draw_box(px, py, H, W);
if(r->should_select() && x == reactor.cursor_x && y == reactor.cursor_y)
{
display::draw_text(px + 1, py + W - 5, "[ ]");
}
if(r->is_selected())
{
display::draw_text(px + 1, py + W - 4, "#");
}
}
refresh();
int c = getch();
switch(c)
{
case KEY_LEFT:
reactor.move_cursor(-1);
break;
case KEY_RIGHT:
reactor.move_cursor(1);
break;
case KEY_UP:
reactor.update_selected(0.001);
break;
case KEY_DOWN:
reactor.update_selected(-0.001);
break;
case ' ':
reactor.toggle_selected();
break;
}
}
return 0;

View File

@ -47,3 +47,11 @@ void control_rod::update(double secs)
absorbed += r_fast + r_slow;
}
void control_rod::update_selected(double a)
{
inserted += a;
if(inserted > 1) inserted = 1;
if(inserted < 0) inserted = 0;
}

View File

@ -24,6 +24,9 @@ public:
virtual void update(double secs);
void set_reactivity(double a);
virtual bool should_select() const { return true; }
virtual void update_selected(double a);
};
}

View File

@ -22,7 +22,7 @@ void pipe::update(double secs)
{
update_rod();
double v = vals[val_t::N_FAST] * 0.25;
double v = vals[val_t::N_FAST];
vals[val_t::N_FAST] -= v;
vals[val_t::N_SLOW] += v;
}

View File

@ -16,6 +16,9 @@ struct reactor
std::array<std::array<rod*, H>, W> rods;
int cursor_x = 0;
int cursor_y = 0;
reactor(std::array<rod*, W * H> rods)
{
for(int y = 0; y < H; y++)
@ -53,6 +56,56 @@ struct reactor
rods[x][y]->update(secs);
}
}
void update_selected(double v)
{
for(int y = 0; y < H; y++)
for(int x = 0; x < W; x++)
if(rods[x][y]->is_selected())
{
rods[x][y]->update_selected(v);
}
}
void move_cursor(int d)
{
for(;;)
{
cursor_x += d;
while(cursor_x >= W)
{
cursor_x -= W;
cursor_y += 1;
}
while(cursor_x < 0)
{
cursor_x += W;
cursor_y -= 1;
}
cursor_y %= H;
if(cursor_y < 0)
{
cursor_y += H;
}
if(rods[cursor_x][cursor_y]->should_select())
{
return;
}
}
}
void toggle_selected()
{
if(rods[cursor_x][cursor_y]->should_select())
{
rods[cursor_x][cursor_y]->toggle_selected();
}
}
};
}

View File

@ -25,11 +25,17 @@ public:
virtual double extract(val_t type, double k, double o);
virtual double get(val_t type) const;
virtual bool should_select() const { return false; }
virtual void update_selected(double a) { }
constexpr void toggle_selected() { selected = !selected; }
constexpr bool is_selected() { return selected; }
friend std::ostream& operator<<(std::ostream& o, const rod& r)
{
if(!r.should_display()) return o;
o << "Name: " << r.get_name() << "\n";
o << r.get_name() << "\n";
r.display(o);
o << "Heat: " << r.get(val_t::HEAT) << "\n";
o << "Fast: " << r.get(val_t::N_FAST) << "\n";
@ -42,6 +48,7 @@ protected:
double vals_in[VAL_N] = {0};
double vals[VAL_N] = {0};
bool selected = false;
virtual void display(std::ostream& o) const { };
virtual double get_k(val_t type) const { return 0; }