summaryrefslogtreecommitdiff
path: root/src/entities/food
diff options
context:
space:
mode:
authorDavid Luevano Alvarado <david@luevano.xyz>2022-06-03 21:13:19 -0600
committerDavid Luevano Alvarado <david@luevano.xyz>2022-06-03 21:13:19 -0600
commitf922fe4669080d1633e0a345a3f8981867c9e841 (patch)
tree7c2c98bfca20fda37fe34ba99e2d3caa9a39ca02 /src/entities/food
parent898877f09808691a5e5d45850d27ae85f270db16 (diff)
add working world gen, fixed food placing, minor refactoring
Diffstat (limited to 'src/entities/food')
-rw-r--r--src/entities/food/scenes/Food.tscn2
-rw-r--r--src/entities/food/scripts/food.gd3
-rw-r--r--src/entities/food/scripts/food_manager.gd43
-rw-r--r--src/entities/food/sprites/apple.pngbin148 -> 135 bytes
4 files changed, 32 insertions, 16 deletions
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
--- a/src/entities/food/sprites/apple.png
+++ b/src/entities/food/sprites/apple.png
Binary files differ