From ee734874d58aecc1dfb491af3db9f9a18f3de639 Mon Sep 17 00:00:00 2001 From: aflak Date: Tue, 5 Nov 2024 11:39:50 -0500 Subject: [PATCH] issue #2003: rotating targeting cursor --- src/utility/hex.ts | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/utility/hex.ts b/src/utility/hex.ts index 4ed5df534..58d38f41c 100644 --- a/src/utility/hex.ts +++ b/src/utility/hex.ts @@ -87,6 +87,16 @@ export class Hex { */ displayPos: { x: number; y: number }; + /** + * Set to true if cursor is outside movement range. + */ + isSpinning: boolean; + + /** + * Store ID of animation frame request. + */ + spinRequest: number; + originalDisplayPos: { x: number; y: number }; tween: Phaser.Tween; hitBox: Phaser.Sprite; @@ -131,6 +141,9 @@ export class Hex { this.originalDisplayPos = $j.extend({}, this.displayPos); + this.isSpinning = false; + this.spinRequest = null; + this.tween = null; if (grid) { @@ -499,7 +512,40 @@ export class Hex { this.updateStyle(); } + /** + * Start spin effect for the targeting cursor + */ + startSpinning() { + this.isSpinning = true; + const spinSpeed = 2; + + const rotate = () => { + if (!this.isSpinning) return; + this.overlay.angle += spinSpeed; + this.spinRequest = requestAnimationFrame(rotate); + }; + + this.spinRequest = requestAnimationFrame(rotate); + } + + /** + * Stop spin effect for the targeting cursor + */ + stopSpinning() { + this.isSpinning = false; + if (this.spinRequest) { + cancelAnimationFrame(this.spinRequest); + this.spinRequest = null; + this.overlay.angle = 0; + } + } + updateStyle() { + // Reset spinning state + if (this.isSpinning) { + this.stopSpinning(); + } + // Display Hex let targetAlpha = this.reachable || Boolean(this.displayClasses.match(/creature/g)); @@ -587,6 +633,10 @@ export class Hex { this.grid.overlayHexesGroup.bringToTop(this.overlay); } else { this.overlay.loadTexture('cancel'); + this.overlay.anchor.set(0.5, 0.5); + if (!this.isSpinning) { + this.startSpinning(); + } } this.overlay.alpha = targetAlpha ? 1 : 0;