Esempio n. 1
0
void  GameMap::tagObstaclesWithinViewRange(BaseGameEntity* const entity, double range)
{
	tagNeighbors(entity, _collision_areas, range);
}
Vec2 SteeringForce::Calculate()
{
    
    m_vSteeringForce  = Vec2::ZERO;
    Vec2 force;
    
    std::vector<Vehicle*> neighbors;
    
    if (On(allignment)||On(separation)||On(cohesion)) {
        
        if (m_pVehicle->isCellSpaceOn()) {
            
            CellSpacePartition<Vehicle*>* cellSpace = GameData::Instance()->getCellSpace();
            neighbors = cellSpace->getNeighbors();
            
        }else{
        auto data = GameData::Instance()->getEntityVector();
        
        neighbors = tagNeighbors(m_pVehicle,data,SearchRad);
        }
    }
    
    if (On(wall_avoidance)) {
        auto data = GameData::Instance()->getWallls();
        force = WallAvoidance(data);
        
        if (!AccumulateForce(m_vSteeringForce, force)) {
            return  m_vSteeringForce;
        }
    }
    
    if (On(obstacle_avoidance)) {
        
        auto data = GameData::Instance()->getObstacle();
        force = ObstacleAvoidance(data);
        
        if (!AccumulateForce(m_vSteeringForce, force)) {
            return  m_vSteeringForce;
        }
    }
    
    
    
    if (On(separation)) {
        
        force = Separation(neighbors);
        if (!AccumulateForce(m_vSteeringForce, force)) {
            return  m_vSteeringForce;
        }
    }
    
    if (On(cohesion)) {
        
        force = Cohesion(neighbors);
        if (!AccumulateForce(m_vSteeringForce, force)) {
            return  m_vSteeringForce;
        }
    }
    
    if (On(allignment)) {
        
        force = Alignment(neighbors);
        if (!AccumulateForce(m_vSteeringForce, force)) {
            return  m_vSteeringForce;
        }
    }
    
    
    
    if (On(hide)) {
        CCASSERT(m_pVehicle->getHideTarget()!=nullptr, "不存在躲避目标");
        auto data = GameData::Instance()->getEntityVector();
        force = Hide(m_pVehicle->getHideTarget(),data);
        if (!AccumulateForce(m_vSteeringForce, force)) {
            return  m_vSteeringForce;
        }
    }
    
    if (On(follow_path)) {
        force = PathFollow();
        
        if (!AccumulateForce(m_vSteeringForce, force)) {
            return  m_vSteeringForce;
        }
    }
    if (On(seek)) {
        
        force = Seek(m_pVehicle->getTarget());
        
        if (!AccumulateForce(m_vSteeringForce, force)) {
            return  m_vSteeringForce;
        }
        
    }
    
    if (On(flee)) {
        force = Flee(m_pVehicle->getTarget());
        if (!AccumulateForce(m_vSteeringForce, force)) {
            return  m_vSteeringForce;
        }
    }
    
    if (On(arrive)) {
        force = Arrive(m_pVehicle->getTarget(), fast);
        if (!AccumulateForce(m_vSteeringForce, force)) {
            return  m_vSteeringForce;
        }
    }
    
    if (On(offset_pursuit)) {
        force = OffsetPursuit(m_pVehicle->getLeader(), m_pVehicle->getOffsetToLeader());
        if (!AccumulateForce(m_vSteeringForce, force)) {
            return  m_vSteeringForce;
        }
    }
    
    if (On(pursuit)) {
        force = Pursuit(m_pVehicle->getEvaderv());
        if (!AccumulateForce(m_vSteeringForce, force)) {
            return  m_vSteeringForce;
        }
    }
    if (On(evade)) {
        force = Evade(m_pVehicle->getPursuer());
        if (!AccumulateForce(m_vSteeringForce, force)) {
            return  m_vSteeringForce;
        }
    }
    
    if (On(wander)) {
        force = Wander();
        if (!AccumulateForce(m_vSteeringForce, force)) {
            return  m_vSteeringForce;
        }
    }
    
    if (On(interpose)) {
        force = Interpose(m_pVehicle->getInterposeA(), m_pVehicle->getInterposeB());
        if (!AccumulateForce(m_vSteeringForce, force)) {
            return  m_vSteeringForce;
        }
    }
    
    return m_vSteeringForce;
}