From ca21b29f925858bf045fc92cb0e6b57bc2f4cb89 Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Mon, 21 Aug 2023 21:22:25 +1000 Subject: [PATCH] Small cleanup --- source/game/items/StarMaterialItem.cpp | 76 ++++++++++++-------------- source/game/items/StarMaterialItem.hpp | 2 +- 2 files changed, 37 insertions(+), 41 deletions(-) diff --git a/source/game/items/StarMaterialItem.cpp b/source/game/items/StarMaterialItem.cpp index f3b955c..c6d2ce9 100644 --- a/source/game/items/StarMaterialItem.cpp +++ b/source/game/items/StarMaterialItem.cpp @@ -42,7 +42,6 @@ MaterialItem::MaterialItem(Json const& config, String const& directory, Json con m_blockRadius = config.getFloat("blockRadius", defaultParameters.getFloat("blockRadius")); m_altBlockRadius = config.getFloat("altBlockRadius", defaultParameters.getFloat("altBlockRadius")); 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_placeSounds = jsonToStringList(config.get("placeSounds", JsonArray())); @@ -68,7 +67,8 @@ void MaterialItem::init(ToolUserEntity* owner, ToolHand hand) { FireableItem::init(owner, hand); BeamItem::init(owner, hand); owner->addSound(Random::randValueFrom(m_placeSounds), 1.0f, 2.0f); - updateProperties(); + if (auto player = as(owner)) + updatePropertiesFromPlayer(player); } void MaterialItem::uninit() { @@ -85,39 +85,37 @@ void MaterialItem::update(float dt, FireMode fireMode, bool shifting, HashSetisMaster()) { - Input& input = Input::singleton(); - if (auto presses = input.bindDown("opensb", "materialCollisionCycle")) { - CollisionKind baseKind = Root::singleton().materialDatabase()->materialCollisionKind(m_material); - for (size_t i = 0; i != *presses; ++i) { - constexpr auto limit = (uint8_t)TileCollisionOverride::Block + 1; - while (true) { - m_collisionOverride = TileCollisionOverride(((uint8_t)m_collisionOverride + 1) % limit); - if (collisionKindFromOverride(m_collisionOverride) != baseKind) - break; - } - if (auto player = as(owner())) + if (Player* player = as(owner())) { + if (owner()->isMaster()) { + Input& input = Input::singleton(); + if (auto presses = input.bindDown("opensb", "materialCollisionCycle")) { + CollisionKind baseKind = Root::singleton().materialDatabase()->materialCollisionKind(m_material); + for (size_t i = 0; i != *presses; ++i) { + constexpr auto limit = (uint8_t)TileCollisionOverride::Block + 1; + while (true) { + m_collisionOverride = TileCollisionOverride(((uint8_t)m_collisionOverride + 1) % limit); + if (collisionKindFromOverride(m_collisionOverride) != baseKind) + break; + } 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)); - } - if (auto presses = input.bindDown("opensb", "buildingRadiusGrow")) { - m_blockRadius = min(BlockRadiusLimit, int(m_blockRadius + *presses)); - if (auto player = as(owner())) + if (auto presses = input.bindDown("opensb", "buildingRadiusGrow")) { + m_blockRadius = min(BlockRadiusLimit, int(m_blockRadius + *presses)); 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")) { - m_blockRadius = max(1, int(m_blockRadius - *presses)); - if (auto player = as(owner())) + if (auto presses = input.bindDown("opensb", "buildingRadiusShrink")) { + m_blockRadius = max(1, int(m_blockRadius - *presses)); 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 { - updateProperties(); + else + updatePropertiesFromPlayer(player); } } @@ -269,20 +267,18 @@ List const& MaterialItem::generatedPreview(Vec2I position) const { return *m_generatedPreviewCache; } -void MaterialItem::updateProperties() { - if (auto player = as(owner())) { - auto blockRadius = player->getSecretProperty(BlockRadiusPropertyKey); - if (blockRadius.isType(Json::Type::Float)) - m_blockRadius = blockRadius.toFloat(); +void MaterialItem::updatePropertiesFromPlayer(Player* player) { + auto blockRadius = player->getSecretProperty(BlockRadiusPropertyKey); + if (blockRadius.isType(Json::Type::Float)) + m_blockRadius = blockRadius.toFloat(); - auto altBlockRadius = player->getSecretProperty(AltBlockRadiusPropertyKey); - if (altBlockRadius.isType(Json::Type::Float)) - m_altBlockRadius = altBlockRadius.toFloat(); + auto altBlockRadius = player->getSecretProperty(AltBlockRadiusPropertyKey); + if (altBlockRadius.isType(Json::Type::Float)) + m_altBlockRadius = altBlockRadius.toFloat(); - auto collisionOverride = player->getSecretProperty(CollisionOverridePropertyKey); - if (collisionOverride.isType(Json::Type::String)) - m_collisionOverride = TileCollisionOverrideNames.maybeLeft(collisionOverride.toString()).value(TileCollisionOverride::None); - } + auto collisionOverride = player->getSecretProperty(CollisionOverridePropertyKey); + if (collisionOverride.isType(Json::Type::String)) + m_collisionOverride = TileCollisionOverrideNames.maybeLeft(collisionOverride.toString()).value(TileCollisionOverride::None); } float MaterialItem::calcRadius(bool shifting) const { diff --git a/source/game/items/StarMaterialItem.hpp b/source/game/items/StarMaterialItem.hpp index 9d397eb..e0a5c3c 100644 --- a/source/game/items/StarMaterialItem.hpp +++ b/source/game/items/StarMaterialItem.hpp @@ -47,7 +47,7 @@ public: List previewTiles(bool shifting) const override; List const& generatedPreview(Vec2I position = {}) const; private: - void updateProperties(); + void updatePropertiesFromPlayer(Player* player); float calcRadius(bool shifting) const; List& tileArea(float radius, Vec2F const& position) const; MaterialHue placementHueShift(Vec2I const& position) const;