ygdra/tools/cell.gd

99 lines
2.8 KiB
GDScript

class_name Cell
extends Reference
# Abstract cell of labyrinth
# Type of floor
enum Floor { UNKNOWN, PATH, WALL }
# Edge kind
enum Edge { WALL, NONE, DOOR }
# Feature kind
enum Feature { NONE, EVENT, ENTRANCE, EXIT, CHEST, MINE_POINT, TAKE_POINT, CHOP_POINT, HARVEST_POINT }
###
# A cell has :
# - a kind of floor indicating if it is a path, a wall, ...
var floor_kind = Floor.UNKNOWN
# - a list of edges indicating to which adjacent cell it is connected
var edges = [Edge.WALL, Edge.WALL, Edge.WALL, Edge.WALL]
# - an optional feature : entrance, exit, ...
var feature = Feature.NONE
# - a flag indicating if the cell was visited by the player
var visited = false
###
# Edge-related functions
# Set an edge
func set_edge(dir, val):
edges[dir] = val
# Get an edge
func get_edge(dir):
return edges[dir]
# Deadend
func is_deadend():
# if there are 3 walls among the edges, the cell is a deadend
var count = 0
for e in edges:
if e == Edge.WALL:
count += 1
return count == 3
# Utilities for labyrinth-building (transformations)
# Identity : do nothing
func identity():
pass
# Horizontal flip : exchange the EAST/WEST edges
func horizontal_flip():
var tmp = edges[Direction.WEST]
edges[Direction.WEST] = edges[Direction.EAST]
edges[Direction.EAST] = tmp
# Vertical flip : exchange the NORTH/SOUTH edges
func vertical_flip():
var tmp = edges[Direction.SOUTH]
edges[Direction.SOUTH] = edges[Direction.NORTH]
edges[Direction.NORTH] = tmp
# Rotation 180° = horizontal flip + vertical flip
func rotate_180():
self.horizontal_flip()
self.vertical_flip()
# Rotation 90° in trigonometric direction (counter-clockwise)
func rotate_90():
var tmp = edges[Direction.NORTH]
edges[Direction.NORTH] = edges[Direction.EAST]
edges[Direction.EAST] = edges[Direction.SOUTH]
edges[Direction.SOUTH] = edges[Direction.WEST]
edges[Direction.WEST] = tmp
# Rotation 270° in trigonometric direction (counter-clockwise), or 90° clockwise
func rotate_270():
var tmp = edges[Direction.NORTH]
edges[Direction.NORTH] = edges[Direction.WEST]
edges[Direction.WEST] = edges[Direction.SOUTH]
edges[Direction.SOUTH] = edges[Direction.EAST]
edges[Direction.EAST] = tmp
# Transposition (mirror -45°) : exchange NORTH/WEST, SOUTH/EAST
func transpose():
var tmp = edges[Direction.NORTH]
edges[Direction.NORTH] = edges[Direction.WEST]
edges[Direction.WEST] = tmp
tmp = edges[Direction.SOUTH]
edges[Direction.SOUTH] = edges[Direction.EAST]
edges[Direction.EAST] = tmp
# Anti-transposition (mirror 45°): exchange NORTH/EAST, SOUTH/WEST
func antitranspose():
var tmp = edges[Direction.NORTH]
edges[Direction.NORTH] = edges[Direction.EAST]
edges[Direction.EAST] = tmp
tmp = edges[Direction.SOUTH]
edges[Direction.SOUTH] = edges[Direction.WEST]
edges[Direction.WEST] = tmp