I can select you!


yeah this red triangle means – I am selected.. I will be working on sprites manager over the weekend..
At least.. I have managed to select sprites inside of box described by start point and end point – both defined by mouse click and release.

Class probably will be improved in the future but now.. welcome to AddToSelected method

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
void PlayerManager::addEntityToSelected(POINT mouseBoxStart, POINT mouseBoxEnd){
  std::map::iterator iter = playersEntities.begin();
  for(; iter != playersEntities.end(); ++iter){
    if(mouseBoxStart.x> mouseBoxEnd.x){
      mouseBoxStart.x +=mouseBoxEnd.x;
      mouseBoxEnd.x = mouseBoxStart.x - mouseBoxEnd.x;
      mouseBoxStart.x -= mouseBoxEnd.x;
    }
    if(mouseBoxStart.y> mouseBoxEnd.y){
      mouseBoxStart.y +=mouseBoxEnd.y;
      mouseBoxEnd.y = mouseBoxStart.y - mouseBoxEnd.y;
      mouseBoxStart.y -= mouseBoxEnd.y;
    }
    bool collisionResult = false;
    if(mouseBoxStart.x < = iter->second->getPosition().x && iter->second->getPosition().x < = mouseBoxEnd.x){
      if(mouseBoxStart.y <= iter->second->getPosition().y && iter->second->getPosition().y < = mouseBoxEnd.y){
        collisionResult = true;
      }
    }
    //if point is inside square then add to selected
    if(collisionResult == true){
      playersSelectedEntities[iter->second->getID()] = iter->second;
    }
  }
}

idea of this method is to search player entities list.
let me explain

when program is adding new objects to the scene it is adding new entities. Entities can have tag/name simple if check at this stage can save a bit of CPU later on, when instead of searching through all entities on the map we are searching only list of players entities that are pointing to exact entity.

1
2
3
4
5
6
7
void PlayerManager::markSelected(ID3DXSprite* sprite){
  std::map::iterator iter = playersSelectedEntities.begin();
  for(; iter != playersSelectedEntities.end(); ++iter){
    D3DXVECTOR3 deltaPosition = iter->second->getPosition() - LevelManager::getOffset();
    sprite->Draw(texture, 0, 0, &deltaPosition, D3DCOLOR_XRGB(255,255,255));
  }
}

markSelected method is the one that needs little bit more brain storming on. At the moment I am keeping it in PlayerManager class.. which in my opinion is little bit weird.. but in my defense.. we want to mark “Players” selected entities.. don’t we?

Leave a Reply

Your email address will not be published. Required fields are marked *