Skip to content

Commit

Permalink
CandySpawner: Scale down oversize candies
Browse files Browse the repository at this point in the history
It might be that a learner will drop any old image into the Image/Candy
directory of their world.

Handle this case by scaling down any image that is larger than the
default image so that its longest axis is the same size as the default.
  • Loading branch information
wjt committed Feb 13, 2025
1 parent ffe1f3c commit b383268
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions Script/CandySpawner.gd
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ extends Node2D
var _texture_index := 0

const FALLBACK_TEXTURE : Texture2D = preload("res://Image/Candy.png")
var _MAX_SIZE = FALLBACK_TEXTURE.get_size().x

var delay := 3.0
var timer := 0.0
Expand Down Expand Up @@ -47,6 +48,18 @@ func _ready() -> void:
candy_textures.shuffle()


func _make_sprite() -> Sprite2D:
var c := Sprite2D.new()
c.texture = candy_textures[_texture_index]
var dimensions = c.texture.get_size()
if dimensions.x > _MAX_SIZE or dimensions.y > _MAX_SIZE:
c.scale *= (_MAX_SIZE / max(dimensions.x, dimensions.y))
_texture_index += 1
_texture_index %= candy_textures.size()
add_child(c)
return c


func _process(delta):
timer -= delta

Expand All @@ -64,11 +77,7 @@ func _process(delta):
if idle.size() > 0:
c = idle.pop_back()
else:
c = Sprite2D.new()
c.texture = candy_textures[_texture_index]
_texture_index += 1
_texture_index %= candy_textures.size()
add_child(c)
c = _make_sprite()
active.append(c)
c.position.y = -16
c.position.x = randi_range(0, 144)

0 comments on commit b383268

Please sign in to comment.