API Docs for: 0.1.1
Show:

flatworld.Flatworld Class

Main class for the engine

Initializes the whole structure and plugins and is used as primary API for all operations. This class is e.g. passed to every plugin that get initialized with their init-method.

You use the class by instantiating it (new) and then finishing initialization with init-method. Please see examples below.

The biggest part of creating the map, is the data structure. There is a clear data structure that you can see from the tests/data-folder, but the factory is responsible for creating the objects, so you can use your own factory implementation. So to understand more, please see e.g. flatworld.factories.hexaFactory.

The map consists of layer on top of each other. The example is best understood when thinking typical war strategy game. The structure is this:

  1. StaticLayer: Handles things like scaling / zooming the map
  2. MovableLayer: Obviously handles movement of the map. Also is a good place to get map coordinates. Since getting global coordinates won't help you much, half of the time.
  3. Different layers: like units, terrain, fog of war, UIs etc. Can also contains special layers like dynamically changed UIlayers.
  4. possible subcontainers (used for optimized object selection and map movement). Can also contains special layers like dynamically changed UIlayers.
  5. Individual objects, like units, terrains, cities etc...

Plugins can be added with activatePlugins-method by sending them to the class. Plugins must always implement init-method, which receives Map instance. Plugins are not yet restricted what they can do and can add functionality without touching map or can modify objects or their prototypes through access to Map instance.

Constructor

flatworld.Flatworld

(
  • mapCanvas
  • [props]
  • [trackFPSCB]
)
Object

Parameters:

  • mapCanvas HTMLElement

    HTML element which will be container for the created canvas element.

  • [props] Object optional

    Extra properties

    • bounds Object

      Bounds of the viewport

      • width Integer
        Bound width
      • height Integer
        Bound height
    • [mapSize] Object optional

      The total mapSize

      • x Integer
        x-axis
      • y Integer
        y-axis
    • rendererOptions Object

      Renderer options passed to PIXI.autoDetectRenderer

    • subcontainers Object

      Subcontainers size in pixels. If given, will activate subcontainers. If not given or false, subcontainers are not used.

      • width Integer
        Subcontainer width
      • height Integer
        Subcontainer height
  • [trackFPSCB] FPSCallback optional

    Callback function for tracking FPS in renderer. So this is used for debugging and optimizing.

Returns:

Object:

New Map instance

Example:

var map = new Map(divContainer, mapOptions );
promises = map.init( gameData.pluginsToActivate, mapData.startPoint );

A note on the UI part of the map. The UI is not the primary UI interface for the map, but instead it is the UI that is used when interacting with the map and objects in it. So e.g. when user selects a unit on the map. How that unit is highlighted as selected and what kind of possible info-box we show to the user regarding that object, movement of units etc.

Methods

_defaultTick

() private

This handles the default drawing of the map, so that map always updates when drawOnNextTick === true. This tick callback is always set and should not be removed or overruled

_getLayersWithAttributes

(
  • attribute
  • value
)
private

This returns layers by filtering them based on certain attribute. Can be used with more higher order filtering

Parameters:

Returns:

the current map instance

_getSubcontainersUnderPoint

(
  • globalCoords
  • options
)
Array private

Get subcontainers under certain point or rectangle

Parameters:

  • globalCoords type
  • options Object

    Optional options.

Returns:

Array:

All subcontainers that matched the critea

_retrieveObjects

(
  • allCoords
  • [{}.type]
  • [{}.subcontainers]
)
Array private

Retrieves the objects from ObjectManager, with the given parameters. Mostly helper functionality for getObjectsUnderArea

Parameters:

  • allCoords Object

    REQUIRED

    • globalCoords Object

      REQUIRED

      • x Integer
        REQUIRED
      • y Integer
        REQUIRED
      • width Integer
        REQUIRED
      • height Integer
        REQUIRED
    • localCoords Object

      REQUIRED

      • x Integer
        REQUIRED
      • y Integer
        REQUIRED
  • [{}.type] String optional

    The type of objects we want

  • [{}.subcontainers] Array optional

    Array of the subcontainers we will search

Returns:

Array:

Found objects

activateFogOfWar

()

Plugin will overwrite create this method. Method for actually activating fog of war.

activatePlugin

(
  • plugin
)

Activate plugin for the map. Plugins need .pluginName property and .init-method. Plugins init-method activates the plugins and we call them in Map. Plugins init-metho receivse this (Map instance) as their only parameter.

Parameters:

Throws:

Error:

Throws a general error if there is an issue activating the plugin

addLayer

() MapLayer

All parameters are passed to ParentLayerConstructor (normally constructor of MapLayer).

Returns:

MapLayer:

created MapLayer instance

addUIObject

(
  • layer
  • object
)

Add an UI object to the wanted layer.

Parameters:

createSpecialLayer

(
  • name
  • options
)
MapLayer

Create a special layer, that can holds e.g. UI effects in it.

Parameters:

  • name String

    name of the layer

  • options Object

    Optional options.

    • coord Object

      Coordinates of the layer

      • x Integer
        X coordinate
      • y Integer
        Y coordinate
    • toLayer Object

      To which layer will this layer be added to as UILayer. Default false

Returns:

MapLayer:

The created UI layer

drawOnNextTick

()

The correct way to update / redraw the map. Check happens at every tick and thus in every frame.

drawOnNextTick

()

The correct way to update / redraw the map. Check happens at every tick and thus in every frame.

getAllObjects

(
  • [{}.filters]
)
Array

Get all objects on the map, from layers and subcontainers.

Parameters:

  • [{}.filters] MapDataManipulator optional

    The mapDataManipulator instance, that you use for filtering.

Returns:

Array:

Array of found objects

getMinimapLayer

()

Return minimap layer. Holds minimap, if used in the game.

getMovableLayer

() MapLayer | PIXI.Container | PIXI.ParticleContainer

Returns movable layer. This layer is the one that moves when the player moves the map. So this is used for things that are relative to the current map position the player is seeing. This can be used e.g. when you want to display some objects on the map or UI elements, like effects that happen on certain point on the map.

Returns:

MapLayer | PIXI.Container | PIXI.ParticleContainer:

getObjectsUnderArea

(
  • globalCoords
  • options
)
Array

Gets object under specific map coordinates. Using subcontainers if they exist, other methods if not. If you provide type parameter, the method returns only object types that match it.

NOTE! At the moment filters only support layers! You can not give filters object: object and expect them to be filtered. It will filter only layers (object: layer)!

Parameters:

  • globalCoords Object

    Event coordinates on the staticLayer / canvas.

    • x Integer

      X coordinate

    • y Integer

      Y coordinate

  • options Object

    Optional options

    • filter Object

      The filter to apply to subcontainers

Returns:

Array:

Array of object found on the map.

getPrimaryLayers

(
  • [{}.filters]
)
Object

This returns the normal parent layers that we mostly use for manipulation everything. MovableLayer and staticLayer are built-in layers designed to provide the basic functionalities like zooming and moving the map. These layers provide everything that extends the map more.

Parameters:

  • [{}.filters] MapDataManipulator optional

    The mapDataManipulator instance, that you use for filtering.

Returns:

Object:

Basically anything in the map that is used as a layer (not really counting subcontainers).

getRenderer

() PIXI.Renderer

Returns the PIXI renderer. Don't use this unless you must. For more advanced or PIXI specific cases.

Returns:

PIXI.Renderer:

getStaticLayer

()

Return static layer. The static layer is the topmost of all layers. It handles zooming and other non-movable operations.

getSubcontainerConfigs

() Object

Returns current subcontainers configurations (like subcontainers size).

Returns:

getViewportArea

(
  • isLocal
)
X: Integer, y: Integer, width: Integer, height: Integer

Get the size of the area that is shown to the player. More or less the area of the browser window.

Parameters:

  • isLocal Boolean

    Do we want to use Map coordinates or global / canvas coordinates. Default = false

Returns:

X: Integer, y: Integer, width: Integer, height: Integer:

} x- and y-coordinates and the width and height of the viewport

getZoom

() MapLayer | PIXI.Container | PIXI.ParticleContainer

Get map zoom. 1 = no zoom. <1 zoom out, >1 zoom in.

Returns:

MapLayer | PIXI.Container | PIXI.ParticleContainer:

getZoomLayer

() MapLayer | PIXI.Container | PIXI.ParticleContainer

This returns the layer that is responsible for map zoom

Returns:

MapLayer | PIXI.Container | PIXI.ParticleContainer:

init

(
  • plugins
  • coord
  • tickCB
  • options
)
Array

This initializes the map and makes everything appear on the map and actually work. Also initializes the given plugins since normally the plugins have to be activated before the map is shown.

Parameters:

  • plugins String[] | Object[]

    Plugins to be activated for the map. Normally you should give the plugins here instead of separately passing them to activatePlugins method. You can provide the module strings or module objects.

  • coord Object

    Starting coordinates for the map.

    • x Integer

      X coordinate.

    • y Integer

      Y coordinate.

  • tickCB Function

    callback function for tick. Tick callback is initiated in every frame. So map draws happen during ticks.

  • options Object

    Extra options.

    • fullsize Boolean

      Do we set fullsize canvas or not at the beginning. Default: true

Returns:

Array:

Returns an array of Promises. If this is empty / zero. Then there is nothing to wait for, if it contains promises, you have to wait for them to finish for the plugins to work and map be ready.

initMinimap

()

Plugin will overwrite create this method. Method for actually activating minimap.

moveMap

(
  • coord
  • absolute
)

Moves the map the amount of given x and y pixels. Note that this is not the destination coordinate, but the amount of movement that the map should move. Internally it moves the movableLayer, taking into account necessary properties (like scale). Draws map after movement.

Parameters:

  • coord Object

    The amount of x and y coordinates we want the map to move. I.e. { x: 5, y: 0 }. With this we want the map to move horizontally 5 pixels and vertically stay at the same position.

    • x Integer

      X coordinate

    • y Integer

      Y coordinate

  • absolute Integer

    If the given coordinates are not relative, like move map 1 pixel, but instead absolute, like move map to coordinates { x: 1, y: 2 }. Defaults to false (relative).

pluginsArray

(
  • pluginsArray
)
Promise

Activate all plugins for the map. Iterates through the given plugins we wish to activate and does the actual work in activatePlugin- method.

Parameters:

  • pluginsArray Object[]

    Array that consists the plugin modules to be activated

Returns:

Promise:

Promise. If string are provided resolved those with System.import, otherwise resolves immediately.

removeLayer

(
  • layer
)

Remove a primary layer from the map

Parameters:

  • layer MapLayer | PIXI.Container | PIXI.ParticleContainer

    The layer object to be removed

removeUIObject

(
  • layer
  • objectName
)

Remove an UI object to the wanted layer.

Parameters:

  • layer Integer

    Type of the layer. layerTypes.STATIC of layerTypes.MOVABLE.

  • objectName String

    The object to be attached as UI object.

setPrototype

(
  • property
  • value
)

Setting new prototype methods for the Map instance

Parameters:

  • property String

    The property you want to set

  • value

    Value for the property

setZoom

(
  • scale
)
Number

Set map zoom. 1 = no zoom. <1 zoom out, >1 zoom in.

Parameters:

  • scale Number

    The amount of zoom you want to set

Returns:

Number:

The amount of zoom applied

toggleFullScreen

()

Toggles fullscreen mode. Defined by the baseEventlisteners-module (core modules plugin)

toggleFullsize

()

Resize the canvas to fill the whole browser content area. Defined by the baseEventlisteners-module (core modules plugin)

usesSubcontainers

() Boolean

Just a convenience function (for usability and readability), for checking if the map uses subcontainers.

Returns:

whenReady

() Promise

Returns a promise that resolves after the map is fully initialized

Returns:

Promise:

Promise that holds all the individual plugin loading promises

zoomIn

()

This is abstract method and needs to be implemented with a plugin. Core module has an implementation for this and if you don't implement your own, I suggest you use it.

zoomOut

()

This is abstract method and needs to be implemented with a plugin. Core module has an implementation for this and if you don't implement your own, I suggest you use it.

Attributes

canvas

HTMLElement required

canvas element that was generated and is being used by this new generated Map instance.

Fires event canvasChange

Fires when the value for the configuration attribute canvas is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

canvas

HTMLElement required

canvas element that was generated and is being used by this new generated Map instance.

Fires event canvasChange

Fires when the value for the configuration attribute canvas is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

currentlySelectedObjects

Array

The object or objects that are currently selected for details and actions / orders. This gets set by other modules, like plugins.

Fires event currentlySelectedObjectsChange

Fires when the value for the configuration attribute currentlySelectedObjects is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

isTouch

Boolean

Set variable showing if the device supports touch or not.

Fires event isTouchChange

Fires when the value for the configuration attribute isTouch is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

layerTypes

Object

Layer types. Can be extended, but the already defined types are supposed to be constants and not to be changed.

Fires event layerTypesChange

Fires when the value for the configuration attribute layerTypes is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

mapSize

X: Number, y: Number optional

Fires event mapSizeChange

Fires when the value for the configuration attribute mapSize is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

objectManager

ObjectManager

ObjectManager instance. Responsible for retrieving the objects from the map, on desired occasions. Like when the player clicks the map to select some object. This uses subcontainers when present.

Fires event objectManagerChange

Fires when the value for the configuration attribute objectManager is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

plugins

Set

list of plugins that the map uses and are initialized

Fires event pluginsChange

Fires when the value for the configuration attribute plugins is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

subcontainersConfig

Width: Integer, height: Int, maxDetectionOffset: Int

Subcontainers size that we want to generate, when layers use subcontainers.

Fires event subcontainersConfigChange

Fires when the value for the configuration attribute subcontainersConfig is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

trackFPSCB

Function

Callback function that gets the current FPS on the map and shows it in DOM

Fires event trackFPSCBChange

Fires when the value for the configuration attribute trackFPSCB is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.

VERSION

SEMVER http://semver.org/

Self explanatory

Fires event VERSIONChange

Fires when the value for the configuration attribute VERSION is changed. You can listen for the event using the on method if you wish to be notified before the attribute's value has changed, or using the after method if you wish to be notified after the attribute's value has changed.

Parameters:

  • e EventFacade
    An Event Facade object with the following attribute-specific properties added:
    • prevVal Any
      The value of the attribute, prior to it being set.
    • newVal Any
      The value the attribute is to be set to.
    • attrName String
      The name of the attribute being set.
    • subAttrName String
      If setting a property within the attribute's value, the name of the sub-attribute property being set.