Created
February 26, 2024 05:29
-
-
Save tristanbob/b73a334affd8f27242fc8b2fc4cd501c to your computer and use it in GitHub Desktop.
GDevelop extension function to center a group of cards in the player's hand
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Duration in seconds, adjust as needed | |
| const duration = eventsFunctionContext.getArgument("TweenDuration") | |
| // Easing function, adjust as needed | |
| const easing = eventsFunctionContext.getArgument("TweenEasing") | |
| // const duration = 2; | |
| // const easing = "easeInOutQuad"; | |
| const paddingBetweenCards = 10 | |
| /** @type {gdjs.SpriteRuntimeObject} */ | |
| const cards = eventsFunctionContext.getObjects("PlayerCards"); | |
| // Calculate the total width required for all cards with padding between them | |
| let totalWidth = 0; | |
| cards.forEach(function (card, index) { | |
| totalWidth += card.getWidth(); | |
| if (index > 0) totalWidth += paddingBetweenCards; // Add padding after the first card | |
| }); | |
| // Get the game screen width and height | |
| const screenWidth = runtimeScene.getGame().getGameResolutionWidth(); | |
| const screenHeight = runtimeScene.getGame().getGameResolutionHeight(); | |
| // Calculate the starting X position to center the group of cards | |
| let currentX = (screenWidth - totalWidth) / 2; | |
| // Position each card using a tween | |
| cards.forEach(function (card, index) { | |
| const cardHeight = card.getHeight(); | |
| const finalX = currentX; | |
| const finalY = screenHeight - cardHeight; | |
| console.log(`Card ${index} moving to:`, finalX, finalY); | |
| // Define the tween properties | |
| const tweenName = `moveCard${index}`; // Unique name for each tween | |
| // Create a new position tween for the card to move it to its final position | |
| if (!card.hasBehavior("Tween")) { | |
| console.error("Card object does not have TweenBehavior:", card); | |
| return; // Skip this card or handle the error appropriately | |
| } | |
| /** @type {gdjs.TweenRuntimeBehavior} */ | |
| const TweenBehavior = card.getBehavior("Tween"); | |
| TweenBehavior.addObjectPositionTween2( | |
| tweenName, // Unique id to identify the tween | |
| finalX, // The target X position | |
| finalY, // The target Y position | |
| duration, // Duration of the tween in seconds | |
| easing, // Easing function identifier | |
| false // Destroy this object when the tween ends (usually false) | |
| ); | |
| // Update currentX for the next card, including padding | |
| currentX += card.getWidth() + paddingBetweenCards; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment