1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-19 01:29:49 +00:00

Add documentation for LayerTileMap and TileSetCustom, along with a class description for CelTileMap

This commit is contained in:
Emmanouil Papadeas 2024-11-27 15:10:52 +02:00
parent a10a0a9bda
commit 6be273d098
3 changed files with 65 additions and 2 deletions

View file

@ -1,6 +1,15 @@
class_name CelTileMap
extends PixelCel
## A cel type for 2D tile-based maps.
## A Tilemap cel uses a [TileSetCustom], which it inherits from its [LayerTileMap].
## Extending from [PixelCel], it contains an internal [Image], which is divided in
## grid cells, the size of which comes from [member TileSetCustom.tile_size].
## Each cell contains an index, which is an integer used to map that portion of the
## internal [member PixelCel.image] to a tile in [member tileset], as well as
## information that specifies if that cell has a transformation applied to it,
## such as horizontal flipping, vertical flipping, or if it's transposed.
var tileset: TileSetCustom:
set(value):
if is_instance_valid(tileset):
@ -124,8 +133,8 @@ func update_tileset(undo: bool) -> void:
## [br]
## 0.5) Portion is transparent and mapped.
## Set its index to 0 and unuse the mapped tile.
## If the mapped tile is removed, educe the index of all portions that have indices greater or equal
## than the existing tile's index.
## If the mapped tile is removed, reduce the index of all portions that have
## indices greater or equal than the existing tile's index.
## [br]
## 1) Portion not mapped, exists in the tileset.
## Map the portion to the existing tile and increase its times_used by one.

View file

@ -1,6 +1,14 @@
class_name LayerTileMap
extends PixelLayer
## A layer type for 2D tile-based maps.
## A LayerTileMap uses a [TileSetCustom], which is then by all of its [CelTileMap]s.
## This class doesn't hold any actual tilemap data, as they are different in each cel.
## For this reason, that data is being handled by the [CelTileMap] class.
## The [TileSetCustom] that this layer uses.
## Internally, this class doesn't make much use of this.
## It's mostly only used to be passed down to the layer's [CelTileMap]s.
var tileset: TileSetCustom

View file

@ -1,18 +1,34 @@
class_name TileSetCustom
extends RefCounted
## A Tileset is a collection of tiles, used by [LayerTileMap]s and [CelTileMap]s.
## The tileset contains the [Project] that it is being used by, its [member name].
## the size of each individual tile, and the collection of [TileSetCustom.Tile]s itself.
## Not to be confused with [TileSet], which is a Godot class.
## Emitted every time the tileset changes, such as when a tile is added, removed or replaced.
## The [CelTileMap] that the changes are coming from is referenced in the [param cel] parameter.
signal updated(cel: CelTileMap)
## The [Project] the tileset is being used by.
var project: Project
## The tileset's name.
var name := ""
## The size of each individual tile.
var tile_size: Vector2i
## The collection of tiles in the form of an [Array] of type [TileSetCustom.Tile].
var tiles: Array[Tile] = []
## An internal class of [TileSetCustom], which contains data used by individual tiles of a tileset.
class Tile:
## The [Image] tile itself.
var image: Image
## The mode that was used when this tile was added to the tileset.
var mode_added: TileSetPanel.TileEditingMode
## The amount of tiles this tile is being used in tilemaps.
var times_used := 1
## The step number of undo/redo when this tile was added to the tileset.
var undo_step_added := 0
func _init(
@ -22,6 +38,12 @@ class Tile:
mode_added = _mode_added
undo_step_added = _undo_step_added
## A method that checks if the tile should be removed from the tileset.
## Returns [code]true[/code] if the current undo step is less than [member undo_step_added],
## which essentially means that the tile always gets removed if the user undos to the point
## the tile was added to the tileset.
## Otherwise, it returns [code]true[/code] if [member mode_added] is not equal to
## [enum TileSetPanel.TileEditingMode.STACK] and the amount of [member times_used] is 0.
func can_be_removed(project: Project) -> bool:
if project.undos < undo_step_added:
return true
@ -36,12 +58,18 @@ func _init(_tile_size: Vector2i, _project: Project, _name := "") -> void:
tiles.append(Tile.new(empty_image, TileSetPanel.tile_editing_mode))
## Adds a new [param image] as a tile to the tileset.
## The [param cel] parameter references the [CelTileMap] that this change is coming from,
## and the [param edit_mode] parameter contains the tile editing mode at the time of this change.
func add_tile(image: Image, cel: CelTileMap, edit_mode: TileSetPanel.TileEditingMode) -> void:
var tile := Tile.new(image, edit_mode, project.undos)
tiles.append(tile)
updated.emit(cel)
## Adds a new [param image] as a tile in a given [param position] in the tileset.
## The [param cel] parameter references the [CelTileMap] that this change is coming from,
## and the [param edit_mode] parameter contains the tile editing mode at the time of this change.
func insert_tile(
image: Image, position: int, cel: CelTileMap, edit_mode: TileSetPanel.TileEditingMode
) -> void:
@ -50,6 +78,12 @@ func insert_tile(
updated.emit(cel)
## Reduces a tile's [member TileSetCustom.Tile.times_used] by one,
## in a given [param index] in the tileset.
## If the times that tile is used reaches 0 and it can be removed,
## it is being removed from the tileset by calling [method remove_tile_at_index].
## Returns [code]true[/code] if the tile has been removed.
## The [param cel] parameter references the [CelTileMap] that this change is coming from.
func unuse_tile_at_index(index: int, cel: CelTileMap) -> bool:
tiles[index].times_used -= 1
if tiles[index].can_be_removed(project):
@ -58,16 +92,21 @@ func unuse_tile_at_index(index: int, cel: CelTileMap) -> bool:
return false
## Removes a tile in a given [param index] from the tileset.
## The [param cel] parameter references the [CelTileMap] that this change is coming from.
func remove_tile_at_index(index: int, cel: CelTileMap) -> void:
tiles.remove_at(index)
updated.emit(cel)
## Replaces a tile in a given [param index] in the tileset with a [param new_tile].
## The [param cel] parameter references the [CelTileMap] that this change is coming from.
func replace_tile_at(new_tile: Image, index: int, cel: CelTileMap) -> void:
tiles[index].image.copy_from(new_tile)
updated.emit(cel)
## Finds and returns the position of a tile [param image] inside the tileset.
func find_tile(image: Image) -> int:
for i in tiles.size():
var tile := tiles[i]
@ -76,6 +115,9 @@ func find_tile(image: Image) -> int:
return -1
## Loops through the array of tiles, and automatically removes any tile that can be removed.
## Returns [code]true[/code] if at least one tile has been removed.
## The [param cel] parameter references the [CelTileMap] that this change is coming from.
func remove_unused_tiles(cel: CelTileMap) -> bool:
var tile_removed := false
for i in range(tiles.size() - 1, 0, -1):
@ -86,10 +128,14 @@ func remove_unused_tiles(cel: CelTileMap) -> bool:
return tile_removed
## Serializes the data of this class into the form of a [Dictionary],
## which is used so the data can be stored in pxo files.
func serialize() -> Dictionary:
return {"name": name, "tile_size": tile_size, "tile_amount": tiles.size()}
## Deserializes the data of a given [member dict] [Dictionary] into class data,
## which is used so data can be loaded from pxo files.
func deserialize(dict: Dictionary) -> void:
name = dict.get("name", name)
tile_size = str_to_var("Vector2i" + dict.get("tile_size"))