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

summaryrefslogtreecommitdiff
path: root/source/game/StarProjectileDatabase.cpp
blob: bb7fbc7e65bdb2871bc616a70a3fe146c160c7ea (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
172
173
174
175
#include "StarProjectileDatabase.hpp"
#include "StarProjectile.hpp"
#include "StarJsonExtra.hpp"
#include "StarAssets.hpp"
#include "StarRoot.hpp"

namespace Star {

ProjectileDatabase::ProjectileDatabase() {
  auto assets = Root::singleton().assets();

  auto& files = assets->scanExtension("projectile");
  assets->queueJsons(files);
  for (auto& file : files) {
    try {
      auto projectileConfig = readConfig(file);
      if (m_configs.contains(projectileConfig->typeName))
        Logger::error("Duplicate projectile asset typeName {}. configfile {}", projectileConfig->typeName, file);
      else
        m_configs[projectileConfig->typeName] = projectileConfig;
    } catch (std::exception const& e) {
      Logger::error("Could not read projectile '{}', error: {}", file, outputException(e, false));
    }
  }
}

StringList ProjectileDatabase::allProjectileTypes() const {
  return m_configs.keys();
}

bool ProjectileDatabase::isProjectile(String const& projectileName) const {
  return m_configs.contains(projectileName);
}

Json ProjectileDatabase::projectileConfig(String const& type) const {
  if (!m_configs.contains(type))
    throw ProjectileDatabaseException(strf("Unknown projectile with typeName {}.", type));
  return m_configs.get(type)->config;
}

ProjectilePtr ProjectileDatabase::createProjectile(String const& type, Json const& parameters) const {
  if (!m_configs.contains(type))
    throw ProjectileDatabaseException(strf("Unknown projectile with typeName {}.", type));
  return make_shared<Projectile>(m_configs.get(type), parameters);
}

String ProjectileDatabase::damageKindImage(String const& type) const {
  if (!m_configs.contains(type))
    throw ProjectileDatabaseException(strf("Unknown projectile with typeName {}.", type));
  auto& config = m_configs.get(type);
  return config->damageKindImage;
}

float ProjectileDatabase::gravityMultiplier(String const& type) const {
  if (!m_configs.contains(type))
    throw ProjectileDatabaseException(strf("Unknown projectile with typeName {}.", type));
  auto& config = m_configs.get(type);
  return config->movementSettings.getFloat("gravityMultiplier", 1);
}

ProjectilePtr ProjectileDatabase::netLoadProjectile(ByteArray const& netStore, NetCompatibilityRules rules) const {
  DataStreamBuffer ds(netStore);
  ds.setStreamCompatibilityVersion(rules);
  String typeName = ds.read<String>();
  return make_shared<Projectile>(m_configs.get(typeName), ds, rules);
}

ProjectileConfigPtr ProjectileDatabase::readConfig(String const& path) {
  auto assets = Root::singleton().assets();

  Json config = assets->json(path);

  auto projectileConfig = make_shared<ProjectileConfig>();
  projectileConfig->config = config;
  projectileConfig->typeName = config.getString("projectileName");
  projectileConfig->directory = AssetPath::directory(path);

  projectileConfig->description = config.getString("description", "");

  projectileConfig->boundBox = config.opt("boundBox").apply(jsonToRectF).value({-5, -5, 5, 5});

  auto physicsType = config.getString("physics", "default");
  JsonObject movementSettings = config.getObject("movementSettings", JsonObject());
  projectileConfig->movementSettings =
      jsonMerge(assets->json(strf("/projectiles/physics.config:{}", physicsType)), movementSettings);

  projectileConfig->initialSpeed = config.getFloat("speed", 50);
  projectileConfig->acceleration = config.getFloat("acceleration", 0);
  projectileConfig->power = config.getFloat("power", 1);
  if (config.contains("damagePoly")) {
    projectileConfig->damagePoly = jsonToPolyF(config.get("damagePoly"));
    projectileConfig->damagePoly.scale(1.0f / TilePixels);
  }
  projectileConfig->piercing = config.getBool("piercing", false);
  projectileConfig->falldown = config.getBool("falldown", false);
  projectileConfig->bounces = config.getInt("bounces", 0);

  projectileConfig->actionOnCollide = config.getArray("actionOnCollide", {});
  projectileConfig->actionOnReap = config.getArray("actionOnReap", {});
  projectileConfig->actionOnHit = config.getArray("actionOnHit", {});
  projectileConfig->actionOnTimeout = config.getArray("actionOnTimeout", {});

  for (auto const& c : config.getArray("periodicActions", {}))
    projectileConfig->periodicActions.append(make_tuple(c.getFloat("time"), c.getBool("repeat", true), c));

  projectileConfig->image = AssetPath::relativeTo(path, config.getString("image"));
  projectileConfig->frameNumber = config.getUInt("frameNumber", 1);
  projectileConfig->animationCycle = config.getFloat("animationCycle", 1.0);
  projectileConfig->animationLoops = config.getBool("animationLoops", true);
  projectileConfig->windupFrames = config.getUInt("windupFrames", 0);
  projectileConfig->intangibleWindup = config.getBool("intangibleWindup", false);
  projectileConfig->winddownFrames = config.getUInt("winddownFrames", 0);
  projectileConfig->intangibleWinddown = config.getBool("intangibleWinddown", false);
  projectileConfig->flippable = config.getBool("flippable", false);
  projectileConfig->orientationLocked = config.getBool("orientationLocked", false);

  projectileConfig->fullbright = config.getBool("fullbright", false);
  projectileConfig->renderLayer = parseRenderLayer(config.getString("renderLayer", "Projectile"));

  projectileConfig->lightColor = jsonToColor(config.get("lightColor", JsonArray{0, 0, 0}));
  projectileConfig->lightPosition = jsonToVec2F(config.get("lightPosition", JsonArray{0, 0}));
  if (auto lightType = config.optString("lightType"))
    projectileConfig->lightType = LightTypeNames.getLeft(*lightType);
  else
    projectileConfig->lightType = (LightType)config.getBool("pointLight", false);

  projectileConfig->persistentAudio = config.getString("persistentAudio", "");

  // Initialize timeToLive after animationCycle so we can have the default be
  // based on animationCycle
  if (!projectileConfig->animationLoops)
    projectileConfig->timeToLive = config.getFloat("timeToLive", projectileConfig->animationCycle);
  else
    projectileConfig->timeToLive = config.getFloat("timeToLive", 5.0);

  projectileConfig->damageKindImage = config.getString("damageKindImage", "");

  projectileConfig->damageKind = config.getString("damageKind", "");
  projectileConfig->damageType = config.getString("damageType", "damage");
  projectileConfig->damageTeam = config.get("damageTeam", Json());
  projectileConfig->damageRepeatGroup = config.optString("damageRepeatGroup");
  projectileConfig->damageRepeatTimeout = config.optFloat("damageRepeatTimeout");

  if (!projectileConfig->damageKindImage.empty())
    projectileConfig->damageKindImage =
        AssetPath::relativeTo(projectileConfig->directory, projectileConfig->damageKindImage);

  projectileConfig->statusEffects = config.getArray("statusEffects", {}).transformed(jsonToEphemeralStatusEffect);

  projectileConfig->emitters = jsonToStringSet(config.getArray("emitters", JsonArray()));

  projectileConfig->hydrophobic = config.getBool("hydrophobic", false);

  projectileConfig->rayCheckToSource = config.getBool("rayCheckToSource", false);
  projectileConfig->knockback = config.getFloat("knockback", 0.0f);
  projectileConfig->knockbackDirectional = config.getBool("knockbackDirectional", false);

  projectileConfig->onlyHitTerrain = config.getBool("onlyHitTerrain", false);

  projectileConfig->clientEntityMode = ClientEntityModeNames.getLeft(config.getString("clientEntityMode", "ClientMasterAllowed"));
  projectileConfig->masterOnly = config.getBool("masterOnly", false);

  projectileConfig->scripts =
      jsonToStringList(config.get("scripts", JsonArray())).transformed(bind(AssetPath::relativeTo, path, _1));

  projectileConfig->physicsForces = config.get("physicsForces", JsonObject());
  projectileConfig->physicsCollisions = config.get("physicsCollisions", JsonObject());

  projectileConfig->persistentStatusEffects = config.getArray("persistentStatusEffects", JsonArray()).transformed(jsonToPersistentStatusEffect);
  projectileConfig->statusEffectArea = jsonToPolyF(config.get("statusEffectArea", JsonArray()));

  return projectileConfig;
}

}