void TextWidget::updateDrawableTextPosition(){ switch (getTextAlignment()){ case CentrallyAligned: m_drawableText.setOrigin(m_drawableText.getLocalBounds().width / 2.f, m_drawableText.getLocalBounds().height / 2.f); m_drawableText.setPosition(getRealPosition() + getSize() / 2.f); break; case LeftAligned: m_drawableText.setOrigin(0, m_drawableText.getLocalBounds().height / 2.f); m_drawableText.setPosition(getRealPosition().x + 8, getRealPosition().y + getSize().y / 2.f); break; case RightAligned: m_drawableText.setOrigin(m_drawableText.getLocalBounds().width, m_drawableText.getLocalBounds().height / 2.f); m_drawableText.setPosition(getRealPosition().x + getSize().x - 8, getRealPosition().y + getSize().y / 2.f); break; } }
void Button::update(){ updateDrawableTextPosition(); m_lastState = m_state; sf::FloatRect rect(getRealPosition().x, getRealPosition().y, getSize().x, getSize().y); if (!rect.contains(GUICore::events()->getMousePosition().x, GUICore::events()->getMousePosition().y)){ m_state = Normal; return; } if (rect.contains(GUICore::events()->getMousePosition().x, GUICore::events()->getMousePosition().y) && GUICore::events()->getMouseBoutonState(sf::Mouse::Left) == GUIEvents::StateReleased){ m_state = Hovered; return; } if (GUICore::events()->getMouseBoutonState(sf::Mouse::Left) == GUIEvents::StatePressed && m_state == Hovered){ m_state = Clicked; giveFocus(); } }
void InterfaceItem::update(unsigned millisElapsed) { if (!hidden) { // We build the model matrix by applying the position, rotation and size, in this order //GameApp *gameApp = GameApp::getInstance(); Vector2 windowSize = Naquadah::getWindowSize(); Vector2 renderSize = getRealSize(); if (renderSize.x >= 0) { Vector2 renderPos = getRealPosition(); float realPosX = renderPos.x + (renderSize.x / 2.0f); float realPosY = windowSize.y - renderPos.y - (renderSize.y / 2.0f); modelMatrix = Matrix4::Translation(Vector3(realPosX, realPosY, 0.0f)); modelMatrix = modelMatrix * Matrix4::Rotation(rotation, Vector3(0, 0, 1)); modelMatrix = modelMatrix * Matrix4::Scale(Vector3(renderSize.x / 2.0f, renderSize.y / 2.0f, 1.0)); } // For each child, we have to manually calculate its model matrix /*for (std::vector<InterfaceItem*>::iterator it = innerItems->begin(); it != innerItems->end(); ++it) { // We call the default update of the child item, because that can be different from the // default as well (*it)->update(millisElapsed); // We build the model matrix by applying the position, rotation and size, in this order Vector2 *childRenderSize = new Vector2((*it)->getSize()); Vector2 *childRenderPos = new Vector2((*it)->getPosition()); if ((*it)->getSize().x == SIZE_NO_RESIZE && (*it)->getTexture() != nullptr) { childRenderSize->x = (float) (*it)->getTexture()->getTextureWidth(); } if ((*it)->getSize().y == SIZE_NO_RESIZE && (*it)->getTexture() != nullptr) { childRenderSize->y = (float) (*it)->getTexture()->getTextureHeight(); } if ((*it)->getPosition().x == POSITION_CENTERED) { childRenderPos->x = (windowSize.x / 2.0f) - (childRenderSize->x / 2.0f); } if ((*it)->getPosition().y == POSITION_CENTERED) { childRenderPos->y = (windowSize.y / 2.0f) - (childRenderSize->y / 2.0f); } float childRealPosX = (childRenderPos->x + renderPos.x) + (childRenderSize->x / 2.0f); float childRealPosY = (windowSize.y - (childRenderPos->y + renderPos.y)) - (childRenderSize->y / 2.0f); Matrix4 childModelMatrix; // We calculate the child's model matrix again, making sure rotation works childModelMatrix = Matrix4::Translation(Vector3(realPosX, realPosY, 0.0f)); childModelMatrix = childModelMatrix * Matrix4::Rotation(rotation, Vector3(0, 0, 1)); childModelMatrix = childModelMatrix * Matrix4::Translation(Vector3(-realPosX, -realPosY, 0.0f)); childModelMatrix = childModelMatrix * Matrix4::Translation(Vector3(childRealPosX, childRealPosY, 0.0f)); childModelMatrix = childModelMatrix * Matrix4::Rotation((*it)->getRotation(), Vector3(0, 0, 1)); childModelMatrix = childModelMatrix * Matrix4::Scale(Vector3(childRenderSize->x / 2, childRenderSize->y / 2, 1.0f)); (*it)->setModelMatrix(childModelMatrix); delete childRenderSize; delete childRenderPos; }*/ } }
/** * \fn int showSolution(sInterface *p_interface, sList *p_solutionPath) * \brief Fonction qui permet l'affichage de la solution du labyrinthe * * \param *p_interface pointeur vers une structure de type sInterface * \param *p_solutionPath pointeur vers une structure de type sList, structure de listes chainées * \return int représentant le déroulement de la fonction */ int showSolution(sInterface *p_interface, sList *p_solutionPath) { SDL_Rect l_posA, l_posB, l_middleOffset; l_middleOffset.x = (WINDOW_WIDTH / CASE_COLUMN_AMOUNT) / 2; l_middleOffset.y = (WINDOW_HEIGHT / CASE_LINE_AMOUNT) / 2; if (p_solutionPath && p_solutionPath->next) { p_solutionPath = p_solutionPath->next; l_posA = getRealPosition(p_solutionPath->position); SDL_RenderDrawLine(p_interface->renderer, p_interface->player.realPosition.x + l_middleOffset.x, p_interface->player.realPosition.y + l_middleOffset.y, l_posA.x + l_middleOffset.x, l_posA.y + l_middleOffset.y); while (p_solutionPath->next) { l_posB = getRealPosition((p_solutionPath->next)->position); SDL_RenderDrawLine(p_interface->renderer, l_posA.x + l_middleOffset.x, l_posA.y + l_middleOffset.y, l_posB.x + l_middleOffset.x, l_posB.y + l_middleOffset.y); l_posA.x = l_posB.x; l_posA.y = l_posB.y; p_solutionPath = p_solutionPath->next; } } return 0; }
bool tick(){ for(unsigned int i=0; i<velocity; i++){ // TODO: Put here new enemies with RoundsManager auto it1 = enemies.begin(); while(it1 != enemies.end()){ bool lost = life<=0; Enemy* e = *it1; if(e->tick()){ life -= e->getDamage(); delete e; it1 = enemies.erase(it1); }else it1++; if(life<=0 && !lost){ money = 0; clearEntities(); for(Tower* t:towers){ entities.push_back(new ExplosionEntity(getRealPosition(t->getPosition()), pixelsPerSquare, 200)); } clearTowers(); } } auto it2 = entities.begin(); while(it2 != entities.end()){ Entity* e = *it2; if(e->tick()){ delete e; it2 = entities.erase(it2); }else it2++; } for(Tower* t : towers){ t->tick(); } if(life>0){ if(tickCount%400 == 1) enemies.push_back(new InhibitorEnemy(1.0, 30+tickCount/500, 1)); else if(tickCount%20 == 1) enemies.push_back(new BasicEnemy(1.0 + (double)(rand()%10)/10.0, 15+tickCount/500, 1)); } ++tickCount; } return false; }
/** * \fn bool WinOrNot(sInterface *p_interface, sMap *p_map) * \brief Fonction qui deduit si l'utilisateur a gagné ou non * * \param *p_interface pointeur vers une structure de type sInterface * \param *p_map pointeur vers une structure de type sMap * \return bool enum de type eBool vaut 1 si l'utilisateur a gagné 0 sinon */ bool WinOrNot(sInterface *p_interface, sMap *p_map) { if (comparePositionRect(getRealPosition(p_map->ending), p_interface->player.realPosition)) { Mix_PlayChannel(-1, p_interface->sonor.applause, 0); while (p_interface->effect.l_congrate->actualFrame != p_interface->effect.l_congrate->frameAmount - 1) { SDL_RenderCopy(p_interface->renderer, p_interface->backgroundSprite, NULL, NULL); updateAnimation(p_interface->effect.l_congrate, p_interface); SDL_RenderPresent(p_interface->renderer); SDL_Delay(100); } p_interface->effect.l_congrate->actualFrame = 0; SDL_Delay(1000); return TRUE; } return FALSE; }
bool Quad::isCoordinateInside(Vector coord, int minSize) { Vector realscale = getRealScale(); int hw = fabsf((width)*realscale.x)*0.5f; int hh = fabsf((height)*realscale.y)*0.5f; if (hw < minSize) hw = minSize; if (hh < minSize) hh = minSize; Vector pos = getRealPosition(); if (coord.x >= pos.x - hw && coord.x <= pos.x + hw) { if (coord.y >= pos.y - hh && coord.y <= pos.y + hh) { return true; } } return false; }
/** * \fn int updateGoal(sInterface *p_interface, sMap *p_map, eDirection p_direction) * \brief Fonction de gestion des deplacements du personnage * * \param *p_interface pointeur vers une structure de type sInterface * \param *p_map pointeur vers une structure de type sMap * \param p_direction enum de type eDirection definissant le sens du deplacement * \return int représentant le déroulement de la fonction */ int updateGoal(sInterface *p_interface, sMap *p_map, eDirection p_direction) { sNode *l_neighbour = NULL; if (p_interface->player.isSliding) return 0; p_interface->player.direction = p_direction; switch (p_direction) { case(DUP): l_neighbour = p_map->path[p_interface->player.mapPosition.y][p_interface->player.mapPosition.x].node.neighbourUP; break; case(DRIGHT): l_neighbour = p_map->path[p_interface->player.mapPosition.y][p_interface->player.mapPosition.x].node.neighbourRIGHT; break; case(DDOWN): l_neighbour = p_map->path[p_interface->player.mapPosition.y][p_interface->player.mapPosition.x].node.neighbourDOWN; break; case(DLEFT): l_neighbour = p_map->path[p_interface->player.mapPosition.y][p_interface->player.mapPosition.x].node.neighbourLEFT; break; } if (!l_neighbour) { p_interface->player.realDestination = p_interface->player.realPosition; }else{ Mix_PlayChannel(-1, p_interface->sonor.slide, 0); p_interface->compteur += 1; p_interface->player.realDestination = getRealPosition(l_neighbour->position); p_interface->player.mapPosition = l_neighbour->position; } p_interface->player.isSliding = TRUE; return 0; }
/** * \fn int gameLoop(sInterface *p_interface, sMap *p_map) * \brief Fonction de rendu de l'enigme et d'interfacage avec l'utilisateur * * \param *p_interface pointeur vers une structure de type sInterface * \param *p_map pointeur vers une structure de type sMap * \return int représentant le déroulement de la fonction */ int gameLoop(sInterface *p_interface, sMap *p_map) { bool l_loop = TRUE, l_solve = FALSE; char txtCmpt[32] = ""; SDL_Rect l_posText = {(WINDOW_WIDTH / CASE_COLUMN_AMOUNT)/2, (WINDOW_HEIGHT / CASE_LINE_AMOUNT)/2, 50, 50}; SDL_Color l_color = {255, 0, 0}; p_interface->player.mapPosition.x = p_map->starting.x; p_interface->player.mapPosition.y = p_map->starting.y; p_interface->player.realPosition = getRealPosition(p_interface->player.mapPosition); p_interface->player.realDestination = getRealPosition(p_interface->player.mapPosition); displayMap(p_interface, p_map); while (l_loop){ if(SDL_PollEvent(&(p_interface->event))) { switch (p_interface->event.type) { case(SDL_KEYDOWN): switch (p_interface->event.key.keysym.sym) { case(SDLK_z): updateGoal(p_interface, p_map, DUP); break; case(SDLK_d): updateGoal(p_interface, p_map, DRIGHT); break; case(SDLK_s): updateGoal(p_interface, p_map, DDOWN); break; case(SDLK_q): updateGoal(p_interface, p_map, DLEFT); break; case(SDLK_x): l_solve = !l_solve; break; case(SDLK_ESCAPE): l_loop = FALSE; break; } break; case(SDL_MOUSEBUTTONDOWN): break; } while (SDL_PollEvent(&(p_interface->event))); } SDL_RenderClear(p_interface->renderer); SDL_RenderCopy(p_interface->renderer, p_interface->backgroundSprite, NULL, NULL); if(l_solve) showSolution(p_interface, p_interface->solution); renderParticle(&(p_interface->effect.particle), p_interface, p_map, TRUE); updateVision(p_interface, p_map); sprintf_s(txtCmpt, 30, "%d", p_interface->compteur); displayText(p_interface->renderer, txtCmpt, l_color, l_posText); SDL_RenderPresent(p_interface->renderer); if (WinOrNot(p_interface, p_map)) { l_loop = FALSE; } SDL_Delay(SDL_ANIMATION_FRAMETIME); } return 0; }
void NodeCom::initWithMap(rapidjson::Value& value) { auto visibleSize=Director::getInstance()->getVisibleSize(); node=cocos2d::Node::create(); if (value.HasMember("contentSize")) { auto size=cocos2d::SizeFromString(value["contentSize"].GetString()); if (size.width<0) size.width=visibleSize.width; if (size.height<0) size.height=visibleSize.height; node->setContentSize(size); } else { node->setContentSize(visibleSize); } if (value.HasMember("anchorPoint")) { node->setAnchorPoint(cocos2d::PointFromString(value["anchorPoint"].GetString())); } cocos2d::Point position=value.HasMember("position")?cocos2d::PointFromString(value["position"].GetString()):cocos2d::Point::ZERO; if (value.HasMember("anchor")) { node->setPosition(getRealPosition(cocos2d::Director::getInstance()->getVisibleSize(), position, cocos2d::PointFromString(value["anchor"].GetString()))); } else if (value.HasMember("position")) { node->setPosition(cocos2d::PointFromString(value["position"].GetString())); } if (value.HasMember("Node")) { int count=value["Node"].Size(); for (int i=0; i<count; i++) { rapidjson::Value& nodeValue=value["Node"][i]; if (!nodeValue.IsNull()&&nodeValue.HasMember("type")) { const char* type=nodeValue["type"].GetString(); cocos2d::Node* child=nullptr; if (strcmp("Label", type)==0) { child=initLabel(nodeValue); } else if (strcmp("Sprite", type)==0) { child=initSprite(nodeValue); } else if (strcmp("Layer", type)==0) { child=initLayer(nodeValue); } else if (strcmp("Button", type)==0) { child=initButton(nodeValue); } else if (strcmp("TableView", type)==0){ child=initTable(nodeValue); } else if (strcmp("ProgressTimer", type)==0){ child=initTimer(nodeValue); } if (child!=nullptr) { node->addChild(child); if (nodeValue.HasMember("name")) { const char* name= nodeValue["name"].GetString(); __nodeNames.insert(std::make_pair(name, child)); } if (nodeValue.HasMember("scale")) { cocos2d::Point point=cocos2d::PointFromString(nodeValue["scale"].GetString()); child->setScale(point.x, point.y); } if (nodeValue.HasMember("position")) { child->setPosition(cocos2d::PointFromString(nodeValue["position"].GetString())); } if (nodeValue.HasMember("anchorPoint")) { child->setAnchorPoint(cocos2d::PointFromString(nodeValue["anchorPoint"].GetString())); } if (nodeValue.HasMember("localZOrder")) { child->setLocalZOrder(nodeValue["localZOrder"].GetInt()); } if (nodeValue.HasMember("globalOrder")) { child->setGlobalZOrder(nodeValue["globalOrder"].GetInt()); } if (nodeValue.HasMember("visible")) { child->setVisible(nodeValue["visible"].GetBool()); } if (nodeValue.HasMember("tag")) { child->setTag(nodeValue["tag"].GetInt()); } //如果声明了anchor,则按照新的规则布局 cocos2d::Point position1=nodeValue.HasMember("position")? cocos2d::PointFromString(nodeValue["position"].GetString()):cocos2d::Point::ZERO; if (nodeValue.HasMember("anchor")) { child->setPosition(getRealPosition(node->getContentSize(), position1, cocos2d::PointFromString(nodeValue["anchor"].GetString()))); } else if (nodeValue.HasMember("position")) { child->setPosition(position1); } } } } } }
bool RenderObject::isCoordinateInRadius(const Vector &pos, float r) { Vector d = pos-getRealPosition(); return (d.getSquaredLength2D() < r*r); }
void Button::draw(sf::RenderTarget& target, sf::RenderStates states) const{ sf::Sprite sprite(*m_texture, sf::IntRect(1 + (m_state - 1) * 16, 0, 8, 8)); sprite.setPosition(getRealPosition()); target.draw(sprite, states); sprite.setTextureRect(sf::IntRect(9 + (m_state - 1) * 16, 0, 8, 8)); sprite.setPosition(getRealPosition().x + getSize().x - 8, getRealPosition().y); target.draw(sprite, states); sprite.setTextureRect(sf::IntRect(1 + (m_state - 1) * 16, 16, 8, 8)); sprite.setPosition(getRealPosition().x, getRealPosition().y + getSize().y - 8); target.draw(sprite, states); sprite.setTextureRect(sf::IntRect(9 + (m_state - 1) * 16, 16, 8, 8)); sprite.setPosition(getRealPosition().x + getSize().x - 8, getRealPosition().y + getSize().y - 8); target.draw(sprite, states); sprite.setTextureRect(sf::IntRect(1 + (m_state - 1) * 16, 24, 8, 8)); sprite.setScale((getSize().x - 16.f) / 8.f, 1); sprite.setPosition(getRealPosition().x + 8, getRealPosition().y); target.draw(sprite, states); sprite.setTextureRect(sf::IntRect(9 + (m_state - 1) * 16, 24, 8, 8)); sprite.setScale((getSize().x - 16.f) / 8.f, 1); sprite.setPosition(getRealPosition().x + 8, getRealPosition().y + getSize().y - 8); target.draw(sprite, states); sprite.setTextureRect(sf::IntRect(1 + (m_state - 1) * 16, 8, 8, 8)); sprite.setScale(1, (getSize().y - 16.f) / 8.f); sprite.setPosition(getRealPosition().x, getRealPosition().y + 8); target.draw(sprite, states); sprite.setTextureRect(sf::IntRect(9 + (m_state - 1) * 16, 8, 8, 8)); sprite.setScale(1, (getSize().y - 16.f) / 8.f); sprite.setPosition(getRealPosition().x + getSize().x - 8, getRealPosition().y + 8); target.draw(sprite, states); sprite.setTextureRect(sf::IntRect(0, m_state - 1, 1, 1)); sprite.setScale(getSize().x - 16.f, getSize().y - 16.f); sprite.setPosition(getRealPosition() + sf::Vector2f(8, 8)); target.draw(sprite, states); target.draw(m_drawableText, states); }