74 lines
2.3 KiB
GDScript
74 lines
2.3 KiB
GDScript
extends Area2D
|
|
class_name Fruit
|
|
|
|
const TILE_SIZE = 40
|
|
|
|
var all_positions : Array[Vector2] = []
|
|
|
|
const possible_growth_amounts = [1,1,1,3,3,5,10]
|
|
|
|
const growth_sprite_map : Dictionary[int, CompressedTexture2D] = {1: preload("res://Sprites/cherry.png"),
|
|
3: preload("res://Sprites/lemon.png"),
|
|
5: preload("res://Sprites/apple.png"),
|
|
10: preload("res://Sprites/watermelon.png")}
|
|
|
|
var growth_amount : int = 1
|
|
|
|
@onready
|
|
var label : Label = $Label
|
|
@onready
|
|
var sprite : Sprite2D = $Sprite2D
|
|
|
|
func _ready() -> void:
|
|
|
|
# Determine the level limits
|
|
var level : Area2D = get_tree().get_first_node_in_group("Level")
|
|
var x_bounds : Vector2 = level.position.x * Vector2.ONE + Vector2(-TILE_SIZE, TILE_SIZE) * level.scale.x / 2
|
|
var y_bounds : Vector2 = level.position.y * Vector2.ONE + Vector2(-TILE_SIZE, TILE_SIZE) * level.scale.y / 2
|
|
|
|
# Determine the possible x and y coordinates
|
|
var possible_x : Array = range(x_bounds.x, x_bounds.y + 20, TILE_SIZE)
|
|
var possible_y : Array = range(y_bounds.x, y_bounds.y + 20, TILE_SIZE)
|
|
|
|
# Add each combination of x and y to the list of positions
|
|
for x in possible_x:
|
|
for y in possible_y:
|
|
all_positions.append(Vector2(x, y))
|
|
|
|
respawn()
|
|
|
|
func _process(_delta: float) -> void:
|
|
if !GridManager.current_allowed_spawns.is_empty() and not position in GridManager.current_allowed_spawns:
|
|
respawn()
|
|
|
|
var obstacles : Array[Node] = get_tree().get_nodes_in_group("BlocksSpawn")
|
|
|
|
for obstacle in obstacles:
|
|
if obstacle is Area2D and not obstacle == self:
|
|
if position == obstacle.position:
|
|
respawn()
|
|
|
|
func respawn() -> void:
|
|
var possible_positions : Array[Vector2]
|
|
if GridManager.current_allowed_spawns.is_empty():
|
|
possible_positions = all_positions.duplicate()
|
|
else:
|
|
possible_positions = GridManager.current_allowed_spawns.duplicate()
|
|
|
|
var obstacles : Array[Node] = get_tree().get_nodes_in_group("BlocksSpawn")
|
|
|
|
for obstacle in obstacles:
|
|
if obstacle is Area2D:
|
|
possible_positions.erase(obstacle.position)
|
|
|
|
if possible_positions.is_empty():
|
|
queue_free()
|
|
return
|
|
|
|
var chosen_position = possible_positions.pick_random()
|
|
|
|
position = chosen_position
|
|
growth_amount = possible_growth_amounts.pick_random()
|
|
label.text = str(growth_amount)
|
|
sprite.texture = growth_sprite_map[growth_amount]
|