void EnemyObject::walkInZone(const float delta){ auto const rect = getHitbox(); const float minX = rect->getMidX(); const float maxX = rect->getMaxX(); if(maxX >softXMax){ setVelocityX(-speed); setPrevDir(GO_LEFT); objectSprite->setScaleX(-1 * abs(objectSprite->getScale())); }else if(minX < softXMin){ setVelocityX(speed); setPrevDir(GO_RIGHT); objectSprite->setScaleX(1 * abs(objectSprite->getScale())); } }
/*** Add this function to the objects update function to make it turn at edges. This Algorithm was not designed For slopes. If the objects path includes slopes use a predefined zone instead***/ void EnemyObject::turnAtEdge(const float delta) { const float lookAhead = 0.5f*getVelocityX() / delta; auto const hitbox = getHitbox(); if (!isBlocked((uint32_t)((hitbox->getMidX() + lookAhead) / 16.0), (uint32_t)(hitbox->getMinY() - 1) / 16.0)) { if (getPrevDir() == GO_LEFT) { setVelocityX(speed); setPrevDir(GO_RIGHT); objectSprite->setScaleX(1); } else { setVelocityX(-speed); setPrevDir(GO_LEFT); objectSprite->setScaleX(-1); } } }
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; }
bool RunnerSprite::isCollisionWithBottom(cocos2d::Rect box){ auto manBox = mRunner->boundingBox(); Vec2 manPoint = Vec2(manBox.getMidX(),manBox.getMinY()); return box.containsPoint(manPoint); }