Ejemplo n.º 1
0
/*** This should be called when Objects colided. If 1/2 of the object height is > the other objects minY
it will hurt the other object with damage dmg.***/
void EnemyObject::simpleWalkerHurt(GameObject* pPlayer, const uint32_t otherType) {
    auto player = pPlayer->getHitbox();
    auto rThis = getHitbox();
    if (rThis->getMidY() > player->getMinY()) {
      pPlayer->hurt(dmg, Vec2(getVelocityX(), fSimpleHurtForce));
    }
}
Ejemplo n.º 2
0
void Ball::collideWithPaddle(Paddle* paddle)
{
    auto paddleRect = paddle->getRect();
    paddleRect.origin.x += paddle->getPosition().x;
    paddleRect.origin.y += paddle->getPosition().y;
    
    float lowY  = paddleRect.getMinY();
    float midY  = paddleRect.getMidY();
    float highY = paddleRect.getMaxY();
    
    float leftX  = paddleRect.getMinX();
    float rightX = paddleRect.getMaxX();
    
    if (getPosition().x > leftX && getPosition().x < rightX) {
    
        bool hit = false;
        float angleOffset = 0.0f; 
        
        if (getPosition().y > midY && getPosition().y <= highY + radius()) 
        {
            setPosition( Point(getPosition().x, highY + radius()) );
            hit = true;
            angleOffset = KD_PI_F / 2;
        }
        else if (getPosition().y < midY && getPosition().y >= lowY - radius()) 
        {
            setPosition( Point(getPosition().x, lowY - radius()) );
            hit = true;
			angleOffset = -KD_PI_F / 2;
        }
        
        if (hit) 
        {
            float hitAngle = (paddle->getPosition() - getPosition()).getAngle() + angleOffset;
            
            float scalarVelocity = _velocity.getLength() * 1.05f;
            float velocityAngle = -_velocity.getAngle() + 0.5f * hitAngle;
            
            _velocity = Point::forAngle(velocityAngle) * scalarVelocity;
        }
    }    
} 
Ejemplo n.º 3
0
Vec2 GameCharacterShape::getCenterPos()
{
    auto tmpRect    =   this->getCollisionRect();
    auto tmpPos     =   Vec2(tmpRect.getMidX(), tmpRect.getMidY());
    return tmpPos;
}
// on "init" you need to initialize your instance
bool LoseScene::init()
{
    if ( !Layer::init() )
    {
        return false;
    }

	auto borders = this->getBoundingBox();
	auto title = Label::createWithSystemFont("Oh no...", "Arial", 30);

	title->setPosition(Point(borders.getMidX(), borders.getMidY()+150));

	this->addChild(title);

	menu_play = Label::createWithSystemFont("Try Again?", "Arial", 30);
	//menu_help = Label::createWithSystemFont("Help", "Arial", 30);
	menu_exit = Label::createWithSystemFont("Exit", "Arial", 30);

	menu_play->setPosition(Point(title->getPositionX(), title->getPositionY()-150));
	//menu_help->setPosition(Point(menu_play->getPositionX(), menu_play->getPositionY() - 50));
	menu_exit->setPosition(Point(menu_play->getPositionX(), menu_play->getPositionY() - 50));

	this->addChild(menu_play);
	//this->addChild(menu_help);
	this->addChild(menu_exit);

	//create "loseselector"
	loseselector = Sprite::create("ball.png");
	loseselector->setPosition(menu_play->getPositionX()-700, menu_play->getPositionY());
	this->addChild(loseselector);

	loseselect = 1;

	//keyboard listener
	auto eventListener = EventListenerKeyboard::create();
	eventListener->onKeyPressed = [=](EventKeyboard::KeyCode keyCode,
		Event* event){

		if (keys.find(keyCode) == keys.end()){
			keys[keyCode] = std::chrono::high_resolution_clock::now();
		}
		Vec2 pos = loseselector->getPosition();

		switch (keyCode){
		case EventKeyboard::KeyCode::KEY_UP_ARROW:{
			log("up");
			if (loseselect == 1)
				loseselect = 2;
			else loseselect--;
		}; break;
		case EventKeyboard::KeyCode::KEY_DOWN_ARROW:{
			log("down");
			if (loseselect == 2)
				loseselect = 1;
			else loseselect++;
		}; break;

		}
		
	};

	eventListener->onKeyReleased = [=](EventKeyboard::KeyCode keyCode, Event* event){
		keys.erase(keyCode);
	};

	this->_eventDispatcher->addEventListenerWithSceneGraphPriority(eventListener, loseselector);
	this->scheduleUpdate();
    return true;
}
bool VirtualHandle::init()
{
    auto visibleSize = Director::getInstance()->getVisibleSize();
    auto winSize = Director::getInstance()->getWinSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    
    _rocker_range = ui::Widget::create();
    _a = ui::Button::create("res/logo/A.png");
    _b = ui::Button::create("res/logo/B.png");
    _rocker = ui::Button::create("res/logo/X.png");
    this->_rocker_range_value = 0.78*_rocker->getContentSize().width;
    
//    CCLOG("%f", _rocker->getContentSize().width);
//    CCLOG("%f", this->_rocker_range_value/2);
    
    _rocker_range->setContentSize(Size(_rocker_range_value, _rocker_range_value));
    _rocker_range->setPosition(Vec2(_rocker_range_value/2 + _margin.x,
                                    _rocker_range_value/2));
    _rocker->setPosition(Vec2(_rocker_range_value/2,
                              _rocker_range_value/2));
    _rocker_range->addChild(_rocker);
    
    _a->setPosition(Vec2(visibleSize.width-1.5*_a->getContentSize().width-_margin.x/3+2,
                         _rocker_range_value/2-18));
    _b->setPosition(Vec2(visibleSize.width-_b->getContentSize().width/2-_margin.x/3-1,
                         _rocker_range_value/2+7));
    _rocker->setTouchEnabled(false);
    
    
    this->addChild(_rocker_range);
    this->addChild(_a);
    this->addChild(_b);
    
    // bind
    _a->addTouchEventListener(std::bind(&VirtualHandle::touchEvent, this, std::placeholders::_1, std::placeholders::_2));
    _b->addTouchEventListener(std::bind(&VirtualHandle::touchEvent, this, std::placeholders::_1, std::placeholders::_2));
    
    auto event_dispatcher = Director::getInstance()->getEventDispatcher();
    auto rocker_range_event = EventListenerTouchOneByOne::create();
    
    rocker_range_event->onTouchBegan = [this, visibleSize, origin](Touch *touch, Event *event){
        
        auto bound = _rocker_range->getBoundingBox();
        bound.origin = _rocker_range->convertToWorldSpace(Vec2(0, 0));
        
        auto orig = Vec2(bound.getMidX(), bound.getMidY());
        auto p = touch->getLocation();
        
//        CCLOG("Origin: (%04f, %04f)", orig.x, orig.y);
//        CCLOG("Touch at: (%04f, %04f)", p.x, p.y);
//        
//        CCLOG("%f, %f, %f", bound.getMaxX(), bound.getMidX(), bound.getMinX());
//        CCLOG("%f, %f, %f", bound.getMaxY(), bound.getMidY(), bound.getMinY());
        
        if (bound.containsPoint(p)) {
            _rockerTouchID = touch->getID();
            _rockerLastPoint = p;
            
            if (orig.getDistance(p) < 20) {
                // stay
//                CCLOG("stay");
                _rockerWay = 0;
                _rockerDirection = Vec2(0, 0);
            } else {
                // check direction
                _rockerDirection = Vec2(p.x-orig.x, p.y-orig.y);
                _rockerDirection.normalize();
                auto arc = 180*atan2(_rockerDirection.y, _rockerDirection.x)/M_PI;
//                CCLOG("TID = %d, arc = %f", _rockerTouchID, arc);
                _rockerWay = callBackWithDirection(arc);
            }
            
            updateRockerPosition(_rockerLastPoint);
            
            return true;
            
        } else {
            resetRockerPosition(orig);
            _rockerDirection = Vec2(0, 0);
        }
        
        return false;
    };
    
    rocker_range_event->onTouchMoved = [this](Touch *touch, Event *event){
        if (_rockerWay == -1) {
            _rockerDirection = Vec2(0, 0);
            return ;
        }
        auto bound = _rocker_range->getBoundingBox();
        bound.origin = _rocker_range->convertToWorldSpace(Vec2(0, 0));
        auto orig = Vec2(bound.getMidX(), bound.getMidY());
        auto p = touch->getLocation();
        
        _rockerTouchID = touch->getID();
        _rockerLastPoint = p;
        _rockerDirection = Vec2(p.x-orig.x, p.y-orig.y);
        _rockerDirection.normalize();
        auto arc = 180*atan2(_rockerDirection.y, _rockerDirection.x)/M_PI;
//        CCLOG("onMoved: %f", arc);
        _rockerWay = callBackWithDirection(arc);
        
        if (bound.containsPoint(p)) {
            _rocker->setPosition(_rocker_range->convertToNodeSpace(p));
        } else {
            float radius = _rocker_range_value/2;
            auto edge = Vec2(orig.x + radius*_rockerDirection.x,
                             orig.y + radius*_rockerDirection.y);
            _rocker->setPosition(_rocker_range->convertToNodeSpace(edge));
//            CCLOG("orig: [%f, %f]", orig.x, orig.y);
//            CCLOG("edge: [%f, %f]", edge.x, edge.y);
        }
        
    };
    
    rocker_range_event->onTouchEnded = [this](Touch *touch, Event *event){
        _rockerDirection = Vec2(0, 0);
        if (_rockerWay == -1) {
            return ;
        }
        auto bound = _rocker_range->getBoundingBox();
        bound.origin = _rocker_range->convertToWorldSpace(Vec2(0, 0));
        auto orig = Vec2(bound.getMidX(), bound.getMidY());
        
        cancelWay();
        _rockerTouchID = -1;
        _rockerLastPoint = orig;
        updateRockerPosition(orig);
    };
    
    rocker_range_event->onTouchCancelled = [this](Touch *touch, Event *event){
        _rockerDirection = Vec2(0, 0);
        if (_rockerWay == -1) {
            return ;
        }
        auto bound = _rocker_range->getBoundingBox();
        bound.origin = _rocker_range->convertToWorldSpace(Vec2(0, 0));
        auto orig = Vec2(bound.getMidX(), bound.getMidY());
        
        cancelWay();
        _rockerTouchID = -1;
        _rockerLastPoint = orig;
        updateRockerPosition(orig);
    };
    
    event_dispatcher->addEventListenerWithSceneGraphPriority(rocker_range_event, _rocker_range);
    
    return true;
}
Ejemplo n.º 6
0
bool RunnerSprite::isCollisionWithRight(cocos2d::Rect box){
    auto manBox = mRunner->boundingBox();
    Vec2 manPoint = Vec2(manBox.getMaxX(),manBox.getMidY());
    return box.containsPoint(manPoint);
}