From fbbb4af708185a8c493fea2d73da32245ca5a2f6 Mon Sep 17 00:00:00 2001 From: Victor Date: Sun, 1 Sep 2024 21:06:48 +0200 Subject: [PATCH] objectrees plant count --- objects/Animal.tscn | 2 +- scripts/animal.gd | 21 +++++++++++++++++---- scripts/terrain.gd | 8 +++++++- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/objects/Animal.tscn b/objects/Animal.tscn index 321e42b..f1aa943 100644 --- a/objects/Animal.tscn +++ b/objects/Animal.tscn @@ -10,7 +10,7 @@ radius = 599.083 script = ExtResource("1_bjim0") [node name="Sprite2D" type="Sprite2D" parent="."] -scale = Vector2(0.3, 0.3) +scale = Vector2(0.7, 0.7) texture = ExtResource("2_n8edq") offset = Vector2(170, -550) diff --git a/scripts/animal.gd b/scripts/animal.gd index 45f3ed4..61b7188 100644 --- a/scripts/animal.gd +++ b/scripts/animal.gd @@ -2,17 +2,30 @@ extends Node2D signal liberated -@export var plant_need: String -var libarated := false +@export var n_plant_needed: int + +var is_liberated := false +var current_plants := 0 func _on_area_2d_area_entered(area: Area2D) -> void: var plant = area.get_parent() if plant is Plant and not liberated: - if plant_need == plant.parameter.type: - plant.grown.connect(tracked_plant_grew) + plant.grown.connect(tracked_plant_grew) + plant.died.connect() func tracked_plant_grew(): if liberated: return + current_plants += 1 + if current_plants == n_plant_needed: + liberate() + +func tracked_plant_died(): + if liberated: + return + current_plants -= 1 + +func liberate(): + is_liberated = true print("Liberated !!") liberated.emit() diff --git a/scripts/terrain.gd b/scripts/terrain.gd index 0d74151..2418a4f 100644 --- a/scripts/terrain.gd +++ b/scripts/terrain.gd @@ -28,7 +28,7 @@ func map_to_pixel( ) func is_on_map(pos: Vector2) -> bool: - return pos.x >= 0 and pos.x <= TERRAIN_SIZE.x * MAP_RATIO and pos.y >= 0 and pos.y <= TERRAIN_SIZE.y * MAP_RATIO + return pos.x >= 0 and pos.x < TERRAIN_SIZE.x * MAP_RATIO and pos.y >= 0 and pos.y < TERRAIN_SIZE.y * MAP_RATIO func color_value_to_level( color_value : float @@ -84,6 +84,8 @@ func modify_pixel( stat: Stats, modification: int, ): + if not is_on_map(pixel_pos): + return var actual_levels = color_to_levels(image.get_pixelv(pixel_pos)) var modification_levels = modification_to_levels(stat, modification) var calculated_levels = actual_levels + modification_levels @@ -93,6 +95,8 @@ func set_pixel( pixel_pos: Vector2i, level: Vector3i, ): + if not is_on_map(pixel_pos): + return image.set_pixelv(pixel_pos, levels_to_color(level)) func modify_zone( @@ -142,6 +146,8 @@ func get_stat( pos: Vector2, stat : Stats ) -> int: + if not is_on_map(pos): + return 0 var pixel_pos = map_to_pixel(pos) var levels = color_to_levels(image.get_pixelv(pixel_pos)) match stat: