Skip to content

Refactor Registry.kt with Polymorphic Behavior Model - #153

Merged
candour merged 2 commits into
mainfrom
refactor-registry-polymorphism-9736991352443048903
May 12, 2026
Merged

Refactor Registry.kt with Polymorphic Behavior Model#153
candour merged 2 commits into
mainfrom
refactor-registry-polymorphism-9736991352443048903

Conversation

@candour

@candour candour commented May 12, 2026

Copy link
Copy Markdown
Owner

This refactoring replaces a conditional-heavy architecture with a polymorphic behavior model in Registry.kt. It improves code maintainability and follows clean coding principles by delegating type-specific logic to specialized behavior classes.


PR created automatically by Jules for task 9736991352443048903 started by @candour

Summary by CodeRabbit

  • Bug Fixes

    • Puddle slow now correctly applies when enemies occupy permanently wet tiles, improving slowdown consistency.
  • Refactor

    • Stall and enemy behavior logic reorganized into per-type behavior strategies for more consistent and maintainable effects.
  • Documentation

    • Added a release note entry documenting the behavior refactor.

Review Change Stack

- Introduced `StallBehavior` and `EnemyBehavior` interfaces to encapsulate specific logic.
- Implemented `DefaultStallBehavior` and `DefaultEnemyBehavior` classes.
- Created specialized behavior classes for each stall and enemy type (e.g., `SatayStallBehavior`, `TouristEnemyBehavior`).
- Updated `StallDefinition` and `EnemyDefinition` to delegate logic to their behaviors, eliminating large `when` blocks.
- Updated `StallRegistry` and `EnemyRegistry` to instantiate and pass the appropriate behaviors.
- Simplified `MainViewModel.kt` to use the updated behavior-based API.
- Recorded the refactor in `fixes.md` with ID `REF-007`.

Co-authored-by: candour <4670475+candour@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@coderabbitai

coderabbitai Bot commented May 12, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: d1de2a0e-d955-426c-8009-7f0fc3aff830

📥 Commits

Reviewing files that changed from the base of the PR and between 41cf259 and 0c7a5a6.

📒 Files selected for processing (1)
  • app/src/main/java/com/messark/hawker/registry/Registry.kt
🚧 Files skipped from review as they are similar to previous changes (1)
  • app/src/main/java/com/messark/hawker/registry/Registry.kt

📝 Walkthrough

Walkthrough

This PR refactors stall and enemy combat behavior from type-based conditional logic into a polymorphic strategy pattern, introducing StallBehavior and EnemyBehavior interfaces. Concrete behavior implementations replace hardcoded when expressions in definitions, registries wire each stall and enemy type to the appropriate behavior strategy, and MainViewModel's puddle-slow logic is simplified to delegate through the behavior interface.

Changes

Behavior Strategy Pattern and Registry Refactor

Layer / File(s) Summary
Behavior interfaces and defaults
app/src/main/java/com/messark/hawker/registry/Registry.kt
StallBehavior and EnemyBehavior interfaces define method contracts for damage modifiers, freeze modifiers, speed boosts, puddle slow, and special behavior updates. DefaultStallBehavior and DefaultEnemyBehavior provide baseline implementations.
Stall behavior implementations
app/src/main/java/com/messark/hawker/registry/Registry.kt
TehTarikStallBehavior creates sticky puddles with DRAIN tile duration halving. SatayStallBehavior applies enemy-type-specific damage and fires arc projectiles. DurianStallBehavior grants speed boost against SALARYMAN. IceKachangStallBehavior applies freeze durations by enemy type and fires freeze projectiles.
Enemy behavior implementations
app/src/main/java/com/messark/hawker/registry/Registry.kt
TouristEnemyBehavior implements time-based stop/start logic with configurable stop duration. DeliveryRiderEnemyBehavior and AuntieEnemyBehavior specialize puddle slow multipliers.
Definition delegation and registry wiring
app/src/main/java/com/messark/hawker/registry/Registry.kt
StallDefinition and EnemyDefinition add behavior fields and delegate modifier computation, firing, puddle slow, and special behavior updates to configured strategies. StallRegistry entries for TEH_TARIK, SATAY, DURIAN, ICE_KACHANG and EnemyRegistry entries for TOURIST, DELIVERY_RIDER, AUNTIE are updated to instantiate the correct behavior implementations.
MainViewModel enemy movement
app/src/main/java/com/messark/hawker/MainViewModel.kt
Enemy puddle slow calculation now delegates to enemyDef.getPuddleSlowMultiplier() and applies slow when the enemy stands on a permanently wet hex, replacing prior proximity-based puddle lookups.
Changelog entry
fixes.md
REF-007 documents the behavior refactor, registry polymorphism, and MainViewModel simplification.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • candour/towerpower#147: Both PRs touch the same puddle-slow mechanic: they modify MainViewModel.updateGame to apply additional enemy speed slowdown when a tile is isPermanentlyWet, relying on the registry’s per-enemy puddle slow multiplier logic.
  • candour/towerpower#151: Both PRs modify Registry.kt’s stall-firing logic for TEH_TARIK and SATAY and related registry wiring concerns.
  • candour/towerpower#125: Both PRs modify MainViewModel’s enemy movement/puddle slow logic and are related at the code-level for slow computation.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 3.23% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change in the pull request—a refactoring of Registry.kt to implement a polymorphic behavior model that replaces type-based conditional logic with delegated behavior classes.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor-registry-polymorphism-9736991352443048903

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions

Copy link
Copy Markdown

Build Successful! 🚀

Download APK

Note: This link will be removed when the PR is closed.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 50b7d86f-07b1-434d-8cbb-ce3d86fa954f

📥 Commits

Reviewing files that changed from the base of the PR and between 3fd62d6 and 41cf259.

📒 Files selected for processing (3)
  • app/src/main/java/com/messark/hawker/MainViewModel.kt
  • app/src/main/java/com/messark/hawker/registry/Registry.kt
  • fixes.md

Comment thread app/src/main/java/com/messark/hawker/registry/Registry.kt
@candour

candour commented May 12, 2026

Copy link
Copy Markdown
Owner Author

Initialize Tourist lastStopMs at spawn time to prevent immediate stall.

The Enemy model defaults lastStopMs to 0L, and EnemyRegistry.toEnemy() does not override this. When a Tourist spawns at any time after ~8 seconds, the condition on line 203 (currentTimeMs - lastStopMs > 8000) evaluates true immediately, causing the tourist to stall on its first update tick. Initialize lastStopMs = currentTimeMs when spawning tourists, or set it on first behavior update.

@candour
candour enabled auto-merge (squash) May 12, 2026 23:43
@google-labs-jules

Copy link
Copy Markdown
Contributor

Initialize Tourist lastStopMs at spawn time to prevent immediate stall.

The Enemy model defaults lastStopMs to 0L, and EnemyRegistry.toEnemy() does not override this. When a Tourist spawns at any time after ~8 seconds, the condition on line 203 (currentTimeMs - lastStopMs > 8000) evaluates true immediately, causing the tourist to stall on its first update tick. Initialize lastStopMs = currentTimeMs when spawning tourists, or set it on first behavior update.

Great catch! I've updated TouristEnemyBehavior.updateSpecialBehavior to initialize lastStopMs to currentTimeMs if it's currently 0L. This ensures Tourists don't stall immediately upon spawning if the game has already been running for a while.

…PR feedback

- Introduced `StallBehavior` and `EnemyBehavior` interfaces to encapsulate specific logic.
- Implemented `DefaultStallBehavior` and `DefaultEnemyBehavior` classes.
- Created specialized behavior classes for each stall and enemy type.
- Updated `StallDefinition` and `EnemyDefinition` to delegate logic to their behaviors.
- Updated registries to assign these behaviors.
- Fixed Tourist stalling bug by initializing `lastStopMs` in `TouristEnemyBehavior`.
- Simplified `MainViewModel.kt` to use the updated behavior-based API.
- Recorded the refactor in `fixes.md` with ID `REF-007`.

Co-authored-by: candour <4670475+candour@users.noreply.github.com>
@candour
candour merged commit cc3d4f6 into main May 12, 2026
3 checks passed
@candour
candour deleted the refactor-registry-polymorphism-9736991352443048903 branch May 12, 2026 23:49
@github-actions

Copy link
Copy Markdown

Build Successful! 🚀

Download APK

Note: This link will be removed when the PR is closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant