Skip to content

Instantly share code, notes, and snippets.

@igrir
Created May 28, 2023 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igrir/7dc9eef342e5e40a75fa339a46f11a2f to your computer and use it in GitHub Desktop.
Save igrir/7dc9eef342e5e40a75fa339a46f11a2f to your computer and use it in GitHub Desktop.
extends Node2D
class_name ImageSplicer
@export var sourceImage:AtlasTexture
@onready var piece:JigPiece = get_node("%piece")
@onready var container:Node2D = get_node("container")
var splices = Vector2(9,8)
var pieceUnit:float = 64.0
var overlappingUnit:float = pieceUnit * 2.0 / 5.0
var pieceList:Array[JigPiece]
# Called when the node enters the scene tree for the first time.
func _ready():
splice_puzzle()
set_puzzle_sides()
func splice_puzzle():
#duplicated sprite
var source1:AtlasTexture = sourceImage.duplicate()
source1.region = Rect2(0,0,400,400);
var overlappingRatio:float = (overlappingUnit / pieceUnit)
var normalWidth = pieceUnit * splices.x
var normalHeight = pieceUnit * splices.y
var edgeXWidth = (overlappingUnit)
var edgeYWidth = (overlappingUnit)
var totWidth = (pieceUnit * splices.x) - (overlappingUnit * (splices.x-1)) - edgeXWidth
var totHeight = (pieceUnit * splices.y) - (overlappingUnit * (splices.y-1)) - edgeYWidth
var scaleXRatio = totWidth / normalWidth
var scaleYRatio = totHeight / normalHeight
for i in splices.x:
for j in splices.y:
var newPiece:JigPiece = piece.duplicate()
pieceList.append(newPiece)
container.add_child(newPiece)
newPiece.scale = Vector2.ONE;
newPiece.position = Vector2((i * pieceUnit)- (i * overlappingUnit) , (j * pieceUnit)- (j * overlappingUnit))
newPiece.visible = true
newPiece.duplicate_material()
newPiece.set_mat_tile(source1)
newPiece.set_mat_scale(1/splices.x/ scaleXRatio, 1/splices.y / scaleYRatio)
var edgeX = (overlappingRatio/splices.x / scaleXRatio/2)
var edgeY = (overlappingRatio/splices.y / scaleXRatio/2)
var offsetY = (j / splices.y/ scaleYRatio) - (j * overlappingRatio / splices.y / scaleYRatio) - edgeY
var offsetX = (i / splices.x / scaleXRatio) - (i * overlappingRatio / splices.x / scaleXRatio) - edgeX
newPiece.set_mat_offset(offsetX, offsetY)
func set_puzzle_sides():
var randomSide = RandomNumberGenerator.new()
for i in splices.x:
for j in splices.y:
var index = (i * splices.y) + j
print(index)
var piece = pieceList[index]
var topType:JigPiece.SideType = JigPiece.SideType.OUT;
var rightType:JigPiece.SideType = JigPiece.SideType.OUT;
var bottomType:JigPiece.SideType = JigPiece.SideType.OUT;
var leftType:JigPiece.SideType = JigPiece.SideType.OUT;
# top
if (j == 0):
topType = JigPiece.SideType.PLAIN
else:
var neighborPiece = pieceList[(i * splices.y) - 1 + j]
topType = JigPiece.SideType.OUT if (neighborPiece.bottomSideType == JigPiece.SideType.IN) else JigPiece.SideType.IN
# right
if (i == splices.x - 1):
rightType = JigPiece.SideType.PLAIN
else:
rightType = JigPiece.SideType.OUT if randomSide.randi_range(0,1) == 0 else JigPiece.SideType.IN
# bottom
if (j == splices.y - 1):
bottomType = JigPiece.SideType.PLAIN
else:
bottomType = JigPiece.SideType.OUT if randomSide.randi_range(0,1) == 0 else JigPiece.SideType.IN
# left
if (i == 0):
leftType = JigPiece.SideType.PLAIN
else:
var neighborPiece = pieceList[( (i-1) * splices.y) + j]
leftType = JigPiece.SideType.OUT if (neighborPiece.rightSideType == JigPiece.SideType.IN) else JigPiece.SideType.IN
piece.set_side(topType, rightType, bottomType, leftType)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment