bool Map::isInFog(const cocos2d::Vec2& position) { auto bVisionRSq = buildingVisionRadius * buildingVisionRadius; for (auto& entity : m_level->getEntitiesByType(EGameEntityType::AllyBuilding)) { if (auto building = entity.lock()) { if (position.distanceSquared(building->getPosition()) < bVisionRSq) { return false; } } } for (auto& entity : m_level->getEntitiesByType(EGameEntityType::AllyShip)) { if (auto ship = std::static_pointer_cast<Ship>(entity.lock())) { if (position.distanceSquared(ship->getPosition()) < ship->getVisionRadiusSq()) { return false; } } } return true; }
void SteeringAgent::truncateVelocity(cocos2d::Vec2& velocity) const { if (velocity.length() > m_maxSpeed) { velocity.normalize(); velocity *= m_maxSpeed; } }
inline void MovingEntity::setHeading(const cocos2d::Vec2& heading) { // TODO: try and catch exception. if(heading.getLengthSq() < kMathEpsilon) return ; _heading = heading; _heading.normalize(); _side = _heading.getPerp(); }
static inline float vector2angel(cocos2d::Vec2 v) { if (v.length() == 0) { return 0; } float pir = std::acos(v.x/v.length()); if (v.y < 0) { pir = 2*3.1415926 - pir; } return pir*(180.f/3.1415926); }
void SteeringAgent::truncateSteeringForce(cocos2d::Vec2& force) const { if (force.length() > m_maxForce) { force.normalize(); force *= m_maxForce; } if (force.length() > m_velocity.length()) { force = getForward() * m_maxForce; } }
void BlockHouse::setTarget(cocos2d::Vec2 target) { this->target = target; float angle = target.getAngle(); angle = -CC_RADIANS_TO_DEGREES(angle); this->setRotation(angle); }
BlueParticles::BlueParticles(cocos2d::Vec2 position, std::string img, cocos2d::Vec2 powerDirection, float power) : m_power(power), m_area(cocos2d::Rect(0,0,0,0)), m_direction(powerDirection.getNormalized()) { setCameraMask(4); setPosition(position); init(); this->setTexture(img); }
cocos2d::Vec2 GameEntity::getForwardVector() const { cocos2d::Vec2 forwardVector; const cocos2d::Vec2 initialForward = cocos2d::Vec2(0.0f, 1.0f); float rotationRadians = CC_DEGREES_TO_RADIANS(getRotation()); const cocos2d::Node* parent = getParent(); while (parent) { rotationRadians += CC_DEGREES_TO_RADIANS(parent->getRotation()); parent = parent->getParent(); } forwardVector = initialForward.rotateByAngle(cocos2d::Vec2::ZERO, rotationRadians); forwardVector.x *= -1.0f; return forwardVector; }
StationSector* BaseStation::getSectorUnderMouse(const cocos2d::Vec3& position) { static const auto winSize = cocos2d::Director::getInstance()->getOpenGLView()->getVisibleSize(); static auto camera = GameHub::instance()->getCameraWrap(); static cocos2d::Vec3 src; static cocos2d::Vec3 dst; static cocos2d::Vec2 offset; static float dist; static float dot; static float det; static float angle; offset.set(getToCenterOffset(position)); dist = offset.distanceSquared(cocos2d::Vec2::ZERO); dot = cocos2d::Vec2::UNIT_X.x * offset.x + cocos2d::Vec2::UNIT_X.y * offset.y; // dot product det = cocos2d::Vec2::UNIT_X.x * offset.y - cocos2d::Vec2::UNIT_X.y * offset.x; // determinant angle = -atan2f(det, dot); angle = (angle > 0 ? angle : (2 * 3.14f + angle)) * 360 / (2 * 3.14f); for (unsigned int i = 0; i < m_maxLevels; ++i) { for (auto sector : m_builtAssets[i]) { if (!sector) { // if sectors gonna be destoyed at random indecies in the future // change it to continue break; } if (dist > sector->getMinR() && dist < sector->getMaxR() && angle > sector->getMinA() && angle < sector->getMaxA()) { return sector; } } } return nullptr; }
/** * @desc 指定された頂点座標群から、指定されたベクトル方向における最も離れた頂点を求める * @param 頂点座標群 * @param ベクトル方向 * @return 頂点座標 */ cocos2d::Vec2 CCollisionData::getFarthestApexInDirection(const cocos2d::Vec2 &position, const std::vector<cocos2d::Vec2> *pApexs, const cocos2d::Vec2 &direction) const { // 頂点座標群がなければ位置座標を返す if(pApexs == NULL) return position ; // 誤差を抑えるために方向ベクトルを正規化する cocos2d::Vec2 vec = direction.getNormalized() ; // 暫定戻り値座標と、その頂点までの長さを最初の頂点で登録する cocos2d::Vec2 result = (*pApexs)[0] ; float maxLength = (*pApexs)[0].dot(vec) ; // 登録されている全ての頂点と比べる for(cocos2d::Vec2 apex : (*pApexs)){ float length = apex.dot(vec) ; if(maxLength < length){ maxLength = length ; result = apex ; } } return result ; }
void UnlockPortals::SetLockCollision(cocos2d::Vec2 collBoxSize, cocos2d::Vec2 collBoxOffset) { m_lockCollision = cocos2d::PhysicsBody::createBox(cocos2d::Size(collBoxSize), cocos2d::PhysicsMaterial(1.0f, 0.0f, 0.0f), collBoxOffset); m_lockCollision->setContactTestBitmask(0x0); m_lockCollision->setCollisionBitmask(0x1 << curOffset); m_lockCollision->setDynamic(false); m_lockCollision->setGravityEnable(true); m_lockCollision->setRotationEnable(false); m_lockCollision->retain(); if (curOffset < 30) ++curOffset; else curOffset = 27; if (!IsUnlocked()) { setPhysicsBody(m_lockCollision); removeAllChildrenWithCleanup(true); if (!collBoxSize.isZero() && m_boundaryImg != "none") { removeAllChildrenWithCleanup(true); auto boundary = cocos2d::Sprite::create(); boundary->setAnchorPoint(cocos2d::Vec2(0, 0)); boundary->setTexture(m_boundaryImg); boundary->setTextureRect(cocos2d::Rect(collBoxOffset.x - collBoxSize.x / 2, collBoxOffset.y - collBoxSize.y / 3, collBoxSize.x, collBoxSize.y)); cocos2d::Texture2D::TexParams params = cocos2d::Texture2D::TexParams(); params.magFilter = GL_LINEAR; params.minFilter = GL_LINEAR; params.wrapS = GL_REPEAT; params.wrapT = GL_REPEAT; boundary->getTexture()->setTexParameters(params); boundary->setCameraMask(4); addChild(boundary,22); boundary->setPosition(cocos2d::Vec2(collBoxOffset.x - collBoxSize.x / 3.0, collBoxOffset.y - collBoxSize.y / 3.0) + cocos2d::Vec2(m_imgXOffset, m_imgYOffset)); } } }
bool GameScene::linkTemp(cocos2d::Vec2 v1, cocos2d::Vec2 v2) { if (v1.equals(v2)) { return false; } // 判断点击的两个点是不是图案一致 if (mMap[(int)v1.x][(int)v1.y] == mMap[(int)v2.x][(int)v2.y]) { // 直连 if (linkD(v1, v2)) { return true; } // 一个拐角,对角可直连 auto p = Vec2(v1.x, v2.y); if (mMap[(int)p.x][(int)p.y] == 0) { if (linkD(v1, p) && linkD(p, v2)) { return true; } } // 一个拐角,对角可直连 p = Vec2(v2.x, v1.y); if (mMap[(int)p.x][(int)p.y] == 0) { if (linkD(v1, p) && linkD(p, v2)) { return true; } } // X扩展,判断是否有可直连的点 expandX(v1, p1E); expandX(v2, p2E); for (auto pt1 : p1E) { for (auto pt2 : p2E) { if (pt1.x == pt2.x) { if (linkD(pt1, pt2)) { return true; } } } } // Y扩展,判断是否有可直连的点 expandY(v1, p1E); expandY(v2, p2E); for (auto pt1 : p1E) { for (auto pt2 : p2E) { if (pt1.y == pt2.y) { if (linkD(pt1, pt2)) { return true; } } } } return false; } return false; }
float getSpeed() const { return _velocity.getLength(); }
void BaseStation::onMouseMove(cocos2d::Event* evt) { static const auto winSize = cocos2d::Director::getInstance()->getOpenGLView()->getVisibleSize(); static auto camera = GameHub::instance()->getCameraWrap(); static cocos2d::Vec3 src; static cocos2d::Vec3 dst; static cocos2d::Ray ray; static cocos2d::Vec3 hitpoint; auto mouseEvent = dynamic_cast<cocos2d::EventMouse*>(evt); const auto& mouseLoc = mouseEvent->getLocationInView(); const auto mouseOffset = cocos2d::Vec2(mouseLoc.x, winSize.height - mouseLoc.y); src.set(mouseOffset.x, mouseOffset.y, 10000.0f); dst.set(mouseOffset.x, mouseOffset.y, 0.0f); camera->unproject(src, src); camera->unproject(dst, dst); ray.set(src, dst - src); if (intersects(ray, m_collider.getAABB(), hitpoint)) { static cocos2d::Vec2 offset; static float dist; static float dot; static float det; static float angle; offset.set(getToCenterOffset(hitpoint)); dist = offset.distanceSquared(cocos2d::Vec2::ZERO); dot = cocos2d::Vec2::UNIT_X.x * offset.x + cocos2d::Vec2::UNIT_X.y * offset.y; // dot product det = cocos2d::Vec2::UNIT_X.x * offset.y - cocos2d::Vec2::UNIT_X.y * offset.x; // determinant angle = -atan2f(det, dot); angle = (angle > 0 ? angle : (2 * 3.14f + angle)) * 360 / (2 * 3.14f); bool found = false; for (unsigned int i = 0; i < m_maxLevels; ++i) { for (auto sector : m_builtAssets[i]) { if (!sector) { // if sectors gonna be destoyed at random indecies in the future // change it to continue break; } // special check for hovering the center of the station //m_isCenterHovered = (i == 0 && dist < sector->getMinR()); if (dist > sector->getMinR() && dist < sector->getMaxR() && angle > sector->getMinA() && angle < sector->getMaxA()) { m_selections[i].first->setVisible(true); m_selections[i].first->setRotation(sector->getMinA()); found = true; if (m_hoveredSector && m_hoveredSector != sector) { // hide the previous one auto level = m_hoveredSector->getLevel(); m_selections[level].first->setVisible(false); m_hoveredSector = sector; } else { m_hoveredSector = sector; } break; } } if (found) { //CCLOG("level:%d index:%d", m_hoveredSector->getLevel(), m_hoveredSector->getIndex()); break; } else { m_selections[i].first->setVisible(false); m_hoveredSector = nullptr; } } } }
bool FireAOE::isPositionInArea(const cocos2d::Vec2 & relativePos) { return relativePos.getLength()<radius; }