PlayerManager – prototype class

Just a quick note. I have managed to write Player Manager class which is kind a prototype.. the two issues that I am having are how to get an offset of a map.. if I am trying to set up offset to static DXDVECTOR3 variable inside Level class linker is complaining, passing as attribute well it is a option but then I need to modify other classes.

Second problem is click in a frame. actually I have just figured this out :).. wow sometimes one sec break and step back can do the trick.

Anyway deep beta presents PlayerManager prototype class – probably it will be totally modified but as far as now I have something like that:

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
// PlayerManager.h
 
#pragma once
#ifndef PLAYERMANAGER_H
#define PLAYERMANAGER_H
 
[...]
#include "Entity.h"
#include "Level.h"
 
class PlayerManager
{
private:
  //players entities
  static std::map<int , Entity*> playersEntities;
  static std::map</int><int , Entity*> playersSelectedEntities;	
public:
  static PlayerManager* instance;
  static void init();
  static void registerPlayersEntity(Entity* playersEntity); // called in players unity type constructor
  static void removePlayersEntity(Entity* playersEntity);
  static Entity* getPlayersEntity(int id);
  static void clearSelected();
  static void addEntityToSelected(POINT mousePosition);
 
  PlayerManager();
  ~PlayerManager();
};
#endif
</int>
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
// PlayerManager.cpp
#include "PlayerManager.h"
#include "Collision.h"
 
PlayerManager* PlayerManager::instance =0;
std::map<int , Entity*> PlayerManager::playersEntities = std::map</int><int , Entity*>();
std::map</int><int , Entity*> PlayerManager::playersSelectedEntities = std::map</int><int , Entity*>();
 
PlayerManager::PlayerManager(){}
 
void PlayerManager::init(){
  PlayerManager::instance = new PlayerManager();
}
 
PlayerManager::~PlayerManager(){
  //clear selected units list
  std::map</int><int ,Entity*>::iterator iter = playersSelectedEntities.begin();
  for(; iter != playersSelectedEntities.end(); ++iter){
    delete iter->second;
    iter->second = 0;
  }
  playersSelectedEntities.clear();
  playersEntities.clear();
}
 
//register entity in players list
void PlayerManager::registerPlayersEntity(Entity* entity){
  //register entity called only once and after registration in entity manager
  // we have entity ID and I am pretty sure it will not dublicate.
  playersEntities[entity->getID()] = entity;
}
 
 
void PlayerManager::removePlayersEntity(Entity* entity){
  //removes selecred entity from players entities lise
  std::map</int><int , Entity*>::iterator iter = playersEntities.find(entity->getID());
  if(iter != playersEntities.end())
    playersEntities.erase(iter);
}
 
Entity* PlayerManager::getPlayersEntity(int id){
  //returns pointer to entity defined by id
  std::map</int><int , Entity*>::iterator iter = playersEntities.find(id);
  if(iter != playersEntities.end())
    return iter->second;
  return 0;
}
 
void PlayerManager::addEntityToSelected(POINT mousePosition){
  D3DXVECTOR3 position(0.0f,0.0f,0.0f);
  float radius = 1.0f;
 
  D3DXVECTOR3 mPosition(mousePosition.x, mousePosition.y, 0.0f);
 
  Circle mouseCircle = {mPosition, radius };
  Circle unitCircle = {position, radius };
  mouseCircle.position.x = mousePosition.x; // need to figure out how to apply offset
  mouseCircle.position.y = mousePosition.y;
  mouseCircle.position.z = 0.0f;
  mouseCircle.radius = 2.0f;
 
  std::map</int><int , Entity*>::iterator iter = playersEntities.begin();
  for(; iter != playersEntities.end(); ++iter){
    unitCircle.position = iter->second->getPosition();
    unitCircle.radius = 20.0f; // need to set this global instead of calculated based on number images etc like TILEBASEWIDTH or something
    CollisionResults collisionResult = TestCollisionCircle(mouseCircle, unitCircle);
    if(collisionResult == OVERLAPPING){
      std::cout < < "AWESOME";
    }else {
      PlayerManager::clearSelected();
      std::cout << "NOT";
    }
  }
}
 
void PlayerManager::clearSelected(){
  std::map<int, Entity*>::iterator iter = playersSelectedEntities.begin();
  for(; iter != playersSelectedEntities.end(); ++iter){
    if(iter->second){
      delete iter->second;
      iter->second = 0;
    }
  }
  playersSelectedEntities.clear();	
}
</int>

Leave a Reply

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