summaryrefslogtreecommitdiff
path: root/src/entities
diff options
context:
space:
mode:
authorDavid Luevano Alvarado <david@luevano.xyz>2022-06-02 02:35:03 -0600
committerDavid Luevano Alvarado <david@luevano.xyz>2022-06-02 02:35:03 -0600
commit898877f09808691a5e5d45850d27ae85f270db16 (patch)
treee4f9005273815c35327b5bdf65026c93301cb593 /src/entities
parent2c5d588bc593929a96f0e58d7d1ab52f269add55 (diff)
add food system
Diffstat (limited to 'src/entities')
-rw-r--r--src/entities/actors/snake/scenes/Body.tscn2
-rw-r--r--src/entities/actors/snake/scenes/Head.tscn7
-rw-r--r--src/entities/actors/snake/scripts/generic_segment.gd10
-rw-r--r--src/entities/actors/snake/scripts/snake.gd22
-rw-r--r--src/entities/food/scenes/Food.tscn16
-rw-r--r--src/entities/food/scenes/FoodManager.tscn8
-rw-r--r--src/entities/food/scripts/food.gd23
-rw-r--r--src/entities/food/scripts/food_manager.gd39
-rw-r--r--src/entities/food/sprites/apple.pngbin0 -> 148 bytes
-rw-r--r--src/entities/food/sprites/apple.png.import35
10 files changed, 149 insertions, 13 deletions
diff --git a/src/entities/actors/snake/scenes/Body.tscn b/src/entities/actors/snake/scenes/Body.tscn
index 8e4a8a4..49694c0 100644
--- a/src/entities/actors/snake/scenes/Body.tscn
+++ b/src/entities/actors/snake/scenes/Body.tscn
@@ -4,7 +4,7 @@
[ext_resource path="res://entities/actors/snake/scripts/generic_segment.gd" type="Script" id=2]
[sub_resource type="CapsuleShape2D" id=1]
-radius = 5.99999
+radius = 4.99999
height = 4.00002
[node name="BodyPathFollow" type="PathFollow2D"]
diff --git a/src/entities/actors/snake/scenes/Head.tscn b/src/entities/actors/snake/scenes/Head.tscn
index 22aa7e6..d118fbf 100644
--- a/src/entities/actors/snake/scenes/Head.tscn
+++ b/src/entities/actors/snake/scenes/Head.tscn
@@ -3,12 +3,11 @@
[ext_resource path="res://entities/actors/snake/sprites/head.png" type="Texture" id=1]
[ext_resource path="res://entities/actors/snake/scripts/head.gd" type="Script" id=2]
-[sub_resource type="CircleShape2D" id=1]
-radius = 5.0
+[sub_resource type="ConvexPolygonShape2D" id=1]
+points = PoolVector2Array( -5, 0, 5, 0, 5, -3, 3, -4, 1, -5, -1, -5, -3, -4, -5, -3 )
[node name="Head" type="KinematicBody2D"]
-position = Vector2( -1, 0 )
-collision_mask = 0
+collision_mask = 262
script = ExtResource( 2 )
[node name="Sprite" type="Sprite" parent="."]
diff --git a/src/entities/actors/snake/scripts/generic_segment.gd b/src/entities/actors/snake/scripts/generic_segment.gd
index 6c65d11..e2db91d 100644
--- a/src/entities/actors/snake/scripts/generic_segment.gd
+++ b/src/entities/actors/snake/scripts/generic_segment.gd
@@ -2,6 +2,16 @@ extends PathFollow2D
export(String, "body", "tail") var TYPE: String = "body"
+onready var _segment: Area2D = get_child(0)
+
+
+func _ready() -> void:
+ _segment.connect("body_entered", self, "_on_body_entered")
+
func _physics_process(delta: float) -> void:
offset += Global.SNAKE_SPEED * delta
+
+
+func _on_body_entered(body: Node) -> void:
+ Event.emit_signal("snake_segment_body_entered", body)
diff --git a/src/entities/actors/snake/scripts/snake.gd b/src/entities/actors/snake/scripts/snake.gd
index 76b8bee..ead6254 100644
--- a/src/entities/actors/snake/scripts/snake.gd
+++ b/src/entities/actors/snake/scripts/snake.gd
@@ -18,9 +18,11 @@ var body_segment_queue: Array
func _ready():
set_physics_process(false)
set_process_input(false)
- Event.connect("snake_path_new_point", self, "_on_Head_snake_path_new_point")
- Event.connect("snake_added_new_segment", self, "_on_Snake_snake_added_new_segment")
- Event.connect("snake_added_initial_segments", self, "_on_Snake_snake_added_initial_segments")
+ Event.connect("snake_path_new_point", self, "_on_snake_path_new_point")
+ 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")
func _physics_process(delta: float) -> void:
@@ -42,7 +44,7 @@ func _add_new_segment() -> void:
var _path_length_threshold: float = body_segment_queue[0] + Global.SNAKE_SEGMENT_SIZE
if path.curve.get_baked_length() >= _path_length_threshold:
var _temp_body_segment: PathFollow2D = BODY_SEGMENT_NP.instance()
- Event.emit_signal("snake_add_new_segment", "body")
+ Event.emit_signal("snake_adding_new_segment", "body")
var _new_body_offset: float = body_segment_stack.back().offset - Global.SNAKE_SEGMENT_SIZE
_temp_body_segment.offset = _new_body_offset
@@ -58,7 +60,7 @@ func _add_new_segment() -> void:
func _add_initial_segment(type: PackedScene) -> void:
if path.curve.get_baked_length() >= (current_body_segments + 1.0) * Global.SNAKE_SEGMENT_SIZE:
var _temp_body_segment: PathFollow2D = type.instance()
- Event.emit_signal("snake_add_new_segment", _temp_body_segment.TYPE)
+ Event.emit_signal("snake_adding_new_segment", _temp_body_segment.TYPE)
if _temp_body_segment.TYPE == "body":
body_segment_stack.append(_temp_body_segment)
else:
@@ -78,7 +80,7 @@ func _add_segment_to_queue() -> void:
body_segment_queue.append(body_segment_queue.back() + Global.SNAKE_SEGMENT_SIZE)
-func _on_Head_snake_path_new_point(coordinates: Vector2) -> void:
+func _on_snake_path_new_point(coordinates: Vector2) -> void:
path.curve.add_point(coordinates)
# update call is to draw curve as there are new points to the path's curve
update()
@@ -89,11 +91,15 @@ func _on_Head_snake_path_new_point(coordinates: Vector2) -> void:
_add_initial_segment(TAIL_SEGMENT_NP)
-func _on_Snake_snake_added_new_segment(type: String) -> void:
+func _on_snake_added_new_segment(type: String) -> void:
if type == "tail":
Event.emit_signal("snake_added_initial_segments")
-func _on_Snake_snake_added_initial_segments() -> void:
+func _on_snake_added_initial_segments() -> void:
set_physics_process(true)
set_process_input(true)
+
+
+func _on_food_eaten(type: int) -> void:
+ _add_segment_to_queue() \ No newline at end of file
diff --git a/src/entities/food/scenes/Food.tscn b/src/entities/food/scenes/Food.tscn
new file mode 100644
index 0000000..bc330e2
--- /dev/null
+++ b/src/entities/food/scenes/Food.tscn
@@ -0,0 +1,16 @@
+[gd_scene load_steps=3 format=2]
+
+[ext_resource path="res://entities/food/scripts/food.gd" type="Script" id=2]
+
+[sub_resource type="CircleShape2D" id=1]
+radius = 7.0
+
+[node name="Food" type="Area2D"]
+collision_layer = 256
+collision_mask = 0
+script = ExtResource( 2 )
+
+[node name="Sprite" type="Sprite" parent="."]
+
+[node name="Collision" type="CollisionShape2D" parent="."]
+shape = SubResource( 1 )
diff --git a/src/entities/food/scenes/FoodManager.tscn b/src/entities/food/scenes/FoodManager.tscn
new file mode 100644
index 0000000..3287271
--- /dev/null
+++ b/src/entities/food/scenes/FoodManager.tscn
@@ -0,0 +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/scripts/food_manager.gd" type="Script" id=2]
+
+[node name="FoodManager" type="Node2D"]
+script = ExtResource( 2 )
+FOOD = ExtResource( 1 )
diff --git a/src/entities/food/scripts/food.gd b/src/entities/food/scripts/food.gd
new file mode 100644
index 0000000..1689837
--- /dev/null
+++ b/src/entities/food/scripts/food.gd
@@ -0,0 +1,23 @@
+class_name Food
+extends Area2D
+
+enum Type {
+ APPLE
+}
+
+var _type_texture: Dictionary = {
+ Type.APPLE: preload("res://entities/food/sprites/apple.png")
+}
+
+export(Type) var TYPE
+onready var _sprite: Sprite = $Sprite
+
+
+func _ready():
+ connect("body_entered", self, "_on_body_entered")
+ _sprite.texture = _type_texture[TYPE]
+
+
+func _on_body_entered(body: Node) -> void:
+ Event.emit_signal("food_eaten", TYPE)
+ 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
new file mode 100644
index 0000000..20772db
--- /dev/null
+++ b/src/entities/food/scripts/food_manager.gd
@@ -0,0 +1,39 @@
+class_name FoodManager
+extends Node2D
+
+export(PackedScene) var FOOD: PackedScene
+
+var max_apples: int = 10
+var current_apples: int = 0
+
+
+func _ready():
+ Event.connect("food_eaten", self, "_on_food_eaten")
+ randomize()
+
+
+func _process(delta) -> void:
+ if current_apples < 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
+ add_child(food)
+ Event.emit_signal("food_placed_new_food", food.TYPE)
+
+
+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
+
+ return Vector2(temp_x, temp_y)
+
+
+func _on_food_eaten(type: int) -> void:
+ current_apples -= 1 \ No newline at end of file
diff --git a/src/entities/food/sprites/apple.png b/src/entities/food/sprites/apple.png
new file mode 100644
index 0000000..d4fd6ad
--- /dev/null
+++ b/src/entities/food/sprites/apple.png
Binary files differ
diff --git a/src/entities/food/sprites/apple.png.import b/src/entities/food/sprites/apple.png.import
new file mode 100644
index 0000000..c007dad
--- /dev/null
+++ b/src/entities/food/sprites/apple.png.import
@@ -0,0 +1,35 @@
+[remap]
+
+importer="texture"
+type="StreamTexture"
+path="res://.import/apple.png-3f0e1c07d45c9f73006d6dd04c7dfa93.stex"
+metadata={
+"vram_texture": false
+}
+
+[deps]
+
+source_file="res://entities/food/sprites/apple.png"
+dest_files=[ "res://.import/apple.png-3f0e1c07d45c9f73006d6dd04c7dfa93.stex" ]
+
+[params]
+
+compress/mode=0
+compress/lossy_quality=0.7
+compress/hdr_mode=0
+compress/bptc_ldr=0
+compress/normal_map=0
+flags/repeat=0
+flags/filter=false
+flags/mipmaps=false
+flags/anisotropic=false
+flags/srgb=2
+process/fix_alpha_border=false
+process/premult_alpha=false
+process/HDR_as_SRGB=false
+process/invert_color=false
+process/normal_map_invert_y=false
+stream=false
+size_limit=0
+detect_3d=false
+svg/scale=1.0