void MouseSystem::Update() { World* world = engine->GetContext()->GetWorld(); //Grids made as boundaries for the level Grid upperLeft(0, 0); Grid bottomRight(world->GetHeight(), world->GetWidth()); if (engine->GetContext()->GetGameState() == Context::PL_PLAY) { //Hover event if (al->IsMouseEvent()) { ALLEGRO_EVENT ev = al->GetCurrentEvent(); Vector2 vector2(ev.mouse.x, ev.mouse.y); Grid grid = Graphics::Instance().FromPx(vector2); if (grid < bottomRight && grid >= upperLeft) { //Update the old position of the selectionTile. Entity* selectedTile = engine->GetEntityStream()->EntityWithTags({ Component::SELECTION, Component::HOVER }); PositionComponent* pc = dynamic_cast<PositionComponent*>(selectedTile->GetComponent(Component::POSITION)); pc->pos = Graphics::Instance().ToPx(grid); engine->UpdateEntity(selectedTile); } } //Click Event else if (al->IsMouseClickEvent()) { // left click if (al->GetCurrentEvent().mouse.button == 1) { ALLEGRO_EVENT ev = al->GetCurrentEvent(); Vector2 vector2(ev.mouse.x, ev.mouse.y); Grid grid = Graphics::Instance().FromPx(vector2); if (grid < bottomRight && grid >= upperLeft) { //Select a tile by getting it from the matrix in world and checking the entitystream to know if there's a unit on the tile. Entity* tile = world->getEntity(grid.row, grid.col); std::set<Entity*> entities = engine->GetEntityStream()->WithTag(Component::UNIT); for (Entity* ent : entities) { PositionComponent* position = dynamic_cast<PositionComponent*>(ent->GetComponent(Component::POSITION)); if (grid == Graphics::Instance().FromPx(position->pos)) { tile = ent; } } //There was a unit selected. Entity* currentUnit = engine->GetEntityStream()->EntityWithTags({ Component::SELECTION, Component::UNIT }); if (currentUnit != NULL) { //Attack if (tile->GetComponent(Component::UNIT) != NULL && ((UnitComponent*)tile->GetComponent(Component::UNIT))->player == Entity::Player::AI) { Attack(currentUnit, vector2, tile); } else if (tile->GetComponent(Component::UNIT) != NULL && ((UnitComponent*)tile->GetComponent(Component::UNIT))->player == Entity::Player::HUMAN && ((UnitComponent*)tile->GetComponent(Component::UNIT))->type != Entity::UnitType::HQ && ((UnitComponent*)tile->GetComponent(Component::UNIT))->ap>0) { SelectUnit(tile); } //Move the unit. else if (tile->GetComponent(Component::UNIT) == NULL) { //Calculate path to selected tile std::vector<Grid> path = engine->GetContext()->GetPathfinder()->FindPath(currentUnit, tile); Move(currentUnit, vector2, path); } } //There was no unit selected. else { //Select the unit if its a unit. The unit should not be a HQ. if (tile->GetComponent(Component::UNIT) != NULL && ((UnitComponent*)tile->GetComponent(Component::UNIT))->player == Entity::Player::HUMAN && ((UnitComponent*)tile->GetComponent(Component::UNIT))->type != Entity::UnitType::HQ && ((UnitComponent*)tile->GetComponent(Component::UNIT))->ap > 0 ) { SelectUnit(tile); } } } } // right click else if (al->GetCurrentEvent().mouse.button == 2) { DeselectCurrentUnit(); } } } }