void HeroController::update(EntityManager &em, Entity &entity, const InputState &input, int now) { float dx = 0; float dy = 0; if (input.isKeyDown(InputState::Up)) { dy -= 2; } if (input.isKeyDown(InputState::Down)) { dy += 2; } if (input.isKeyDown(InputState::Left)) { dx -= 2; } if (input.isKeyDown(InputState::Right)) { dx += 2; } Mobile *m = NULL; if (!entity.hasComponent(Mobile::TYPE)) { m = new Mobile(); entity.addComponent(m); } else { m = entity.getComponent<Mobile>(); } m->setSpeed(dx, dy); }
//Apply world geometry to the mobile: force it to stay inside the world bounds void World::applyBounds( Mobile & mob ) { //For now, only force mobile to stay within the rectangular area of the World. //In future, add some obstacles? const vec2 & pos = mob.getLocation(); if ( bounds.contains( pos ) ){ //Bot is within the world rectangle return; }else{ //Bot is outside the world rectangle //force it back vec2 pos1 = pos; vec2 speed1 = mob.getSpeed(); if ( pos.x < bounds.left() ){ pos1.x = bounds.left(); speed1.x = 0; }else if ( pos.x > bounds.right() ) { pos1.x = bounds.right(); speed1.x = 0; } if ( pos.y < bounds.top() ){ pos1.y = bounds.top(); speed1.y = 0; }else if ( pos.y > bounds.bottom() ) { pos1.y = bounds.bottom(); speed1.y = 0; } //update the speed and position mob.setLocation( pos1 ); mob.setSpeed( speed1 ); } }
void DetectorObjectView::check() { InternalMessage("Model","Model::DetectorObjectView::check entering") ; Detector* detector = getViewPoint()->getObserver() ; if (!detector) return ; bool in_range = detector ? detector->canDetect(getObject()) : false ; if (!detector->getComputer()) return ; Computer* computer = detector->getComputer()->getTrait<Computer>() ; if (! computer) return ; if (!in_range && m_detection_information) { // destroy object computer->getMemoryModel()->destroyObject(m_detection_information) ; m_detection_information = NULL ; } else if (in_range) { InternalMessage( "Model", "Model::DetectorObjectView::check in range updating detection data") ; bool mobile = false ; if (! m_detection_information) { Solid* solid = getObject()->getTrait<Solid>() ; // create object m_detection_information = computer->getMemoryModel()->createObject() ; m_detection_information->addTrait(new DetectionData(detector->getComputer())) ; m_detection_information->addTrait(new Positionned()) ; m_detection_information->addTrait(new Solid(solid->getMesh())) ; m_detection_information->getTrait<DetectionData>()->m_detected = getObject() ; } // update object position Position position = getRelativePosition(getObject(), computer->getObject()) ; m_detection_information->getTrait<Positionned>()->setPosition(position) ; // update speed if exists Mobile* mobileTrait = getObject()->getTrait<Mobile>() ; if (mobileTrait) { Mobile* data = m_detection_information->getTrait<Mobile>() ; if (! data) { data = new Mobile() ; m_detection_information->addTrait(data) ; } data->setSpeed(mobileTrait->getSpeed()) ; data->setAngularSpeed(mobileTrait->getAngularSpeed()) ; } // update identification Transponder* identified = getObject()->getTrait<Transponder>() ; Transponder* identifedData = m_detection_information->getTrait<Transponder>() ; if (identified) { if (! identifedData) { // gained identification m_detection_information->addTrait(new Transponder(*identified)) ; } else if (identifedData->getCode() != identified->getCode()) { // changed identification identifedData->setCode(identified) ; } } else if (identifedData) { // lost identification m_detection_information->destroyTrait(identifedData) ; } // update Targetting std::set<TargetingSystem*> systems = getObject()->getChildren<TargetingSystem>() ; TargetingSystem* system_data = m_detection_information->getTrait<TargetingSystem>() ; if (systems.size() == 1) { TargetingSystem* system = *(systems.begin()) ; if (! system_data) { // gained identification m_detection_information->addTrait(new TargetingSystem()) ; m_detection_information->getTrait<TargetingSystem>()->m_target = system->getTarget() ; m_detection_information->getTrait<TargetingSystem>()->notify() ; } else if (system_data->getTarget() != system->getTarget()) { // changed selection system_data->m_target = system->getTarget() ; system_data->notify() ; } } else if (system_data) { // lost targeting system m_detection_information->destroyTrait(system_data) ; } } InternalMessage("Model","Model::DetectorObjectView::check leaving") ; }