fog.cpp 1.98 KB
Newer Older
1
2
#include <game/game_client.hpp>

3
4
5
6
7
#include <gui/camera/fog.hpp>
#include <gui/camera/camera.hpp>

#include <world/world.hpp>
#include <world/vision.hpp>
louiz’'s avatar
louiz’ committed
8
#include <world/location.hpp>
9
#include <world/team.hpp>
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

#include <world/map.hpp>

Fog::Fog(const unsigned int width, const unsigned int height,
         const Camera* const camera, const World* const world):
  texture(),
  sprite(),
  needs_redraw(true),
  camera(camera),
  world(world)
{
  this->resize(width, height);
}

void Fog::resize(const unsigned int width, const unsigned int height)
{
  // TODO, have a fallback
louiz’'s avatar
louiz’ committed
27
  this->texture.create(width, height);
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  this->texture.setSmooth(true);
  this->invalidate();
}

void Fog::invalidate()
{
  this->needs_redraw = true;
}

const sf::Sprite& Fog::get_sprite()
{
  if (this->needs_redraw)
    this->redraw();
  return this->sprite;
}

void Fog::redraw()
{
  this->texture.clear({0, 0, 0, 150});

louiz’'s avatar
louiz’ committed
48
49
  // For each entity (with a vision and position), draw the vision range
  for (const auto& entity: this->world->entities)
50
    {
louiz’'s avatar
louiz’ committed
51
52
      Vision* vision = entity->get<Vision>();
      Location* location = entity->get<Location>();
53
54
55
      Team* team = entity->get<Team>();
      if (vision && location &&
          team && (team->get() == this->camera->get_game_client()->get_self_team()))
56
57
58
59
        {
          sf::CircleShape circle(vision->get_range(),
                                 60);
          circle.setFillColor({0, 0, 0, 0});
louiz’'s avatar
louiz’ committed
60
          auto center = this->camera->world_to_camera_position(location->position());
61
62
63
64
65
66
67
68
69
70
71
72
73
74
          center.x -= this->camera->x;
          center.y -= this->camera->y;
          circle.setPosition(center.x, center.y);
          circle.move(-static_cast<float>(vision->get_range()),
                      -static_cast<float>(vision->get_range()) * TILE_HW_RATIO);
          circle.setScale(1, 0.666);
          this->texture.draw(circle, sf::BlendMode::BlendMultiply);
        }
    }

  this->texture.display();
  this->sprite.setTexture(this->texture.getTexture());
  this->needs_redraw = false;
}