Small cleanup

This commit is contained in:
Kae 2023-08-21 21:22:25 +10:00
parent ce6cadd2a0
commit ca21b29f92
2 changed files with 37 additions and 41 deletions

View File

@ -42,7 +42,6 @@ MaterialItem::MaterialItem(Json const& config, String const& directory, Json con
m_blockRadius = config.getFloat("blockRadius", defaultParameters.getFloat("blockRadius")); m_blockRadius = config.getFloat("blockRadius", defaultParameters.getFloat("blockRadius"));
m_altBlockRadius = config.getFloat("altBlockRadius", defaultParameters.getFloat("altBlockRadius")); m_altBlockRadius = config.getFloat("altBlockRadius", defaultParameters.getFloat("altBlockRadius"));
m_collisionOverride = TileCollisionOverrideNames.maybeLeft(config.getString("collisionOverride", "None")).value(TileCollisionOverride::None); m_collisionOverride = TileCollisionOverrideNames.maybeLeft(config.getString("collisionOverride", "None")).value(TileCollisionOverride::None);
//TODO: Work out a vanilla-compatible way to network the 3 above for visual reasons without storing as a parameter, because that prevents stacking.
m_multiplace = config.getBool("allowMultiplace", BlockCollisionSet.contains(materialDatabase->materialCollisionKind(m_material))); m_multiplace = config.getBool("allowMultiplace", BlockCollisionSet.contains(materialDatabase->materialCollisionKind(m_material)));
m_placeSounds = jsonToStringList(config.get("placeSounds", JsonArray())); m_placeSounds = jsonToStringList(config.get("placeSounds", JsonArray()));
@ -68,7 +67,8 @@ void MaterialItem::init(ToolUserEntity* owner, ToolHand hand) {
FireableItem::init(owner, hand); FireableItem::init(owner, hand);
BeamItem::init(owner, hand); BeamItem::init(owner, hand);
owner->addSound(Random::randValueFrom(m_placeSounds), 1.0f, 2.0f); owner->addSound(Random::randValueFrom(m_placeSounds), 1.0f, 2.0f);
updateProperties(); if (auto player = as<Player>(owner))
updatePropertiesFromPlayer(player);
} }
void MaterialItem::uninit() { void MaterialItem::uninit() {
@ -85,6 +85,7 @@ void MaterialItem::update(float dt, FireMode fireMode, bool shifting, HashSet<Mo
setEnd(BeamItem::EndType::TileGroup); setEnd(BeamItem::EndType::TileGroup);
m_shifting = shifting; m_shifting = shifting;
if (Player* player = as<Player>(owner())) {
if (owner()->isMaster()) { if (owner()->isMaster()) {
Input& input = Input::singleton(); Input& input = Input::singleton();
if (auto presses = input.bindDown("opensb", "materialCollisionCycle")) { if (auto presses = input.bindDown("opensb", "materialCollisionCycle")) {
@ -96,7 +97,6 @@ void MaterialItem::update(float dt, FireMode fireMode, bool shifting, HashSet<Mo
if (collisionKindFromOverride(m_collisionOverride) != baseKind) if (collisionKindFromOverride(m_collisionOverride) != baseKind)
break; break;
} }
if (auto player = as<Player>(owner()))
player->setSecretProperty(CollisionOverridePropertyKey, TileCollisionOverrideNames.getRight(m_collisionOverride)); player->setSecretProperty(CollisionOverridePropertyKey, TileCollisionOverrideNames.getRight(m_collisionOverride));
} }
owner()->addSound("/sfx/tools/cyclematcollision.ogg", 1.0f, Random::randf(0.9f, 1.1f)); owner()->addSound("/sfx/tools/cyclematcollision.ogg", 1.0f, Random::randf(0.9f, 1.1f));
@ -104,20 +104,18 @@ void MaterialItem::update(float dt, FireMode fireMode, bool shifting, HashSet<Mo
if (auto presses = input.bindDown("opensb", "buildingRadiusGrow")) { if (auto presses = input.bindDown("opensb", "buildingRadiusGrow")) {
m_blockRadius = min(BlockRadiusLimit, int(m_blockRadius + *presses)); m_blockRadius = min(BlockRadiusLimit, int(m_blockRadius + *presses));
if (auto player = as<Player>(owner()))
player->setSecretProperty(BlockRadiusPropertyKey, m_blockRadius); player->setSecretProperty(BlockRadiusPropertyKey, m_blockRadius);
owner()->addSound("/sfx/tools/buildradiusgrow.wav", 1.0f, 1.0f + m_blockRadius / BlockRadiusLimit); owner()->addSound("/sfx/tools/buildradiusgrow.wav", 1.0f, 1.0f + m_blockRadius / BlockRadiusLimit);
} }
if (auto presses = input.bindDown("opensb", "buildingRadiusShrink")) { if (auto presses = input.bindDown("opensb", "buildingRadiusShrink")) {
m_blockRadius = max(1, int(m_blockRadius - *presses)); m_blockRadius = max(1, int(m_blockRadius - *presses));
if (auto player = as<Player>(owner()))
player->setSecretProperty(BlockRadiusPropertyKey, m_blockRadius); player->setSecretProperty(BlockRadiusPropertyKey, m_blockRadius);
owner()->addSound("/sfx/tools/buildradiusshrink.wav", 1.0f, 1.0f + m_blockRadius / BlockRadiusLimit); owner()->addSound("/sfx/tools/buildradiusshrink.wav", 1.0f, 1.0f + m_blockRadius / BlockRadiusLimit);
} }
} }
else { else
updateProperties(); updatePropertiesFromPlayer(player);
} }
} }
@ -269,8 +267,7 @@ List<Drawable> const& MaterialItem::generatedPreview(Vec2I position) const {
return *m_generatedPreviewCache; return *m_generatedPreviewCache;
} }
void MaterialItem::updateProperties() { void MaterialItem::updatePropertiesFromPlayer(Player* player) {
if (auto player = as<Player>(owner())) {
auto blockRadius = player->getSecretProperty(BlockRadiusPropertyKey); auto blockRadius = player->getSecretProperty(BlockRadiusPropertyKey);
if (blockRadius.isType(Json::Type::Float)) if (blockRadius.isType(Json::Type::Float))
m_blockRadius = blockRadius.toFloat(); m_blockRadius = blockRadius.toFloat();
@ -283,7 +280,6 @@ void MaterialItem::updateProperties() {
if (collisionOverride.isType(Json::Type::String)) if (collisionOverride.isType(Json::Type::String))
m_collisionOverride = TileCollisionOverrideNames.maybeLeft(collisionOverride.toString()).value(TileCollisionOverride::None); m_collisionOverride = TileCollisionOverrideNames.maybeLeft(collisionOverride.toString()).value(TileCollisionOverride::None);
} }
}
float MaterialItem::calcRadius(bool shifting) const { float MaterialItem::calcRadius(bool shifting) const {
if (!multiplaceEnabled()) if (!multiplaceEnabled())

View File

@ -47,7 +47,7 @@ public:
List<PreviewTile> previewTiles(bool shifting) const override; List<PreviewTile> previewTiles(bool shifting) const override;
List<Drawable> const& generatedPreview(Vec2I position = {}) const; List<Drawable> const& generatedPreview(Vec2I position = {}) const;
private: private:
void updateProperties(); void updatePropertiesFromPlayer(Player* player);
float calcRadius(bool shifting) const; float calcRadius(bool shifting) const;
List<Vec2I>& tileArea(float radius, Vec2F const& position) const; List<Vec2I>& tileArea(float radius, Vec2F const& position) const;
MaterialHue placementHueShift(Vec2I const& position) const; MaterialHue placementHueShift(Vec2I const& position) const;