Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/game/StarWorldClientState.cpp
blob: 4fbefc96c821dc5fcf1570d91bacf42b0f939b3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "StarWorldClientState.hpp"
#include "StarDataStreamExtra.hpp"
#include "StarRoot.hpp"
#include "StarAssets.hpp"

namespace Star {

WorldClientState::WorldClientState() {
  auto clientConfig = Root::singleton().assets()->json("/client.config");

  m_windowMonitoringBorder = clientConfig.getInt("windowMonitoringBorder");
  m_presenceEntityMonitoringBorder = clientConfig.getInt("presenceEntityMonitoringBorder");

  m_playerId.set(NullEntityId);

  m_netVersion = 0;

  m_netGroup.addNetElement(&m_windowXMin);
  m_netGroup.addNetElement(&m_windowYMin);
  m_netGroup.addNetElement(&m_windowWidth);
  m_netGroup.addNetElement(&m_windowHeight);

  m_netGroup.addNetElement(&m_playerId);
  m_netGroup.addNetElement(&m_clientPresenceEntities);
}

RectI WorldClientState::window() const {
  return RectI::withSize(Vec2I(m_windowXMin.get(), m_windowYMin.get()), Vec2I(m_windowWidth.get(), m_windowHeight.get()));
}

void WorldClientState::setWindow(RectI const& window) {
  m_windowXMin.set(window.xMin());
  m_windowYMin.set(window.yMin());
  m_windowWidth.set(window.width());
  m_windowHeight.set(window.height());
}

Vec2F WorldClientState::windowCenter() const {
  return RectF(window()).center();
}

EntityId WorldClientState::playerId() const {
  return m_playerId.get();
}

void WorldClientState::setPlayer(EntityId playerId) {
  return m_playerId.set(playerId);
}

List<EntityId> const& WorldClientState::clientPresenceEntities() const {
  return m_clientPresenceEntities.get();
}

void WorldClientState::setClientPresenceEntities(List<EntityId> entities) {
  m_clientPresenceEntities.set(std::move(entities));
}

List<RectI> WorldClientState::monitoringRegions(function<Maybe<RectI>(EntityId)> entityBounds) const {
  List<RectI> regions;
  
  auto windowRegion = window().padded(m_windowMonitoringBorder);

  if (window() != RectI())
    regions.append(windowRegion);

  if (auto playerBounds = entityBounds(m_playerId.get())) {
    // add an extra region the size of the window centered on the player's position to prevent nearby sectors
    // being unloaded due to camera panning or centering on other entities
    regions.append(RectI::withCenter(playerBounds->center(), windowRegion.size()));
  }

  for (auto entityId : m_clientPresenceEntities.get()) {
    if (auto bounds = entityBounds(entityId))
      regions.append(bounds->padded(m_presenceEntityMonitoringBorder));
  }

  return regions;
}

ByteArray WorldClientState::writeDelta() {
  ByteArray delta;
  tie(delta, m_netVersion) = m_netGroup.writeNetState(m_netVersion, m_netCompatibilityRules);
  return delta;
}

void WorldClientState::readDelta(ByteArray delta) {
  m_netGroup.readNetState(std::move(delta), 0.0f, m_netCompatibilityRules);
}

void WorldClientState::setNetCompatibilityRules(NetCompatibilityRules netCompatibilityRules) {
  m_netCompatibilityRules = netCompatibilityRules;
}

NetCompatibilityRules WorldClientState::netCompatibilityRules() const {
  return m_netCompatibilityRules;
}

void WorldClientState::reset() {
  m_netVersion = 0;
}

}