Skip to content

mzetkowski/flexible-grid-controller-samples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 

Repository files navigation

Flexible Grid Controller <-> Turn Based Strategy Framework integration v1.1

Table of Contents

  1. Introduction
  2. Getting Started
  3. Integration Guide
  4. Contact and Support

Introduction

This repository demonstrates the integration between the two Unity Asset Store projects: the Flexible Grid Controller and the Turn Based Strategy Framework. The Framework is a flexible tool for developing turn-based strategy games in Unity and is available in the Unity Asset Store. The Flexible Grid Controller facilitates integrating various input methods, such as keyboard or gamepad into any grid-based project. It is a great match for the Turn Based Strategy Framework, but it is not limited to it.

Getting Started

Prerequisites

  • Unity version 2023.2.20f or higher
  • Turn Based Strategy Framework version 4.0.0 or higher
  • Flexible Grid Controller version 1.0.2 or higher

⚠️ Compatibility Note:
The current release of this integration guide is compatible with Turn Based Strategy Framework v4.x.
If you are working with Framework v3.x, use integration package v1.0.0 instead.

Installing Turn Based Strategy Framework

  1. If you haven't already, purchase the Turn Based Strategy Framework from the Unity Asset Store.
  2. Import it into your Unity project following the Asset Store's instructions.

Installing Flexible Grid Controller

  1. If you haven't already, purchase the Flexible Grid Controller from the Unity Asset Store.
  2. Import it into your Unity project following the Asset Store's instructions.

Installing the package

  1. Download the latest package from the 'Releases' tab
  2. Import the package into your project

Package Structure

The package content is organized as follows:

├── FGC_TBSF_Integration
│   ├── Resources
│   ├── Scenes
│   └── Scripts

The Scenes folder includes a playable demo, showcasing a level from the Turn Based Strategy Framework with gamepad and keyboard controls integrated with the help of Flexible Grid Controller. The Scripts folder contains all custom scripts that the package requires. Finally, the Resources folder holds some sprites used in the UI.

⚠️ Important: The demo scene uses Unity’s New Input System. Make sure the Input System Package is installed and enabled in your project settings, otherwise the demo will not function correctly.

Integration Guide

The baseline that gets keyboard controls integrated into it is the Turn Based Strategy Framework Example1 demo scene. The scene consists of a hexagonal grid and some units for the player to control. Please check out the Flexible Grid Controller docs first to understand the integration process better, as I only explain the choices specific to this project here.

The key decision in integrating Flexible Grid Controller into any project is selecting the right Interactable interface to implement. Two factors come into play here, the grid layout and the actions that should be enabled on the tiles. In case of the Example1 demo scene, the grid is two dimensional and the tiles support selection and clicking. The IBasicInteractable2D interface fits these requirements.

The interface is implemented as a separate component that will be added to the tile prefab. The implemented methods, OnClick, OnSelected and OnDeselected simply forward the actions to methods already existing on the underlying Cell. The GetGridIndex references the Cell's OffsetCoords field to determine its position within the grid.

public class CellInteractable : MonoBehaviour, IBasicInteractable2D
{
    private Cell _cellReference;

    public UnityEvent<IBasicInteractable2D, BasicControllable2D> Selected { get; set; } = new UnityEvent<IBasicInteractable2D, BasicControllable2D>();
    public UnityEvent<IBasicInteractable2D, BasicControllable2D> Deselected { get; set; } = new UnityEvent<IBasicInteractable2D, BasicControllable2D>();
    public UnityEvent<IBasicInteractable2D, BasicControllable2D> Clicked { get; set; } = new UnityEvent<IBasicInteractable2D, BasicControllable2D>();

    private void Awake()
    {
        _cellReference = GetComponent<Cell>(); 
    }

    public GridIndex2D GetGridIndex(BasicControllable2D controllable)
    {
        // Grid Index is given by referencing underlying cell's Offset Coords
        return new GridIndex2D((int)_cellReference.OffsetCoord.x, (int)_cellReference.OffsetCoord.y);
    }

    public void OnClicked(BasicControllable2D controllable)
    {
        if(_cellReference.CurrentUnits.Count > 0) 
        {
            _cellReference.CurrentUnits[0].OnMouseDown();
        }
        else
        {
            _cellReference.OnMouseDown();
        }
    }

    public void OnDeselected(BasicControllable2D controllable)
    {
        _cellReference.OnMouseExit();
        if (_cellReference.CurrentUnits.Count > 0)
        {
            _cellReference.CurrentUnits[0].OnMouseExit();
        }
    }

    public void OnSelected(BasicControllable2D controllable)
    {
        _cellReference.OnMouseEnter();
        if (_cellReference.CurrentUnits.Count > 0)
        {
            _cellReference.CurrentUnits[0].OnMouseEnter();
        }
    }
}

The choice of the IBasicInteractable2D interface determines subsequent choices: GridControllable2D and subsequently the BasicGridController2D. The BasicInputProvider2D complements the setup for input handling. Scene configuration of these components finalizes the integration process.

Contact and Support

If you have any questions, feedback, or need assistance with the Flexible Grid Controller, feel free to reach out. You can contact me directly via email at [email protected] for specific queries or suggestions. Additionally, for broader community support and discussions, join the Flexible Grid Controller Discord server.

About

Showcases the integration between the Turn Based Strategy Framework and the Flexible Grid Controller

Resources

Stars

Watchers

Forks

Packages

No packages published