• Docs
  • Install
  • Follow @cosplayengine
  • v.0.9.5
  • GitHub
  • Watch
  • Examples
  1. Home
  2. Tile Mapping

Tile Mapping

  • Introduction
  • Install
  • ASCII Games
  • Demos
  • Examples
  • Macarena
  • Pong
  • Snake
  • Bird
  • Developer Guide
  • Quick Game 🏃
  • Key Concepts
  • Game Structure
  • Scenes & Objects
  • Log & Debugging
  • Pixels & Colors
  • Images
  • Keyboard Input
  • Sprite Animation
  • Shaders
  • Particle Effects
  • Fonts
  • Canvas Drawing
  • Text Input
  • Camera Tracking
  • Tile Mapping
  • Audio
  • Video
  • UI Toolkit
  • Build & Run

Tile Mapping

Tile mapping is a function that takes an image and maps this image into a sequence of scene objects. Each pixel in the map image optionally represents a scene object in the result sequence.

In CosPlay, tile mapping is provided by CPTileMapper object. Here's the code snippet example from Tile Mapping Example that demonstrates the usage of CPTileMapper object:

            val tileMapImg = new CPArrayImage(
                // # - brick
                // X - player
                // D - door
                prepSeq(
                """
                    |#        #
                    | #
                    |  #
                    |   #
                    |      D
                    |    ####
                    |####
                    |
                    |      ##
                    |    ##
                    |  ##
                    |##
                    |       X
                    |##########
                """),
                (ch, _, _) => ch&C_BLUE // Does not matter for map.
            )

            // Layout tile sprites.
            val sprs = CPTileMapper.layout(0, 0, tileMapImg, tileDim = CPDim(10, 10), (ppx, x, y) => ppx.char match
                case '#' => Option(CPImageSprite(...))
                case 'D' => Option(CPImageSprite(...))
                case 'X' => Option(CPImageSprite(...))
                case _ => None
            )
        

NOTES:

  • On line 1 we create an image map where each non-space pixel (character) will represent a sprite.
  • One line 26 we map each non-space pixel into a sprite.
    • Note that all tiles should be of the same dimension (in our example it is 10x10).
  • Once the sequence of sprites (sprs) is obtained we should add them to the scene.

Here's the screenshot of the tile mapping result from the Tile Mapping Example:

  • On This Page
  • Tile Mapping
  • Example
  • Tile Example
  • Quick Links
  • Discord
  • Stack Overflow
  • GitHub
  • @cosplayengine
  • YouTube
  • API
Copyright © 2023 Rowan Games, Inc. Privacy • Docs release: 0.9.5 Latest: