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

summaryrefslogtreecommitdiff
path: root/source/core/StarNetElementSyncGroup.cpp
blob: cd9863818d124e92c9b1eab7367c087c7beede2f (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
#include "StarNetElementSyncGroup.hpp"

namespace Star {

void NetElementSyncGroup::enableNetInterpolation(float extrapolationHint) {
  NetElementGroup::enableNetInterpolation(extrapolationHint);
  if (m_hasRecentChanges)
    netElementsNeedLoad(false);
}

void NetElementSyncGroup::disableNetInterpolation() {
  NetElementGroup::disableNetInterpolation();
  if (m_hasRecentChanges)
    netElementsNeedLoad(false);
}

void NetElementSyncGroup::tickNetInterpolation(float dt) {
  NetElementGroup::tickNetInterpolation(dt);
  if (m_hasRecentChanges) {
    m_recentDeltaTime -= dt;
    if (netInterpolationEnabled())
      netElementsNeedLoad(false);

    if (m_recentDeltaTime < 0.0f && m_recentDeltaWasBlank)
      m_hasRecentChanges = false;
  }
}

void NetElementSyncGroup::netStore(DataStream& ds, NetCompatibilityRules rules) const {
  if (!checkWithRules(rules)) return;
  const_cast<NetElementSyncGroup*>(this)->netElementsNeedStore();
  return NetElementGroup::netStore(ds, rules);
}

void NetElementSyncGroup::netLoad(DataStream& ds, NetCompatibilityRules rules) {
  if (!checkWithRules(rules)) return;
  NetElementGroup::netLoad(ds, rules);
  netElementsNeedLoad(true);
}

bool NetElementSyncGroup::writeNetDelta(DataStream& ds, uint64_t fromVersion, NetCompatibilityRules rules) const {
  if (!checkWithRules(rules)) return false;
  const_cast<NetElementSyncGroup*>(this)->netElementsNeedStore();
  return NetElementGroup::writeNetDelta(ds, fromVersion, rules);
}

void NetElementSyncGroup::readNetDelta(DataStream& ds, float interpolationTime, NetCompatibilityRules rules) {
  if (!checkWithRules(rules)) return;
  NetElementGroup::readNetDelta(ds, interpolationTime, rules);

  m_hasRecentChanges = true;
  m_recentDeltaTime = interpolationTime;
  m_recentDeltaWasBlank = false;

  netElementsNeedLoad(false);
}

void NetElementSyncGroup::blankNetDelta(float interpolationTime) {
  NetElementGroup::blankNetDelta(interpolationTime);

  if (!m_recentDeltaWasBlank) {
    m_recentDeltaTime = interpolationTime;
    m_recentDeltaWasBlank = true;
  }

  if (m_hasRecentChanges && netInterpolationEnabled())
    netElementsNeedLoad(false);
}

void NetElementSyncGroup::netElementsNeedLoad(bool) {}

void NetElementSyncGroup::netElementsNeedStore() {}

void NetElementCallbackGroup::setNeedsLoadCallback(function<void(bool)> needsLoadCallback) {
  m_netElementsNeedLoad = std::move(needsLoadCallback);
}

void NetElementCallbackGroup::setNeedsStoreCallback(function<void()> needsStoreCallback) {
  m_netElementsNeedStore = std::move(needsStoreCallback);
}

void NetElementCallbackGroup::netElementsNeedLoad(bool load) {
  if (m_netElementsNeedLoad)
    m_netElementsNeedLoad(load);
}

void NetElementCallbackGroup::netElementsNeedStore() {
  if (m_netElementsNeedStore)
    m_netElementsNeedStore();
}

}