summaryrefslogtreecommitdiff
path: root/src/entities
diff options
context:
space:
mode:
authorDavid Luevano Alvarado <david@luevano.xyz>2022-06-04 03:17:22 -0600
committerDavid Luevano Alvarado <david@luevano.xyz>2022-06-04 03:17:22 -0600
commit36874a535a3d5f7f2955f33e34aa1a4768b6fec1 (patch)
tree8f4eaa57d5e82d962719a8a399cfc210ced672e6 /src/entities
parentf922fe4669080d1633e0a345a3f8981867c9e841 (diff)
added hud for snake size and grow progress
Diffstat (limited to 'src/entities')
-rw-r--r--src/entities/actors/snake/scripts/snake.gd24
-rw-r--r--src/entities/food/scenes/FoodBasic.tscn (renamed from src/entities/food/scenes/Food.tscn)6
-rw-r--r--src/entities/food/scenes/FoodManager.tscn4
-rw-r--r--src/entities/food/scripts/food.gd59
-rw-r--r--src/entities/food/scripts/food_basic.gd10
-rw-r--r--src/entities/food/scripts/food_manager.gd19
6 files changed, 88 insertions, 34 deletions
diff --git a/src/entities/actors/snake/scripts/snake.gd b/src/entities/actors/snake/scripts/snake.gd
index 01e09da..91e4e39 100644
--- a/src/entities/actors/snake/scripts/snake.gd
+++ b/src/entities/actors/snake/scripts/snake.gd
@@ -6,8 +6,8 @@ export(PackedScene) var TAIL_SEGMENT_NP: PackedScene
onready var path: Path2D = $Path
+var finished_adding_initial_segments: bool = false
var current_body_segments: int = 0
-var max_body_initial_segments: int = 1
var body_segment_stack: Array
var tail_segment: PathFollow2D
# didn't konw how to name this, basically holds the current path lenght
@@ -20,16 +20,17 @@ func _ready():
set_physics_process(false)
set_process_input(false)
Event.connect("snake_path_new_point", self, "_on_snake_path_new_point")
+ Event.connect("snake_add_new_segment", self, "_on_snake_add_new_segment")
Event.connect("snake_added_new_segment", self, "_on_snake_added_new_segment")
Event.connect("snake_added_initial_segments", self, "_on_snake_added_initial_segments")
- Event.connect("food_eaten", self, "_on_food_eaten")
+ # Event.connect("food_eaten", self, "_on_food_eaten")
# need to always set a new curve when ready, so when restarting it's af resh curve
path.curve = Curve2D.new()
func _physics_process(delta: float) -> void:
- if body_segment_queue.size() != 0 and current_body_segments > max_body_initial_segments:
+ if body_segment_queue.size() != 0 and current_body_segments >= Global.SNAKE_INITIAL_SEGMENTS:
_add_new_segment()
# if body_segment_stack.size() > 0:
@@ -75,6 +76,7 @@ func _add_initial_segment(type: PackedScene) -> void:
tail_segment = _temp_body_segment
path.add_child(_temp_body_segment)
+ # just to keep things going, tail shouldn't count as body segment
current_body_segments += 1
Event.emit_signal("snake_added_new_segment", _temp_body_segment.TYPE)
@@ -93,14 +95,17 @@ func _on_snake_path_new_point(coordinates: Vector2) -> void:
# update call is to draw curve as there are new points to the path's curve
# update()
- if current_body_segments < max_body_initial_segments:
- _add_initial_segment(BODY_SEGMENT_NP)
- elif current_body_segments == max_body_initial_segments:
- _add_initial_segment(TAIL_SEGMENT_NP)
+ if not finished_adding_initial_segments:
+ if current_body_segments < Global.SNAKE_INITIAL_SEGMENTS:
+ _add_initial_segment(BODY_SEGMENT_NP)
+ elif current_body_segments == Global.SNAKE_INITIAL_SEGMENTS:
+ _add_initial_segment(TAIL_SEGMENT_NP)
func _on_snake_added_new_segment(type: String) -> void:
if type == "tail":
+ current_body_segments -= 1
+ finished_adding_initial_segments = true
Event.emit_signal("snake_added_initial_segments")
@@ -109,5 +114,6 @@ func _on_snake_added_initial_segments() -> void:
set_process_input(true)
-func _on_food_eaten(type: int, location: Vector2) -> void:
- _add_segment_to_queue()
+func _on_snake_add_new_segment(amount: int) -> void:
+ for i in amount:
+ _add_segment_to_queue()
diff --git a/src/entities/food/scenes/Food.tscn b/src/entities/food/scenes/FoodBasic.tscn
index 7accbc6..5a4474e 100644
--- a/src/entities/food/scenes/Food.tscn
+++ b/src/entities/food/scenes/FoodBasic.tscn
@@ -1,14 +1,14 @@
[gd_scene load_steps=3 format=2]
-[ext_resource path="res://entities/food/scripts/food.gd" type="Script" id=2]
+[ext_resource path="res://entities/food/scripts/food_basic.gd" type="Script" id=1]
[sub_resource type="CircleShape2D" id=1]
radius = 4.0
-[node name="Food" type="Area2D"]
+[node name="FoodBasic" type="Area2D"]
collision_layer = 256
collision_mask = 0
-script = ExtResource( 2 )
+script = ExtResource( 1 )
[node name="Sprite" type="Sprite" parent="."]
diff --git a/src/entities/food/scenes/FoodManager.tscn b/src/entities/food/scenes/FoodManager.tscn
index 3287271..e56b9de 100644
--- a/src/entities/food/scenes/FoodManager.tscn
+++ b/src/entities/food/scenes/FoodManager.tscn
@@ -1,8 +1,8 @@
[gd_scene load_steps=3 format=2]
-[ext_resource path="res://entities/food/scenes/Food.tscn" type="PackedScene" id=1]
+[ext_resource path="res://entities/food/scenes/FoodBasic.tscn" type="PackedScene" id=1]
[ext_resource path="res://entities/food/scripts/food_manager.gd" type="Script" id=2]
[node name="FoodManager" type="Node2D"]
script = ExtResource( 2 )
-FOOD = ExtResource( 1 )
+FOOD_BASIC = ExtResource( 1 )
diff --git a/src/entities/food/scripts/food.gd b/src/entities/food/scripts/food.gd
index e8b0261..2e00978 100644
--- a/src/entities/food/scripts/food.gd
+++ b/src/entities/food/scripts/food.gd
@@ -1,24 +1,59 @@
class_name Food
extends Area2D
-enum Type {
- APPLE
-}
+onready var _sprite: Sprite = $Sprite
-var _type_texture: Dictionary = {
- Type.APPLE: preload("res://entities/food/sprites/apple.png")
-}
+var texture: Dictionary
+var timer: Timer
-export(Type) var TYPE
-onready var _sprite: Sprite = $Sprite
-var location: Vector2
+var properties: Dictionary
+var points: int = 1
+var time_to_live: float = -1.0
func _ready():
+ randomize_stats()
+ _set_properties()
+ timer = Timer.new()
+ timer.one_shot = true
+
+ timer.connect("timeout", self, "_on_timer_timeout")
connect("body_entered", self, "_on_body_entered")
- _sprite.texture = _type_texture[TYPE]
+
+
+func set_type(type: int) -> void:
+ set_property("type", type)
+ _sprite.texture = texture[type]
+
+
+func set_location(loc: Vector2) -> void:
+ set_property("location", loc)
+
+
+func set_timer(time: float) -> void:
+ time_to_live = time
+ if time_to_live != -1.0:
+ timer.wait_time = time_to_live
+ timer.start()
+
+
+func set_property(property: String, value) -> void:
+ properties[property] = value
+
+
+func _set_properties() -> void:
+ set_property("points", points)
+
+
+func randomize_stats() -> void:
+ points = int(rand_range(1, 30))
func _on_body_entered(body: Node) -> void:
- Event.emit_signal("food_eaten", TYPE, location)
- queue_free() \ No newline at end of file
+ Event.emit_signal("food_eaten", properties)
+ queue_free()
+
+
+func _on_timer_timeout() -> void:
+ Event.emit_signal("food_timed_out", properties)
+ queue_free()
diff --git a/src/entities/food/scripts/food_basic.gd b/src/entities/food/scripts/food_basic.gd
new file mode 100644
index 0000000..0a56208
--- /dev/null
+++ b/src/entities/food/scripts/food_basic.gd
@@ -0,0 +1,10 @@
+class_name FoodBasic
+extends Food
+
+enum Type {
+ APPLE
+}
+
+
+func _ready():
+ texture[Type.APPLE] = preload("res://entities/food/sprites/apple.png") \ 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 426677c..4196806 100644
--- a/src/entities/food/scripts/food_manager.gd
+++ b/src/entities/food/scripts/food_manager.gd
@@ -1,7 +1,7 @@
class_name FoodManager
extends Node2D
-export(PackedScene) var FOOD: PackedScene
+export(PackedScene) var FOOD_BASIC: 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()
@@ -21,17 +21,21 @@ func _process(delta) -> void:
func _place_new_food() -> void:
- var food: Area2D = FOOD.instance()
- Event.emit_signal("food_placing_new_food", food.TYPE)
+ var food: FoodBasic = FOOD_BASIC.instance()
+ var type: int = FoodBasic.Type.APPLE
+ Event.emit_signal("food_placing_new_food", type)
var pos_loc: Array = _get_random_pos()
var pos: Vector2 = pos_loc[0]
var loc: Vector2 = pos_loc[1]
+ # need to set the position first, else it will spawn on the middle and the moved
food.global_position = pos
- food.location = loc
add_child(food)
+ food.set_type(FoodBasic.Type.APPLE)
+ food.set_location(loc)
+ food.properties["points"] = 1
current_food.append(loc)
- Event.emit_signal("food_placed_new_food", food.TYPE, loc)
+ Event.emit_signal("food_placed_new_food", food.properties["type"], food.properties["location"])
func _get_random_pos() -> Array:
@@ -40,7 +44,6 @@ func _get_random_pos() -> Array:
var location: Vector2
while not found_valid_loc:
- print("trying")
index = randi() % possible_food_locations.size()
location = possible_food_locations[index]
if current_food.find(location) == -1:
@@ -49,6 +52,6 @@ func _get_random_pos() -> Array:
return [world_generator.get_centered_world_position(location), location]
-func _on_food_eaten(type: int, location: Vector2) -> void:
- var index: int = current_food.find(location)
+func _on_food_eaten(properties: Dictionary) -> void:
+ var index: int = current_food.find(properties["location"])
current_food.remove(index)