Exemple #1
0
void Monster::realTimeAction(Map * m, Player * p) // p est le joueur en train de jouer
{
    Cell * pCell = m->getCell(p->getPosition().x, p->getPosition().y);
    Cell * myCell = m->getCell(c_Position.x, c_Position.y);

    if(c_AggroState == AGGRESIVE && m->getCellDist(pCell, myCell) <= c_AggroDist)
    {
        c_IsInAggro = true;
        c_LastAggroTime = sf::Time::Zero;
    }
    else if(c_LastAggroTime.asSeconds() >= c_LostAggroTime)
        c_IsInAggro = false; // perte d'aggro au fil du temps si trop éloigné

    if(isMoveable()) // gestion du déplacmeent
    {
        if(c_IsInAggro && m->getCellDist(pCell, myCell) > 1)
        {// si en en aggro et en mode aggresif
            std::vector< Cell * > path = m->getPath(myCell, pCell, false, false, 1);

            if(path.size() != 0 && path[0]->isWalkable())
            {
                if(path[0]->getC() - c_Position.x == 1)
                    setDirection(RIGHT);
                else if(path[0]->getC() - c_Position.x == -1)
                    setDirection(LEFT);
                else if(path[0]->getL() - c_Position.y == 1)
                    setDirection(DOWN);
                else if(path[0]->getL() - c_Position.y == -1)
                    setDirection(UP);

                m->moveLiving(this, path[0]->getC(), path[0]->getL());
            }
            else
            {
                std::vector< Cell * > path = m->getPath(myCell, pCell, true, false, 0);

                if(path[0]->getC() - c_Position.x == 1)
                    setDirection(RIGHT);
                else if(path[0]->getC() - c_Position.x == -1)
                    setDirection(LEFT);
                else if(path[0]->getL() - c_Position.y == 1)
                    setDirection(DOWN);
                else if(path[0]->getL() - c_Position.y == -1)
                    setDirection(UP);

                if(path.size() != 0 && path[0]->isWalkable())
                {
                    m->moveLiving(this, path[0]->getC(), path[0]->getL());
                }
            }
        }
        else if(!c_IsInAggro)
        {
            uint16_t moveProb = rand() % 101;
            if(moveProb < 1) // 1% de chance de se déplacer
            {
                Direction dir = (Direction)(rand() % 4); // direction vers laquelle il veut se diriger
                Cell * cell = NULL;// cell vers laquelle il veut se diriger
                switch(dir)
                {
                case UP:
                    if(c_Position.y > 0)
                    {
                        cell = m->getUCell(myCell);
                    }
                    break;
                case DOWN:
                    if(c_Position.y < m->getNbrLine()-1)
                    {
                        cell = m->getDCell(myCell);
                    }
                    break;
                case LEFT:
                    if(c_Position.x > 0)
                    {
                        cell = m->getLCell(myCell);
                    }
                    break;
                case RIGHT:
                    if(c_Position.x < m->getNbrColumn()-1)
                    {
                        cell = m->getRCell(myCell);
                    }
                    break;
                }

                setDirection(dir);
                if(cell != NULL && cell->isWalkable() && !cell->gotStairs())
                    m->moveLiving(this, cell->getC(), cell->getL());
            }
        }
    }
    attack(m, p);

    Living::realTimeAction(m, p);
}
Exemple #2
0
bool PopupWindow::onProcessMessage(Message* msg)
{
    switch (msg->type()) {

    case kCloseMessage:
        stopFilteringMessages();
        break;

    case kMouseLeaveMessage:
        if (m_hotRegion.isEmpty() && !isMoveable())
            closeWindow(NULL);
        break;

    case kKeyDownMessage:
        if (m_filtering) {
            KeyMessage* keymsg = static_cast<KeyMessage*>(msg);
            KeyScancode scancode = keymsg->scancode();

            if (scancode == kKeyEsc ||
                    scancode == kKeyEnter ||
                    scancode == kKeyEnterPad) {
                closeWindow(NULL);
            }

            // If we are filtering messages we don't propagate key-events
            // to other widgets. As we're a popup window and we're
            // filtering messages, the user shouldn't be able to start
            // other actions pressing keyboard shortcuts.
            return false;
        }
        break;

    case kMouseDownMessage:
        if (m_filtering) {
            gfx::Point mousePos = static_cast<MouseMessage*>(msg)->position();

            switch (m_clickBehavior) {

            // If the user click outside the window, we have to close
            // the tooltip window.
            case kCloseOnClickInOtherWindow: {
                Widget* picked = pick(mousePos);
                if (!picked || picked->getRoot() != this) {
                    closeWindow(NULL);
                }
                break;
            }

            case kCloseOnClickOutsideHotRegion:
                if (!m_hotRegion.contains(mousePos)) {
                    closeWindow(NULL);
                }
                break;
            }
        }
        break;

    case kMouseMoveMessage:
        if (!isMoveable() &&
                !m_hotRegion.isEmpty() &&
                getManager()->getCapture() == NULL) {
            gfx::Point mousePos = static_cast<MouseMessage*>(msg)->position();

            // If the mouse is outside the hot-region we have to close the
            // window.
            if (!m_hotRegion.contains(mousePos))
                closeWindow(NULL);
        }
        break;

    }

    return Window::onProcessMessage(msg);
}
bool PopupWindow::onProcessMessage(Message* msg)
{
  switch (msg->type()) {

    // There are cases where startFilteringMessages() is called when a
    // kCloseMessage for this same PopupWindow is enqueued. Processing
    // the kOpenMessage we ensure that the popup will be filtering
    // messages if it's needed when it's visible (as kCloseMessage and
    // kOpenMessage must be enqueued in the correct order).
    case kOpenMessage:
      if (!isMoveable())
        startFilteringMessages();
      break;

    case kCloseMessage:
      stopFilteringMessages();
      break;

    case kMouseLeaveMessage:
      if (m_hotRegion.isEmpty() && !isMoveable())
        closeWindow(nullptr);
      break;

    case kKeyDownMessage:
      if (m_filtering) {
        KeyMessage* keymsg = static_cast<KeyMessage*>(msg);
        KeyScancode scancode = keymsg->scancode();

        if (scancode == kKeyEsc)
          closeWindow(nullptr);

        if (m_enterBehavior == kCloseOnEnter &&
            (scancode == kKeyEnter ||
             scancode == kKeyEnterPad)) {
          closeWindow(this);
          return true;
        }
      }
      break;

    case kMouseDownMessage:
      if (m_filtering) {
        gfx::Point mousePos = static_cast<MouseMessage*>(msg)->position();

        switch (m_clickBehavior) {

          // If the user click outside the window, we have to close
          // the tooltip window.
          case kCloseOnClickInOtherWindow: {
            Widget* picked = pick(mousePos);
            if (!picked || picked->getRoot() != this) {
              closeWindow(NULL);
            }
            break;
          }

          case kCloseOnClickOutsideHotRegion:
            if (!m_hotRegion.contains(mousePos)) {
              closeWindow(NULL);
            }
            break;
        }
      }
      break;

    case kMouseMoveMessage:
      if (!isMoveable() &&
          !m_hotRegion.isEmpty() &&
          getManager()->getCapture() == NULL) {
        gfx::Point mousePos = static_cast<MouseMessage*>(msg)->position();

        // If the mouse is outside the hot-region we have to close the
        // window.
        if (!m_hotRegion.contains(mousePos))
          closeWindow(NULL);
      }
      break;

  }

  return Window::onProcessMessage(msg);
}