diff options
Diffstat (limited to 'src/entities/food/scripts')
-rw-r--r-- | src/entities/food/scripts/food.gd | 3 | ||||
-rw-r--r-- | src/entities/food/scripts/food_manager.gd | 43 |
2 files changed, 31 insertions, 15 deletions
diff --git a/src/entities/food/scripts/food.gd b/src/entities/food/scripts/food.gd index 1689837..e8b0261 100644 --- a/src/entities/food/scripts/food.gd +++ b/src/entities/food/scripts/food.gd @@ -11,6 +11,7 @@ var _type_texture: Dictionary = { export(Type) var TYPE onready var _sprite: Sprite = $Sprite +var location: Vector2 func _ready(): @@ -19,5 +20,5 @@ func _ready(): func _on_body_entered(body: Node) -> void: - Event.emit_signal("food_eaten", TYPE) + Event.emit_signal("food_eaten", TYPE, location) queue_free()
\ No newline at end of file diff --git a/src/entities/food/scripts/food_manager.gd b/src/entities/food/scripts/food_manager.gd index 20772db..426677c 100644 --- a/src/entities/food/scripts/food_manager.gd +++ b/src/entities/food/scripts/food_manager.gd @@ -2,38 +2,53 @@ class_name FoodManager extends Node2D export(PackedScene) var FOOD: PackedScene +export(NodePath) var WORLD_GENERATOR_NP: NodePath +onready var world_generator: Node2D = get_node(WORLD_GENERATOR_NP) +onready var possible_food_locations: Array = world_generator.get_valid_map_coords() var max_apples: int = 10 -var current_apples: int = 0 +var current_food: Array = [] func _ready(): - Event.connect("food_eaten", self, "_on_food_eaten") randomize() + Event.connect("food_eaten", self, "_on_food_eaten") func _process(delta) -> void: - if current_apples < max_apples: + if current_food.size() < max_apples: _place_new_food() - current_apples += 1 func _place_new_food() -> void: var food: Area2D = FOOD.instance() Event.emit_signal("food_placing_new_food", food.TYPE) - var position: Vector2 = _get_random_pos() - food.global_position = position + var pos_loc: Array = _get_random_pos() + var pos: Vector2 = pos_loc[0] + var loc: Vector2 = pos_loc[1] + + food.global_position = pos + food.location = loc add_child(food) - Event.emit_signal("food_placed_new_food", food.TYPE) + current_food.append(loc) + Event.emit_signal("food_placed_new_food", food.TYPE, loc) + +func _get_random_pos() -> Array: + var found_valid_loc: bool = false + var index: int + var location: Vector2 -func _get_random_pos() -> Vector2: - var screen_size: Vector2 = get_viewport().get_visible_rect().size - var temp_x: float = randf() * screen_size.x - screen_size.x / 2.0 - var temp_y: float = randf() * screen_size.y - screen_size.y / 2.0 + while not found_valid_loc: + print("trying") + index = randi() % possible_food_locations.size() + location = possible_food_locations[index] + if current_food.find(location) == -1: + found_valid_loc = true - return Vector2(temp_x, temp_y) + return [world_generator.get_centered_world_position(location), location] -func _on_food_eaten(type: int) -> void: - current_apples -= 1
\ No newline at end of file +func _on_food_eaten(type: int, location: Vector2) -> void: + var index: int = current_food.find(location) + current_food.remove(index) |