示例#1
0
//==============================================================================
void GameServer::tick()
{
  /*float dt = (time_.elapsed() - lastTime_) * 0.001f;*/
  lastTime_ = time_.elapsed();

  /*if (actors_.size() < 100 && !testingStageActive_)
  {
    GenMonsters_();
  }*/

  auto collideWithGrid = [=](Actor* actor, EActorDirection direction)
  {
    auto& p = *actor;

    float x = p.GetPosition().x;
    float y = p.GetPosition().y;

    bool collided = false;

    if ((levelMap_.GetCell(x + 0.49f, y - 0.51f) != '.'
        || levelMap_.GetCell(x + 0.49f, y + 0.49f) != '.')
        && levelMap_.GetCell(x - slideThreshold_+ 0.5f, y) == '.'
        && (direction == EActorDirection::NORTH
        || direction == EActorDirection::SOUTH))
    {
      p.SetPosition(Vector2(x - slideThreshold_+ 0.0001f, p.GetPosition().y));
    }

    if (levelMap_.GetCell(x + slideThreshold_- 0.5f, y) == '.'
        &&((levelMap_.GetCell(x - 0.5f, y - 0.51f) != '.'
        || levelMap_.GetCell(x - 0.5f, y + 0.49f) != '.')
        && (direction == EActorDirection::NORTH
        || direction == EActorDirection::SOUTH)))
    {
      p.SetPosition(Vector2(x + slideThreshold_- 0.0001f, p.GetPosition().y));
    }

    if (levelMap_.GetCell(x, y - slideThreshold_ + 0.5f) == '.'
        && (levelMap_.GetCell(x - 0.51f, y + 0.49f) != '.'
        || levelMap_.GetCell(x + 0.49f, y + 0.49f) != '.')
        && (direction == EActorDirection::EAST
        || direction == EActorDirection::WEST))
    {
      p.SetPosition(Vector2(p.GetPosition().x, y - slideThreshold_+ 0.0001f));
    }

    if ((levelMap_.GetCell(x + 0.49f, y - 0.5f) != '.'
        || levelMap_.GetCell(x - 0.51f, y - 0.5f) != '.')
        && levelMap_.GetCell(x, y + slideThreshold_- 0.5f) == '.'
        && (direction == EActorDirection::EAST
        || direction == EActorDirection::WEST))
    {
      p.SetPosition(Vector2(p.GetPosition().x, y + slideThreshold_ - 0.001f));
    }

    if (levelMap_.GetCell(x + 0.5f, y) != '.')
    {
      p.SetPosition(Vector2(round(x + 0.5f) - 0.5f, p.GetPosition().y));
      collided = true;
    }

    if (levelMap_.GetCell(x - 0.51f, y) != '.')
    {
      p.SetPosition(Vector2(round(x - 0.5f) + 0.5f, p.GetPosition().y));
      collided = true;
    }

    if (levelMap_.GetCell(x, y + 0.5f) != '.')
    {
      p.SetPosition(Vector2(p.GetPosition().x, round(y + 0.5f) - 0.5f));
      collided = true;
    }

    if (levelMap_.GetCell(x, y - 0.51f) != '.')
    {
      p.SetPosition(Vector2(p.GetPosition().x, round(y - 0.5f) + 0.5f));
      collided = true;
    }

    if (collided)
    {
      actor->OnCollideWorld();
    }
  };

  for (Actor* actor: actors_)
  {
    if (actor->GetType() == EActorType::MONSTER)
    {
      Monster* monster = dynamic_cast<Monster*>(actor);
      Creature* target = monster->target;
      float distance2;
      Vector2 m_pos = actor->GetPosition();
      if (target && target != nullptr)
      {
        Vector2 t_pos = target->GetPosition();
        distance2 = Sqr(m_pos.x - t_pos.x)
                    + Sqr(m_pos.y - t_pos.y);
        if (distance2 < 25)
        {
          if (abs(m_pos.x - t_pos.x - 1.0f) < playerVelocity_
              && m_pos.x - t_pos.x - 1.0f != 0)
          {
            monster->SetPosition(Vector2(t_pos.x + 1.0f, m_pos.y));
            monster->SetDirection(EActorDirection::NONE);
          }

          if (abs(m_pos.y - t_pos.y + 1.0f) < playerVelocity_
              && m_pos.y - t_pos.y + 1.0f != 0)
          {
            monster->SetPosition(Vector2(m_pos.x, t_pos.y - 1.0f));
            monster->SetDirection(EActorDirection::NONE);
          }

          if (m_pos.x < t_pos.x - 1.0f)
          {
            monster->SetDirection(EActorDirection::EAST);
          }
          else if (m_pos.x > t_pos.x + 1.0f)
          {
            monster->SetDirection(EActorDirection::WEST);
          }
          else if (m_pos.y < t_pos.y - 1.0f)
          {
            monster->SetDirection(EActorDirection::SOUTH);
          }
          else if (m_pos.y > t_pos.y + 1.0f)
          {
            monster->SetDirection(EActorDirection::NORTH);
          }
        }
      }

      if (!target
          || target == nullptr
          || distance2 >= 5)
      {
        for (Actor* tar : actors_)
        {
          if (tar != monster)
          {
            bool b = false;
            if (tar->GetType() != EActorType::ITEM
                && tar->GetType () != EActorType::PROJECTILE)
            {
              Creature* m = dynamic_cast<Creature*>(tar);

              QStringList str = monster->Flags.filter("HATE");
              for (QString hate: str)
              {
                if (Hates[hate] == m->GetRace())
                {
                  b = true;
                  break;
                }
              }

              if (b)
              {
                Vector2 t_pos = tar->GetPosition();
                distance2 = Sqr(m_pos.x - t_pos.x) + Sqr(m_pos.y - t_pos.y);
                // Dasha told 5^2 is a protocol defined const
                if (distance2 < 25)
                {
                  monster->target = m;
                  break;
                }
              }
            }
          }
        }
      }
    }

    if (actor->GetType() == EActorType::MONSTER
        || actor->GetType() == EActorType::PLAYER)
    {
      Creature* monster = dynamic_cast<Creature*>(actor);
      if (monster->GetHealth() <= 0)
      {
        KillActor_(actor);
        break;
      }
    }
  }

  for (Actor* actor: actors_)
  {
    if (!actor
        || actor == nullptr)
    {
      break;
    }

    auto v = directionToVector[static_cast<unsigned>(actor->GetDirection())] ;
    actor->SetVelocity(v);
    float dt = playerVelocity_;
    Vector2 old_pos = actor->GetPosition();
    Vector2 new_pos = old_pos + v * (dt + 0.001);
    Vector2 old_pos2 = old_pos + v * 0.51;
    levelMap_.RemoveActor(actor);
    EActorDirection d = actor->GetDirection();
    float x = new_pos.x;
    float y = new_pos.y;
    if (levelMap_.GetCell(old_pos2.x, old_pos2.y) != '#'
        && d != EActorDirection::NONE
        && (((levelMap_.GetCell(x - slideThreshold_+ 0.5f, y) == '.'
              && levelMap_.GetCell(x + slideThreshold_- 0.5f, y) == '.')
             && (d == EActorDirection::NORTH
                 || d == EActorDirection::SOUTH))
            || ((levelMap_.GetCell(x, y - slideThreshold_+ 0.5f) == '.'
                 && levelMap_.GetCell(x, y + slideThreshold_- 0.5f) == '.')
                && (d == EActorDirection::EAST
                    || d == EActorDirection::WEST))))
    {
      if (levelMap_.GetCell(new_pos.x, new_pos.y) == '.')
      {
        if (!actor->Update(dt)
            && actor->GetType () == EActorType::PROJECTILE)
        {
          static_cast<Projectile*>(actor)->death = true;
        }
        collideWithGrid(actor, d);
      }
      else if (levelMap_.GetCell(x , y) != '.'
               && playerVelocity_ >= 1)
      {
        bool b = false;
        for (float i = 0.01; i <= dt; i += 0.01)
        {
          new_pos = old_pos + v * i + v * 0.5f;
          if (levelMap_.GetCell(new_pos.x, new_pos.y) == '#' && !b)
          {
            actor->Update(i - 0.01);
            b = true;
          }
        }
      }
      else
      {
        if (actor->GetType () == EActorType::PROJECTILE)
        {
          static_cast<Projectile*>(actor)->death = true;
        }
      }
    }
    else
    {
      if (actor
          && actor != nullptr)
      {
        if (actor->GetType () == EActorType::PROJECTILE)
        {
          static_cast<Projectile*>(actor)->death = true;
        }
        else
        {
          actor->OnCollideWorld();
        }
      }
    }

    if (actor->GetType() == EActorType::PLAYER)
    {
      Player* player = dynamic_cast<Player*>(actor);
      if (player->GetHealth() < player->GetMaxHealth())
      {
        player->SetHealth(player->GetHealth() + 1);
      }
    }

    if (actor->GetType() == EActorType::MONSTER)
    {
      Monster* monster = dynamic_cast<Monster*>(actor);
      Creature* target = monster->target;
      if (target && target->GetHealth() > 0)
      {
        Vector2 m_pos = actor->GetPosition();
        Vector2 t_pos = target->GetPosition();
        float distance2 = Sqr(m_pos.x - t_pos.x)
                          + Sqr(m_pos.y - t_pos.y);

        if (distance2 <= Sqr(pickUpRadius_))
        {
          events_ << monster->atack(target);
          events_ << target->atack(monster);
        }
      }
    }

    for (Actor* neighbour : actors_)
    {
      if (actor == nullptr
          || neighbour == nullptr
          || actor == neighbour
          || neighbour->GetType() == EActorType::ITEM
          || (actor->GetType() == EActorType::PROJECTILE
              && neighbour->GetType() == EActorType::PROJECTILE))
      {
        break;
      }

      Box box0(neighbour->GetPosition(), 0.9f, 0.9f);
      Box box1(actor->GetPosition(), 0.9f, 0.9f);

      if (box0.Intersect(box1))
      {
        actor->OnCollideActor(neighbour);
        neighbour->OnCollideActor(actor);
        if (actor->GetType() == EActorType::PROJECTILE)
        {
          static_cast<Projectile*>(actor)->death = true;
          if (neighbour->GetType () == EActorType::MONSTER)
          {
            Monster* monster = dynamic_cast<Monster*>(neighbour);
            if (monster->GetHealth () <= 0)
            {
              GetItems(monster);
            }
          }
        }
        else if (neighbour->GetType() == EActorType::PROJECTILE)
        {
          static_cast<Projectile*>(neighbour)->death = true;

          if (actor->GetType() == EActorType::MONSTER)
          {
            Monster* monster = dynamic_cast<Monster*>(actor);
            if (monster->GetHealth () <= 0)
            {
              GetItems(monster);
            }
          }
        }
        else if (neighbour->GetType() != EActorType::ITEM)
        {
          actor->SetPosition(old_pos);
        }
      }
    }
    if (actor->GetType() == EActorType::PROJECTILE
        && static_cast<Projectile*>(actor)->death)
    {
      QVariantMap ans1;
      ans1["radius"] = 1.0f;
      ans1["x"] = actor->GetPosition ().x;
      ans1["y"] = actor->GetPosition ().y;
      QVariantMap ans;
      ans["explode"] = ans1;
      events_  << ans;
      idToActor_.erase(actor->GetId());
      actors_.erase(std::remove(actors_.begin(), actors_.end(), actor), actors_.end());
      delete actor;
      actor = nullptr;
    }
    else
    {
      levelMap_.IndexActor(actor);
    }
  }

  QVariantMap tickMessage;
  tickMessage["tick"] = tick_;
  tickMessage["events"] = events_;
  events_.clear();
  emit broadcastMessage(QString(QJsonDocument::fromVariant(tickMessage).toJson()));
  tick_++;
}
void GameState_HeightMap::Initialize( )
{
	if (sky == nullptr)
	{
		sky = new SkyBox;
		sky->Initialize("cloud_1.png", 2048, 512, 128);
		//sky->Initialize("cloud_1.png", 4096);
	}
	if (heightMap == nullptr)
	{
		heightMap = new HeightMap;
		heightMap->Initialize("field_.raw", "field_1.png");
		//heightMap->Initialize("field_.raw", "field_1.png", 8, HEIGHT_FLAG::HM_NORMAL);
	}
	if (rock == nullptr)
	{
		rock = new Rocks_save( );
		rock->Initialize( );
	}
	if (trees == nullptr)
	{
		trees = new PlantTree( );
		trees->SetHeightMap(heightMap);
		trees->Initialize( );
	}

	if (goat == nullptr)
	{
		goat = new Player( );
		goat->Initialize("Goat/", "Goat.X");
	
		goat->SetPosition(D3DXVECTOR3(193.0f, 12.0f, -306.0f));
		goat->SetTrees(trees->GetTrees( ));
	
		goat->SetHeightMap(heightMap);
		goat->ChangeCharacterState(CharacterState::CHARACTER_IDLE);
		GameManager::GetCamera( )->SetLookTarget(goat->GetPositionAddress( ));
	}
	if (statusBar == nullptr)
	{
		statusBar = new ChracterStatusBar;
		statusBar->Initialize( );
	}
	if (quest == nullptr)
	{
		quest = new Quest;
		quest->Initialize();
	}

	Monster* monster = nullptr;
	monster = new Minion( );
	monster->Initialize("Goat/", "Goat_white.x");
	monster->SetPosition(D3DXVECTOR3(401, 22, -292));
	monster->SetHeightMap(heightMap);
	monster->ChangeCharacterState(CharacterState::CHARACTER_IDLE);
	monsters.push_back(monster);


	monster = nullptr;
	monster = new Minion( );
	monster->Initialize("Goat/", "Goat_white.x");
	monster->SetPosition(D3DXVECTOR3(401, 22, -299));
	monster->SetHeightMap(heightMap);
	monster->ChangeCharacterState(CharacterState::CHARACTER_IDLE);
	monsters.push_back(monster);

	monster = nullptr;
	monster = new Minion( );
	monster->Initialize("Goat/", "Goat_white.x");
	monster->SetPosition(D3DXVECTOR3(408, 22, -292));
	monster->SetHeightMap(heightMap);
	monster->ChangeCharacterState(CharacterState::CHARACTER_IDLE);
	monsters.push_back(monster);

	monster = nullptr;
	monster = new Minion( );
	monster->Initialize("Goat/", "Goat_white.x");
	monster->SetPosition(D3DXVECTOR3(399, 22, -290));
	monster->SetHeightMap(heightMap);
	monster->ChangeCharacterState(CharacterState::CHARACTER_IDLE);
	monsters.push_back(monster);

}