40 lines
1.4 KiB
GDScript
40 lines
1.4 KiB
GDScript
extends Node2D
|
|
|
|
var apple_path : String = "res://prefabs/apple.tscn"
|
|
@export var map_size : Vector2 = Vector2(10,10)
|
|
@export var tile_size : Vector2 = Vector2(320,320)
|
|
@export var top_left : Vector2
|
|
@export var test_area : Area2D
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready() -> void:
|
|
Router.eat_apple.connect(spawn_apple)
|
|
pass # Replace with function body.
|
|
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta: float) -> void:
|
|
#spawn_apple()
|
|
pass
|
|
|
|
func spawn_apple():
|
|
var new_apple : Area2D = load(apple_path).instantiate()
|
|
var spot_found : bool = false
|
|
var rand_pos : Vector2
|
|
rand_pos = Vector2(randi_range(0,map_size.x-1),randi_range(0,map_size.y-1))
|
|
## apple spawning collsion detection
|
|
#while (!spot_found):
|
|
#rand_pos = Vector2(randi_range(0,map_size.x-1),randi_range(0,map_size.y-1))
|
|
#test_area.global_position = Vector2(top_left.x + rand_pos.x * tile_size.x,top_left.y + rand_pos.y * tile_size.y)
|
|
#if test_area.has_overlapping_areas():
|
|
#print("possible collision")
|
|
#for i in test_area.get_overlapping_areas():
|
|
#if (i.is_in_group("apple") or i.is_in_group("wall")):
|
|
#spot_found = false
|
|
#else:
|
|
#spot_found = true
|
|
#else:
|
|
#spot_found = true
|
|
new_apple.global_position = Vector2(top_left.x + rand_pos.x * tile_size.x,top_left.y + rand_pos.y * tile_size.y)
|
|
add_child(new_apple)
|
|
pass
|