void JoystickEntityHandler::OnNewEntityInternal(const Safir::Dob::EntityProxy &entityProxy) { Safir::Logging::SendSystemLog(Safir::Logging::Informational, L"New Joystick entity: " + entityProxy.GetEntityId().ToString()); const Consoden::TankGame::JoystickPtr joystick_ptr = boost::static_pointer_cast<Consoden::TankGame::Joystick>(entityProxy.GetEntity()); if (joystick_ptr->GameId() == m_gameId) { // Joystick for our game m_engine->NewJoystickCB(joystick_ptr->TankId().GetVal(), entityProxy.GetEntityId()); } }
void Ponger::Pong(const Safir::Dob::EntityProxy& entityProxy) { const Safir::Dob::Typesystem::InstanceId instance = entityProxy.GetInstanceId(); // std::wcout << "Pong " << instance << std::endl; PingPongTable::iterator findIt = m_pingPongTable.find(instance); //if it is not in the table it is the first time we've seen it, so add to the table if (findIt == m_pingPongTable.end()) { findIt = m_pingPongTable.insert(std::make_pair(instance,Safir::Dob::Typesystem::InstanceId::GenerateRandom())).first; } m_entity->Number() = boost::static_pointer_cast<DoseStressTest::Ping>(entityProxy.GetEntity())->Number(); m_entity->WhichPing() = instance; m_connection.SetAll(m_entity,findIt->second,m_handler); }
void Player::OnUpdatedEntity(const Safir::Dob::EntityProxy entityProxy) { Consoden::TankGame::GameStatePtr gameState=boost::dynamic_pointer_cast<Consoden::TankGame::GameState>(entityProxy.GetEntity()); if (gameState && m_logic && entityProxy.GetInstanceId()==m_currentGameId) { if (gameState->Winner().GetVal() == Consoden::TankGame::Winner::Unknown) { try { m_logic->MakeMove(gameState); } catch(...) { std::cout<<"Caught unhandled exception in TankLogic!"<<std::endl; } } } }
void Player::OnNewEntity(const Safir::Dob::EntityProxy entityProxy) { Consoden::TankGame::GameStatePtr gameState=boost::dynamic_pointer_cast<Consoden::TankGame::GameState>(entityProxy.GetEntity()); if (gameState) { for (int i=0; i<gameState->TanksArraySize(); ++i) { if (!gameState->Tanks()[i].IsNull() && gameState->Tanks()[i].GetPtr()->PlayerId().GetVal()==m_myPlayerId) { //we participate in this game m_currentGameId=entityProxy.GetInstanceId(); m_currentTankId=gameState->Tanks()[i].GetPtr()->TankId(); m_myJoystickId=Safir::Dob::Typesystem::InstanceId::GenerateRandom(); Consoden::TankGame::JoystickPtr joystick=Consoden::TankGame::Joystick::Create(); joystick->PlayerId()=m_myPlayerId; joystick->GameId()=m_currentGameId; joystick->TankId()=m_currentTankId; joystick->Counter()=0; m_connection.SetAll(joystick, m_myJoystickId, m_myHandlerId); m_logic.reset(new TankLogic(m_currentTankId, boost::bind(&Player::SetJoystick, this, _1, _2, _3, _4))); break; } } } }
void JoystickEntityHandler::MoveJoystick() { // Safir::Logging::SendSystemLog(Safir::Logging::Critical, // L"JoystickEntityHandler::MoveJoystick"); Safir::Dob::EntityProxy entityProxy = m_connection.Read(m_JoystickEntity); Consoden::TankGame::JoystickPtr joystick = boost::static_pointer_cast<Consoden::TankGame::Joystick>(entityProxy.GetEntity()); Consoden::TankGame::Direction::Enumeration newDirection; // Make a random movement float r = random(); r = r / RAND_MAX; // Since moving backwards will kill the tank, make that unprobable (1%) if (r < 0.01) { // Backwards (1%) newDirection = InvertDirection(m_LastDirection); MoveDirection(newDirection, joystick); m_LastDirection = newDirection; } else if (r < 0.41) { // Forward (40%) newDirection = m_LastDirection; MoveDirection(newDirection, joystick); m_LastDirection = newDirection; } else if (r < 0.61) { // Turn right (20%) newDirection = TurnRight(m_LastDirection); MoveDirection(newDirection, joystick); m_LastDirection = newDirection; } else if (r < 0.81) { // Turn left (20%) newDirection = TurnLeft(m_LastDirection); MoveDirection(newDirection, joystick); m_LastDirection = newDirection; } else { // Stand still (19%) MoveDirection(Consoden::TankGame::Direction::Neutral, joystick); } // Set a random fire direction r = random(); r = r / RAND_MAX; if (r < 0.25) { TowerDirection(Consoden::TankGame::Direction::Left, joystick); } else if (r < 0.50) { TowerDirection(Consoden::TankGame::Direction::Right, joystick); } else if (r < 0.75) { TowerDirection(Consoden::TankGame::Direction::Up, joystick); } else { TowerDirection(Consoden::TankGame::Direction::Down, joystick); } // Fire? r = random(); r = r / RAND_MAX; if (r < 0.2) { // Yes! 20% Fire(true, joystick); } else { // No! 80% Fire(false, joystick); } }