summaryrefslogtreecommitdiff
path: root/src/game
diff options
context:
space:
mode:
Diffstat (limited to 'src/game')
-rw-r--r--src/game/game.cpp65
-rw-r--r--src/game/interface.cpp36
2 files changed, 86 insertions, 15 deletions
diff --git a/src/game/game.cpp b/src/game/game.cpp
index f21bccd..27db5e5 100644
--- a/src/game/game.cpp
+++ b/src/game/game.cpp
@@ -91,7 +91,6 @@ void state_t::start(void)
soldier->place(&world, v2f_t(1.5, 0.5));
soldier = new unit_soldier_t(this);
soldier->place(&world, v2f_t(2.5, 0.5));
- select(rectf_t(v2f_t(0.5, 0.5), v2f_t(2.5, 0.5)));
resume();
}
@@ -105,12 +104,53 @@ void state_t::group_say(std::string text)
interface->print(text::get(text::SAY_GROUP) + ": " + text);
}
-void state_t::select(rectf_t x)
+void state_t::select_unit(unit_t *unit, int type)
{
- selection_cookie++;
- selected_units.clear();
+ switch (type) {
+ case SELECT_NEW:
+ case SELECT_OR:
+ if (unit->selected == selection_cookie)
+ return;
+ else
+ goto do_select;
+
+ case SELECT_XOR:
+ if (unit->selected == selection_cookie)
+ goto do_deselect;
+ else
+ goto do_select;
+ }
+
+do_select:
+ unit->selected = selection_cookie;
+ selected_units.insert(unit);
+ return;
+
+do_deselect:
+ unit->selected = selection_cookie - 1;
+ selected_units.erase(unit);
+}
+
+void state_t::select(rectf_t rect, int type)
+{
+ size_t before;
+ bool select_one;
+ std::list<world::entity_t*> ents;
+
+ before = selected_units.size();
- for (world::entity_t *ent : world.get_render_entities(x)) {
+ if (type == SELECT_NEW) {
+ selection_cookie++;
+ selected_units.clear();
+ }
+
+ select_one = rect.area() < 0.2f;
+
+ ents = world.get_render_entities(rect);
+ if (select_one)
+ ents.sort(render::visibility_order);
+
+ for (world::entity_t *ent : ents) {
unit_t *unit;
if (ent->type != ET_UNIT)
@@ -121,14 +161,17 @@ void state_t::select(rectf_t x)
if (!unit->controllable)
continue;
- unit->selected = selection_cookie;
- selected_units.insert(unit);
+ select_unit(unit, type);
+ if (select_one)
+ break;
}
- if (selected_units.size() == 1)
- (*selected_units.begin())->say(text::get(text::SAY_READY));
- else if (selected_units.size() > 1)
- group_say(text::get(text::SAY_READY_GROUP));
+ if (selected_units.size() > before) {
+ if (selected_units.size() == 1)
+ (*selected_units.begin())->say(text::get(text::SAY_READY));
+ else if (selected_units.size() > 1)
+ group_say(text::get(text::SAY_READY_GROUP));
+ }
}
enum {
diff --git a/src/game/interface.cpp b/src/game/interface.cpp
index 25d256c..8a02e98 100644
--- a/src/game/interface.cpp
+++ b/src/game/interface.cpp
@@ -114,6 +114,15 @@ void pie_menu_t::update(v2f_t mouse)
}
}
+static int get_selection_type(void)
+{
+ if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::LControl))
+ return game::SELECT_OR;
+ if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::LShift))
+ return game::SELECT_XOR;
+ return game::SELECT_NEW;
+}
+
void state_t::tick(double dt)
{
vec_t<int, 2> window_size;
@@ -205,7 +214,7 @@ void state_t::tick(double dt)
switch (event.mouseButton.button) {
case sf::Mouse::Button::Left:
if (select.selecting)
- game->select(select.rect);
+ game->select(select.rect, select.type);
select.selecting = false;
break;
@@ -279,8 +288,10 @@ void state_t::tick(double dt)
}
}
- if (select.selecting)
+ if (select.selecting) {
+ select.type = get_selection_type();
select.rect[1] = wmouse;
+ }
pie_menu.update(mouse);
}
@@ -327,8 +338,25 @@ void state_t::render_to(render::state_t *render)
std::stringstream ss;
double fps;
- if (select.selecting)
- render->render_hlrect(select.rect, sf::Color(60, 60, 150));
+ if (select.selecting) {
+ sf::Color color;
+
+ switch (select.type) {
+ case SELECT_NEW:
+ color = sf::Color(60, 60, 150);
+ break;
+
+ case SELECT_OR:
+ color = sf::Color(60, 150, 60);
+ break;
+
+ case SELECT_XOR:
+ color = sf::Color(150, 150, 60);
+ break;
+ }
+
+ render->render_hlrect(select.rect, color);
+ }
window->setView(sf::View(sf::FloatRect(0, 0, w, h)));
em = std::max(w, h) * 0.017;