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

summaryrefslogtreecommitdiff
path: root/source/game/StarTechController.hpp
blob: badc4eca6b9657fe46ab976696f48af40c496662 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#ifndef STAR_TECH_CONTROLLER_HPP
#define STAR_TECH_CONTROLLER_HPP

#include "StarNetElementSystem.hpp"
#include "StarNetworkedAnimator.hpp"
#include "StarLuaComponents.hpp"
#include "StarLuaActorMovementComponent.hpp"
#include "StarTechDatabase.hpp"

namespace Star {

STAR_CLASS(TechController);
STAR_CLASS(StatusController);

// Class that acts as a movement controller for the parent entity that supports
// a variety scriptable "Tech" that the entity can use that affect movement,
// physics, sounds, particles, damage regions, etc.  Network capable, and all
// flags are sensibly set on both the client and server.
class TechController : public NetElementGroup {
public:
  enum class ParentState {
    Stand,
    Fly,
    Fall,
    Sit,
    Lay,
    Duck,
    Walk,
    Run,
    Swim
  };
  static EnumMap<ParentState> const ParentStateNames;

  TechController();

  Json diskStore();
  void diskLoad(Json const& store);

  void init(Entity* parentEntity, ActorMovementController* movementController, StatusController* statusController);
  void uninit();

  void setLoadedTech(StringList const& techModules, bool forceLoad = false);
  StringList loadedTech() const;
  void reloadTech();

  bool techOverridden() const;
  void setOverrideTech(StringList const& techModules);
  void clearOverrideTech();

  void setShouldRun(bool shouldRun);

  void beginPrimaryFire();
  void beginAltFire();
  void endPrimaryFire();
  void endAltFire();

  void moveUp();
  void moveDown();
  void moveLeft();
  void moveRight();
  void jump();
  void special(int specialKey);

  void setAimPosition(Vec2F const& aimPosition);

  void tickMaster();
  void tickSlave();

  Maybe<ParentState> parentState() const;
  String parentDirectives() const;
  Vec2F parentOffset() const;
  bool toolUsageSuppressed() const;

  bool parentHidden() const;

  List<Drawable> backDrawables();
  List<Drawable> frontDrawables();

  List<LightSource> lightSources() const;

  List<AudioInstancePtr> pullNewAudios();
  List<Particle> pullNewParticles();

  Maybe<Json> receiveMessage(String const& message, bool localMessage, JsonArray const& args = {});

private:
  struct TechAnimator : public NetElement {
    TechAnimator(Maybe<String> animationConfig = {});

    void initNetVersion(NetElementVersion const* version = nullptr) override;

    void netStore(DataStream& ds) const override;
    void netLoad(DataStream& ds) override;

    void enableNetInterpolation(float extrapolationHint = 0.0f) override;
    void disableNetInterpolation() override;
    void tickNetInterpolation(float dt) override;

    bool writeNetDelta(DataStream& ds, uint64_t fromVersion) const override;
    void readNetDelta(DataStream& ds, float interpolationTime = 0.0) override;
    void blankNetDelta(float interpolationTime) override;

    // If setting invisible, stops all playing audio
    void setVisible(bool visible);
    bool isVisible() const;

    Maybe<String> animationConfig;
    NetworkedAnimator animator;
    NetworkedAnimator::DynamicTarget dynamicTarget;
    NetElementBool visible;
    NetElementGroup netGroup;
  };

  typedef NetElementDynamicGroup<TechAnimator> TechAnimatorGroup;

  struct TechModule {
    TechConfig config;

    LuaMessageHandlingComponent<LuaStorableComponent<LuaActorMovementComponent<LuaUpdatableComponent<LuaWorldComponent<LuaBaseComponent>>>>>
        scriptComponent;
    bool visible;
    bool toolUsageSuppressed;
    String parentDirectives;
    TechAnimatorGroup::ElementId animatorId;
  };

  // Name of module, any existing module script data.
  void setupTechModules(List<tuple<String, JsonObject>> const& moduleInits);

  void unloadModule(TechModule& techModule);

  void initializeModules();

  void resetMoves();
  void updateAnimators();

  LuaCallbacks makeTechCallbacks(TechModule& techModule);

  Maybe<StringList> m_overriddenTech;
  LinkedList<TechModule> m_techModules;
  TechAnimatorGroup m_techAnimators;

  Entity* m_parentEntity;
  ActorMovementController* m_movementController;
  StatusController* m_statusController;

  bool m_moveRun;
  bool m_movePrimaryFire;
  bool m_moveAltFire;
  bool m_moveUp;
  bool m_moveDown;
  bool m_moveLeft;
  bool m_moveRight;
  bool m_moveJump;
  bool m_moveSpecial1;
  bool m_moveSpecial2;
  bool m_moveSpecial3;

  Vec2F m_aimPosition;

  NetElementData<Maybe<ParentState>> m_parentState;
  NetElementString m_parentDirectives;
  NetElementFloat m_xParentOffset;
  NetElementFloat m_yParentOffset;
  NetElementBool m_parentHidden;
  NetElementBool m_toolUsageSuppressed;
};

}

#endif