~robot3d-team/robot3d/trunk

« back to all changes in this revision

Viewing changes to src/srCore/simulationRobotSelectionComponent.cpp

  • Committer: Anne van Rossum
  • Date: 2010-08-10 15:58:55 UTC
  • Revision ID: anne@gamix-20100810155855-kve7x2vwouagdij9
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Component for robot selection
 
3
 *
 
4
 * @author Adrian Friebel
 
5
 */
 
6
 
 
7
// Robot3D files
 
8
#include <srCore/simulationRobotSelectionComponent.h>
 
9
#include <srCore/simulationArenaComponent.h>
 
10
#include <srCore/robot/robotActorBase.h>
 
11
#include <srCore/simulationEvents.h>
 
12
 
 
13
using namespace srCore;
 
14
 
 
15
////////////////////////////////////////////////////////////////////
 
16
SimulationRobotSelectionComponent::SimulationRobotSelectionComponent(const std::string &name, int robFoc)
 
17
 : dtGame::GMComponent(name)
 
18
{
 
19
        mFocusChanged = new dtDAL::GameEvent( ROBOT_SELECTION_CHANGED );
 
20
        dtDAL::GameEventManager::GetInstance().AddEvent(*mFocusChanged);
 
21
 
 
22
        robotFocus              = robFoc;
 
23
}
 
24
 
 
25
 
 
26
void SimulationRobotSelectionComponent::init()
 
27
{
 
28
        std::vector< dtGame::GameActorProxy* > allGameActors;
 
29
        GetGameManager()->GetAllGameActors(allGameActors);
 
30
 
 
31
        for(int i=0; i<(int)allGameActors.size(); i++)
 
32
        {
 
33
                RobotActorBase &actor = static_cast<RobotActorBase&>(allGameActors[i]->GetGameActor());
 
34
 
 
35
                if(robotFocus == actor.getID())
 
36
                {
 
37
                        robotFocusIndex = i;
 
38
                        actor.setSelectionState(true);
 
39
                        actor.renderCollisionGeometry(true);
 
40
                        break;
 
41
                }
 
42
        }
 
43
}
 
44
 
 
45
 
 
46
//////////////////////////////////////////////////////////////////////////
 
47
void SimulationRobotSelectionComponent::sendMessageToAll(dtDAL::GameEvent &event)
 
48
{
 
49
   dtCore::RefPtr<dtGame::GameEventMessage> eventMsg;
 
50
   GetGameManager()->GetMessageFactory().CreateMessage(dtGame::MessageType::INFO_GAME_EVENT, eventMsg);
 
51
 
 
52
   eventMsg->SetGameEvent(event);
 
53
   GetGameManager()->SendMessage(*eventMsg);
 
54
}
 
55
 
 
56
 
 
57
//////////////////////////////////////////////////////////////////////////
 
58
void SimulationRobotSelectionComponent::ProcessMessage(const dtGame::Message& message)
 
59
{
 
60
        if (message.GetMessageType() == dtGame::MessageType::INFO_GAME_EVENT)
 
61
        {
 
62
                const dtGame::GameEventMessage &eventMsg = static_cast<const dtGame::GameEventMessage&>(message);
 
63
//              printf("Robot Selection Component: %s\n", eventMsg.GetGameEvent()->GetName().c_str());
 
64
 
 
65
                if(eventMsg.GetGameEvent() != NULL)
 
66
                {
 
67
                   if(eventMsg.GetGameEvent()->GetName() == CHANGE_ROBOT_SELECTION )
 
68
 
 
69
                   {            
 
70
                           SimulationArenaComponent *comp = static_cast<SimulationArenaComponent*>(GetGameManager()->GetComponentByName("SimulationArenaComponent"));
 
71
                           
 
72
                           if(comp->getNumRobots()>0)
 
73
                           {
 
74
                                   std::vector< dtGame::GameActorProxy* > allGameActors;
 
75
                                   GetGameManager()->GetAllGameActors(allGameActors);
 
76
 
 
77
                                   RobotActorBase &actor = static_cast<RobotActorBase&>(allGameActors[robotFocusIndex]->GetGameActor());
 
78
                                   actor.setSelectionState(false);
 
79
                                   actor.renderCollisionGeometry(false);
 
80
 
 
81
                                   std::string number = eventMsg.GetGameEvent()->GetDescription();
 
82
                                   if(number.empty())
 
83
                                   {
 
84
                                           robotFocusIndex = (robotFocusIndex+1) % comp->getNumRobots();
 
85
                                           RobotActorBase &actor = static_cast<RobotActorBase&>(allGameActors[robotFocusIndex]->GetGameActor());
 
86
                                           robotFocus = actor.getID();
 
87
                                           actor.setSelectionState(true);
 
88
                                           actor.renderCollisionGeometry(true);
 
89
                                   }
 
90
                                   else 
 
91
                                   {
 
92
                                           robotFocus = atoi(number.c_str());
 
93
 
 
94
                                           for(int i=0; i<comp->getNumRobots(); i++)
 
95
                                           {
 
96
                                                   RobotActorBase &actor = static_cast<RobotActorBase&>(allGameActors[i]->GetGameActor());
 
97
 
 
98
                                                   if(robotFocus == actor.getID())
 
99
                                                   {
 
100
                                                           robotFocusIndex = i;
 
101
                                                           actor.setSelectionState(true);
 
102
                                                           actor.renderCollisionGeometry(true);
 
103
                                                           break;
 
104
                                                   }
 
105
                                           }
 
106
                                   }
 
107
                                   sendMessageToAll(*mFocusChanged);
 
108
                           }
 
109
                   }
 
110
                   if(eventMsg.GetGameEvent()->GetName() == DESTROY_ACTOR )
 
111
                   {
 
112
                           SimulationArenaComponent *comp = static_cast<SimulationArenaComponent*>(GetGameManager()->GetComponentByName("SimulationArenaComponent"));
 
113
 
 
114
                           if(comp->getNumRobots()>0)
 
115
                           {
 
116
                                   robotFocusIndex = (robotFocusIndex+1) % comp->getNumRobots();
 
117
                                   std::vector< dtGame::GameActorProxy* > allGameActors;
 
118
                                   GetGameManager()->GetAllGameActors(allGameActors);
 
119
                                   
 
120
                                   RobotActorBase &actor = static_cast<RobotActorBase&>(allGameActors[robotFocusIndex]->GetGameActor());
 
121
                                   robotFocus = actor.getID();
 
122
                                   actor.setSelectionState(true);
 
123
                                   actor.renderCollisionGeometry(true);                    
 
124
 
 
125
                           }
 
126
                           else
 
127
                                  robotFocusIndex = -1;
 
128
                                  
 
129
 
 
130
                           sendMessageToAll(*mFocusChanged);
 
131
                   }
 
132
                }
 
133
        }
 
134
}
 
135