Glowstick0017 / Little-Plane-Project

Flying through a procedural web world
https://glowstick.me/tlpp/
MIT License
102 stars 26 forks source link

Refactor Controls.js for better readability and performance #8

Closed vlazic closed 11 months ago

vlazic commented 11 months ago

Summary of Changes:

  1. Introduced Constants for Keys: Moved hardcoded keys to a constant dictionary KEYS for better readability and easier future modifications.

  2. Centralized Movement and Rotation: Implemented a unified moveAndRotate function to handle both movement and rotation, reducing repetition and improving code clarity.

  3. Optimized Key Event Handlers:

    • Separated the handlers for key press and release (registerKeyPress and deregisterKeyPress) for better organization.
    • Added a helper formatKey function to streamline the key formatting process, ensuring uniformity.
  4. Mapped Keys to Movements: Created a movementMapping dictionary to associate keys with their corresponding movement functions, enabling easier modifications and additions in the future.

  5. Enhanced Game Loop:

    • Integrated the movement functionality directly within the game loop using the mapping, ensuring only necessary movements are processed.
    • Integrated the needsRedraw flag in the game loop, ensuring the canvas redraws only when there's a change.

By implementing these changes, the Controls.js file is now more structured, efficient, and easily extensible for future updates.

Glowstick0017 commented 11 months ago

Please update movement mapping so when two keys are pressed the plane is shown diagonally as it exists currently.

vlazic commented 11 months ago

Please update movement mapping so when two keys are pressed the plane is shown diagonally as it exists currently.

Done :)

Glowstick0017 commented 11 months ago

I removed DIAGONAL_SPEED_MULTIPLIER and the movement from diagonals because it was stacking on top of the movement already being made.