43 lines
1.2 KiB
GDScript
43 lines
1.2 KiB
GDScript
extends Area2D
|
|
class_name Fruit
|
|
|
|
const TILE_SIZE = 20
|
|
|
|
var all_positions : Array[Vector2] = []
|
|
|
|
const possible_growth_amounts = [1,1,1,3,3,5,10]
|
|
|
|
var growth_amount : int = 1
|
|
|
|
@onready
|
|
var label : Label = $Label
|
|
|
|
func _ready() -> void:
|
|
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
|
|
|
|
var possible_x : Array = range(x_bounds.x, x_bounds.y, TILE_SIZE)
|
|
var possible_y : Array = range(y_bounds.x, y_bounds.y, TILE_SIZE)
|
|
|
|
for x in possible_x:
|
|
for y in possible_y:
|
|
all_positions.append(Vector2(x, y))
|
|
|
|
respawn()
|
|
|
|
func respawn() -> void:
|
|
var possible_positions = all_positions.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)
|
|
|
|
var chosen_position = possible_positions.pick_random()
|
|
|
|
position = chosen_position
|
|
growth_amount = possible_growth_amounts.pick_random()
|
|
label.text = str(growth_amount)
|