0.5.0 • Published 8 months ago

ton-game-sdk v0.5.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

ton-game-sdk - Phaser 3 UI Components Library and utilities

ton-game-sdk is a UI component library and utilities for the TON blockchain bindings in the Phaser game engine.

Key Features

  • Phaser 3 Compatibility: Fully compatible with Phaser 3.8+.
  • Customizable Components: Out-of-the-box UI components such as buttons, sliders, checkboxes, dialogs, and more.
  • TypeScript Support: TypeScript types for a better development experience.
  • Flexible Event Handling: Easy-to-use event handlers for interactions like clicks, hovers, and value changes.
  • Responsive Design: Adaptable UI elements that work across different screen sizes and game layouts.

Supported Components

  • ✅ Checkbox
  • ✅ CheckboxGroup
  • ✅ Container
  • ✅ Dialog
  • ✅ Grid
  • ✅ LinearLayout
  • ✅ Image
  • ✅ ImageButton
  • ✅ Label
  • ✅ ProgressBar
  • ✅ RoundedButton
  • ✅ Slider
  • ✅ Tabs
  • ✅ Text
  • ✅ TextBox
  • ✅ TextButton
  • ✅ Toast
  • ✅ VolumeSlider
  • ✅ Sprite

Installation

Install ton-game-sdk via npm or yarn:

npm install ton-game-sdk
# or
yarn add ton-game-sdk

Usage Example

Here’s a quick example of how to create different types of buttons using the ton-game-sdk library:

import { BaseScene, Mai3Game } from "ton-game-sdk";

export class ButtonDemo extends BaseScene {
  constructor() {
    super('ButtonDemo');
  }
  
  preload() {
    super.preload();
  }

  create() {
    this.mai3.add.imageButton({
        x: 10,
        y: 160,
        width: 160,
        height: 60,
        texture: "StartGameButton",
        borderWidth: 3,
        handleHover: {
          audio: "sfx-hover",
          texture: "StartGameButtonHover",
        },
        handleOut: {
          texture: "StartGameButton",
        },
        handleDown: {
          audio: "sfx-press",
          texture: "StartGameButtonDown",
          handleFn: () => {
            console.log("Button pressed");
          }
        },
        handleUp: {
          texture: "StartGameButton",
          handleFn: () => {
            console.log("Button released");
          }
        },
    });
  }
}

const config = getGameConfig();
const game = Mai3Game.Init(config);
game.scene.add('ButtonDemo', ButtonDemo, true);

GameFi SDK for TON Blockchain Integration

The GameFi SDK provides bindings and utilities to integrate TON blockchain functionalities into game engines like Phaser, Cocos, and vanilla JavaScript. This allows developers to easily add blockchain-based features like wallet connection, in-game purchases, NFT interactions, and more to their games.

Connecting Wallet

import { BaseScene, Mai3Game } from "ton-game-sdk";

export class ButtonDemo extends BaseScene {
  constructor() {
    super('ButtonDemo');
  }
  
  preload() {
    super.preload();
  }

  create() {
    this.mai3.add.connectWalletButton({
      x: 200,
      y: 200,
      width: 160,
      height: 60,
      style: 'dark',
      language: 'en',
      walletApp: 'telegram-wallet',
      onWalletChange: (wallet: Wallet | null) => {
        console.log('wallet address: ', wallet?.account.address);
      }
    });
  }
}

const config = getGameConfig();
const game = Mai3Game.Init(config);
game.scene.add('ButtonDemo', ButtonDemo, true);

This can be used to:

  • Watch the wallet state and reflect it on the game UI.
  • Restore connection with a previously connected wallet after app reload.

GameFi Methods & Props

GameFi Static Methods

MethodDescription
createCreates a GameFi instance

GameFi Instance Methods

MethodDescription
createConnectButtonCreates a button to connect a wallet.
connectWalletConnect wallet manually (without built-in UIs).
onWalletChangeWatch whether a wallet was connected or disconnected.
disconnectWalletDisconnect wallet manually.
buyWithTonBuy from in-game shop with TON.
buyWithJettonBuy from in-game shop with Jetton.
transferTonTransfer TON to another wallet.
transferJettonTransfer Jetton to another wallet.
openNftCollectionOpen an NFT collection contract.
openNftSaleOpen an NFT sale contract.
openNftItemOpen an NFT item contract.
openNftItemByIndexOpen an NFT item contract using its index.
openSbtCollectionOpen an SBT collection contract.
openJettonOpen a Jetton contract.
openJettonWalletOpen a Jetton wallet contract.

GameFi Instance Properties

PropertyDescription
assetsSdkAsset-sdk instance for direct use if needed.
walletConnectorWallet connector instance for direct use.
walletUser's connected wallet.
walletAccountUser's connected account.
walletAddressUser's connected wallet address.
merchantAddressIn-game shop's address to receive TON.
merchantJettonAddressIn-game shop's Jetton used as in-game currency.

Contribution

We welcome contributions! If you’d like to help improve mai3-phaser-ui, feel free to submit issues or pull requests on our GitHub repository.

License

This project is licensed under the MIT License. For details, check the LICENSE file.