DoctorWhoFR / gotiled

Simple Golang 2D library aimed to be used with tiled map representation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

generator_2d

import "generator_2d"

Package generator_2d

2D Tile Renderer

This is a simple library used to generated and manage tile set map and render them in PNG With this library, you can use X, Y position based on the tile grid

Index

Constants

const (
    CheckCanBePosedAlreadyHere            = "ALREADY_ENT_HERE_NO_HOVER_BY"
    CheckCanBePosedAlreadyHereBadHovered  = "BAD_HOVERED_ENTITY"
    CheckCanBePosedAlreadyHereAlreadyUsed = "HOVERED_ENTITY_ALREADY_USED"
    CheckCanBePosedNeedHover              = "NO_ENTITY_TO_PUT"
)

func LoadImage

func LoadImage(path string) ([]byte, image.Image)

LoadImage (path string) ([]byte, image.Image)

Currently, helper function, used to simply load an image based on the current+folder + provided path. --

Params:

  • path string -> the path to your image like ("\\assets\\image.png"), you should always use "\\" instead of "/"

Response:

  • Return two thing , an array of bytes representation of the got image, and also an image.Image linked to the image path provider.

// TODO LoadImage error handling This function need error handling

type Map

Map

Representation of a final map with entities, base tile map, and other functionality.

A map is based on

  • a TileMap (base image for the 2D scene)
  • Lists of Sprite (representation of an image in the 2D scene)
  • Message for in-build notification system
  • Debug is a boolean variable used to display the debugging mod
type Map struct {
    TileMap                   // Representation of an TileMap currently the scene rendered to the user
    Sprites map[string]Sprite // Sprites representations representation of an image in the 2D scene
    Message string            // For in-build image notification system
    Debug   bool
}

func CreateDemoFarmLand

func CreateDemoFarmLand() Map

CreateDemoFarmLand

  • Base : base.png
  • GridSize : 16px
  • Theme : basic

Create a basic FarmLand and returning a working Map.

TODO: Do we need to put this on bot part ? or keep it on 2D lib ?

func (*Map) AddUpdateEnt

func (m *Map) AddUpdateEnt(uuid string, ent Sprite) error

AddUpdateEnt Add an entity to a final Map, add it into a Sprite lists When you are going to use the RenderScene function, all Sprites added wil be generated and added to the scene according to there X,Y position.

Always use AddUpdateEnt to add or update entity, even more if you change the X,Y position. Every conditional positional check is made in this function and in no other step.

If you don't use it, you will maybe have some clapping entity, if not managed well on your side.

func (*Map) CheckCanBePosed

func (m *Map) CheckCanBePosed(uuid string, ent Sprite) error

func (*Map) DeleteEnt

func (m *Map) DeleteEnt(uuid string) error

func (*Map) DrawImage

func (m *Map) DrawImage(entity Sprite, ctx *gg.Context)

DrawImage In build option to draw sprite into final img scene

Take a Sprite in entry and a *gg.Context

func (*Map) GridToPixel

func (m *Map) GridToPixel(x, y int) (int, int)

GridToPixel Simple, in-build position, to transform X,Y cells based position, into there pixel equivalent.

  • x : X grid position
  • x : Y grid position

func (*Map) RenderScene

func (m *Map) RenderScene() string

RenderScene

Main function used to get the final Image png representation of a user Map.

When you are using RenderScene every Sprite's that is present on the Map will be generated.

Linked to their equivalent Sprite.Level if having one.

Or displayed as their base image Sprite.Image if having no level.

You will get a string path, where the generated file belong.

/!\ *The file will NOT BE automatically delete, so you need to handle this part.* /!\

func (*Map) SendNotification

func (m *Map) SendNotification(message string)

SendNotification // TODO Simple, in-build image notification system

type Sprite

Sprite

Representation of an image in a TileMap 2D Scene.

A sprite, is not linked to a specific TileMap scene.

A sprite is based on grid X and Y position.

So you don't need to do any pixels conversions at this point.

A sprite can have multiples images representations, every image after the base one is linked to a Levels.

A basic leveled sprite look like :

basePath := "\\assets\\farms\\pumpinks\\"

base, _ := generator_2d.LoadImage(basePath + "\\pum_2.png")

_sprite := generator_2d.Sprite{
	Image:      base,
	Levels:     make(map[int]generator_2d.Sprite, 0),
	X:          x,
	Y:          y,
	WidthCell:  1,
	HeightCell: 1,
	Level:      level,
}

for i := 1; i < maxLevel+1; i++ {
	base, _ := generator_2d.LoadImage(basePath + fmt.Sprintf("\\pum_%d.png", i))

	_sprite.Levels[i] = generator_2d.Sprite{
		Image:      base,
		Levels:     nil,
		X:          x,
		Y:          y,
		WidthCell:  1,
		HeightCell: 1,
		Level:      0,
	}
}
type Sprite struct {
    Image        []byte
    Levels       map[int]Sprite
    CanBeHoverBy map[string]bool // things that can be on top of this entity
    NeedHoverBy  map[string]bool // things where that entity can be on top off
    Hovered      bool
    X            int
    Y            int
    WidthCell    int
    HeightCell   int
    Level        int
    Type         int
    UniqueID     string
    ZIndex       int
}

type TileMap

TileMap

A TileMap is the entry point of that 2D library.

A TileMap is build with an []byte array of an BaseImage. A GridSize like 16 (=16x per grid cell)

Every other field are DiscordInternal one, and will be automatically populated.

type TileMap struct {
    BaseImage []byte `json:"baseImage,omitempty"`
    GridSize  int

    RGBA      *image.RGBA // read-only
    MaxHeight int         // read-only
    MaxWidth  int         // read-only
    Height    int         // read-only
    Width     int         // read-only
}

Generated by gomarkdoc

About

Simple Golang 2D library aimed to be used with tiled map representation


Languages

Language:Go 100.0%