diff options
Diffstat (limited to 'src/entities/food')
-rw-r--r-- | src/entities/food/scenes/Food.tscn | 16 | ||||
-rw-r--r-- | src/entities/food/scenes/FoodManager.tscn | 8 | ||||
-rw-r--r-- | src/entities/food/scripts/food.gd | 23 | ||||
-rw-r--r-- | src/entities/food/scripts/food_manager.gd | 39 | ||||
-rw-r--r-- | src/entities/food/sprites/apple.png | bin | 0 -> 148 bytes | |||
-rw-r--r-- | src/entities/food/sprites/apple.png.import | 35 |
6 files changed, 121 insertions, 0 deletions
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 Binary files differnew file mode 100644 index 0000000..d4fd6ad --- /dev/null +++ b/src/entities/food/sprites/apple.png 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 |