From f922fe4669080d1633e0a345a3f8981867c9e841 Mon Sep 17 00:00:00 2001 From: David Luevano Alvarado Date: Fri, 3 Jun 2022 21:13:19 -0600 Subject: add working world gen, fixed food placing, minor refactoring --- src/entities/food/scenes/Food.tscn | 2 +- src/entities/food/scripts/food.gd | 3 ++- src/entities/food/scripts/food_manager.gd | 43 ++++++++++++++++++++---------- src/entities/food/sprites/apple.png | Bin 148 -> 135 bytes 4 files changed, 32 insertions(+), 16 deletions(-) (limited to 'src/entities/food') diff --git a/src/entities/food/scenes/Food.tscn b/src/entities/food/scenes/Food.tscn index bc330e2..7accbc6 100644 --- a/src/entities/food/scenes/Food.tscn +++ b/src/entities/food/scenes/Food.tscn @@ -3,7 +3,7 @@ [ext_resource path="res://entities/food/scripts/food.gd" type="Script" id=2] [sub_resource type="CircleShape2D" id=1] -radius = 7.0 +radius = 4.0 [node name="Food" type="Area2D"] collision_layer = 256 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) diff --git a/src/entities/food/sprites/apple.png b/src/entities/food/sprites/apple.png index d4fd6ad..7111450 100644 Binary files a/src/entities/food/sprites/apple.png and b/src/entities/food/sprites/apple.png differ -- cgit v1.2.3-70-g09d2