From e4423cc8490b7f5ec3449f568bf64e81f4d03248 Mon Sep 17 00:00:00 2001 From: David Luevano Alvarado Date: Sun, 5 Jun 2022 04:38:44 -0600 Subject: add more food types, refactored code and tidy up stuff --- src/entities/food/scenes/FoodManager.tscn | 4 ++- src/entities/food/scenes/FoodSpecial.tscn | 16 +++++++++ src/entities/food/scripts/food.gd | 39 +++++++-------------- src/entities/food/scripts/food_basic.gd | 2 +- src/entities/food/scripts/food_manager.gd | 52 ++++++++++++++++++++++------ src/entities/food/scripts/food_special.gd | 16 +++++++++ src/entities/food/sprites/bunny.png | Bin 0 -> 242 bytes src/entities/food/sprites/bunny.png.import | 35 +++++++++++++++++++ src/entities/food/sprites/frog.png | Bin 0 -> 246 bytes src/entities/food/sprites/frog.png.import | 35 +++++++++++++++++++ src/entities/food/sprites/rat.png | Bin 0 -> 277 bytes src/entities/food/sprites/rat.png.import | 35 +++++++++++++++++++ src/entities/food/sprites/turtle.png | Bin 0 -> 258 bytes src/entities/food/sprites/turtle.png.import | 35 +++++++++++++++++++ 14 files changed, 230 insertions(+), 39 deletions(-) create mode 100644 src/entities/food/scenes/FoodSpecial.tscn create mode 100644 src/entities/food/scripts/food_special.gd create mode 100644 src/entities/food/sprites/bunny.png create mode 100644 src/entities/food/sprites/bunny.png.import create mode 100644 src/entities/food/sprites/frog.png create mode 100644 src/entities/food/sprites/frog.png.import create mode 100644 src/entities/food/sprites/rat.png create mode 100644 src/entities/food/sprites/rat.png.import create mode 100644 src/entities/food/sprites/turtle.png create mode 100644 src/entities/food/sprites/turtle.png.import (limited to 'src/entities/food') diff --git a/src/entities/food/scenes/FoodManager.tscn b/src/entities/food/scenes/FoodManager.tscn index e56b9de..73bde55 100644 --- a/src/entities/food/scenes/FoodManager.tscn +++ b/src/entities/food/scenes/FoodManager.tscn @@ -1,8 +1,10 @@ -[gd_scene load_steps=3 format=2] +[gd_scene load_steps=4 format=2] [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] +[ext_resource path="res://entities/food/scenes/FoodSpecial.tscn" type="PackedScene" id=3] [node name="FoodManager" type="Node2D"] script = ExtResource( 2 ) FOOD_BASIC = ExtResource( 1 ) +FOOD_SPECIAL = ExtResource( 3 ) diff --git a/src/entities/food/scenes/FoodSpecial.tscn b/src/entities/food/scenes/FoodSpecial.tscn new file mode 100644 index 0000000..1b8c4e6 --- /dev/null +++ b/src/entities/food/scenes/FoodSpecial.tscn @@ -0,0 +1,16 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://entities/food/scripts/food_special.gd" type="Script" id=2] + +[sub_resource type="CircleShape2D" id=1] +radius = 8.0 + +[node name="FoodSpecial" 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/scripts/food.gd b/src/entities/food/scripts/food.gd index aa5207f..386f0e5 100644 --- a/src/entities/food/scripts/food.gd +++ b/src/entities/food/scripts/food.gd @@ -7,13 +7,9 @@ var texture: Dictionary var timer: Timer 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 @@ -21,35 +17,24 @@ func _ready(): connect("body_entered", self, "_on_body_entered") -func set_type(type: int) -> void: - set_property("type", type) - _sprite.texture = texture[type] +func update_texture() -> void: + _sprite.texture = texture[properties["type"]] -func set_location(loc: Vector2) -> void: - set_property("location", loc) +func set_properties(pos: Vector2, loc: Vector2, special: bool, type: int, points: int=1, ttl: float = -1.0) -> void: + properties["global_position"] = pos + global_position = pos + properties["location"] = loc + properties["special"] = special + properties["type"] = type - -func set_timer(time: float) -> void: - time_to_live = time - if time_to_live != -1.0: - timer.wait_time = time_to_live + properties["points"] = points + properties["ttl"] = ttl + if properties["ttl"] != -1.0: + timer.wait_time = properties["ttl"] timer.start() -func set_property(property: String, value) -> void: - properties[property] = value - - -func _set_properties() -> void: - set_property("points", points) - set_property("global_position", global_position) - - -func randomize_stats() -> void: - points = int(rand_range(1, 10)) - - func _on_body_entered(body: Node) -> void: Event.emit_signal("food_eaten", properties) queue_free() diff --git a/src/entities/food/scripts/food_basic.gd b/src/entities/food/scripts/food_basic.gd index 032ec23..8090cdc 100644 --- a/src/entities/food/scripts/food_basic.gd +++ b/src/entities/food/scripts/food_basic.gd @@ -9,4 +9,4 @@ enum Type { func _ready(): texture[Type.APPLE] = preload("res://entities/food/sprites/apple.png") - texture[Type.BANANA] = preload("res://entities/food/sprites/banana.png") \ No newline at end of file + texture[Type.BANANA] = preload("res://entities/food/sprites/banana.png") diff --git a/src/entities/food/scripts/food_manager.gd b/src/entities/food/scripts/food_manager.gd index bbe27be..a605f9e 100644 --- a/src/entities/food/scripts/food_manager.gd +++ b/src/entities/food/scripts/food_manager.gd @@ -2,39 +2,66 @@ class_name FoodManager extends Node2D export(PackedScene) var FOOD_BASIC: PackedScene +export(PackedScene) var FOOD_SPECIAL: 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() +# needed to know what food is available to setup +var stats: Stats = SaveData.get_stats() + var current_basic_food: Array = [] +var current_special_food: Array = [] +var max_basic_food: int +var max_special_food: int func _ready(): randomize() + max_basic_food = int(possible_food_locations.size() * Global.MAX_BASIC_FOOD) + max_special_food = int(possible_food_locations.size() * Global.MAX_SPECIAL_FOOD) Event.connect("food_eaten", self, "_on_food_eaten") func _process(delta) -> void: - if current_basic_food.size() < Global.MAX_BASIC_FOOD: + if current_basic_food.size() < max_basic_food: _place_new_basic_food() + if current_special_food.size() < max_special_food: + _place_new_special_food() + func _place_new_basic_food() -> void: var food: FoodBasic = FOOD_BASIC.instance() var type: int = _get_random_food_type(FoodBasic.Type) - Event.emit_signal("food_placing_new_food", type) + Event.emit_signal("food_placing_new_food", false, 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.set_properties(pos, loc, false, type) add_child(food) - food.set_type(type) - food.set_location(loc) - # food.properties["points"] = 1 + food.update_texture() current_basic_food.append(loc) - Event.emit_signal("food_placed_new_food", food.properties["type"], food.properties["location"]) + Event.emit_signal("food_placed_new_food", food.properties) + + +func _place_new_special_food() -> void: + var food: FoodSpecial = FOOD_SPECIAL.instance() + var type: int = _get_random_food_type(FoodSpecial.Type) + Event.emit_signal("food_placing_new_food", true, 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.set_properties(pos, loc, true, type, Global.POINTS_TO_GROW) + add_child(food) + food.update_texture() + current_special_food.append(loc) + Event.emit_signal("food_placed_new_food", food.properties) func _get_random_food_type(type) -> int: @@ -49,12 +76,17 @@ func _get_random_pos() -> Array: while not found_valid_loc: index = randi() % possible_food_locations.size() location = possible_food_locations[index] - if current_basic_food.find(location) == -1: + if current_basic_food.find(location) == -1 and current_special_food.find(location) == -1: found_valid_loc = true return [world_generator.get_centered_world_position(location), location] func _on_food_eaten(properties: Dictionary) -> void: - var index: int = current_basic_food.find(properties["location"]) - current_basic_food.remove(index) + var index: int + if properties["special"]: + index = current_special_food.find(properties["location"]) + current_special_food.remove(index) + else: + index = current_basic_food.find(properties["location"]) + current_basic_food.remove(index) diff --git a/src/entities/food/scripts/food_special.gd b/src/entities/food/scripts/food_special.gd new file mode 100644 index 0000000..c2fde78 --- /dev/null +++ b/src/entities/food/scripts/food_special.gd @@ -0,0 +1,16 @@ +class_name FoodSpecial +extends Food + +enum Type { + BUNNY, + FROG, + TURTLE, + RAT +} + + +func _ready(): + texture[Type.BUNNY] = preload("res://entities/food/sprites/bunny.png") + texture[Type.FROG] = preload("res://entities/food/sprites/frog.png") + texture[Type.TURTLE] = preload("res://entities/food/sprites/turtle.png") + texture[Type.RAT] = preload("res://entities/food/sprites/rat.png") diff --git a/src/entities/food/sprites/bunny.png b/src/entities/food/sprites/bunny.png new file mode 100644 index 0000000..900ab6c Binary files /dev/null and b/src/entities/food/sprites/bunny.png differ diff --git a/src/entities/food/sprites/bunny.png.import b/src/entities/food/sprites/bunny.png.import new file mode 100644 index 0000000..435f4b3 --- /dev/null +++ b/src/entities/food/sprites/bunny.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/bunny.png-98d2abf876bf5ec1713a2e3b2c3305b3.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://entities/food/sprites/bunny.png" +dest_files=[ "res://.import/bunny.png-98d2abf876bf5ec1713a2e3b2c3305b3.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 diff --git a/src/entities/food/sprites/frog.png b/src/entities/food/sprites/frog.png new file mode 100644 index 0000000..4057647 Binary files /dev/null and b/src/entities/food/sprites/frog.png differ diff --git a/src/entities/food/sprites/frog.png.import b/src/entities/food/sprites/frog.png.import new file mode 100644 index 0000000..ea33961 --- /dev/null +++ b/src/entities/food/sprites/frog.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/frog.png-f3af79da776ffc12de65d12bcff92073.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://entities/food/sprites/frog.png" +dest_files=[ "res://.import/frog.png-f3af79da776ffc12de65d12bcff92073.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 diff --git a/src/entities/food/sprites/rat.png b/src/entities/food/sprites/rat.png new file mode 100644 index 0000000..b63923f Binary files /dev/null and b/src/entities/food/sprites/rat.png differ diff --git a/src/entities/food/sprites/rat.png.import b/src/entities/food/sprites/rat.png.import new file mode 100644 index 0000000..92456c9 --- /dev/null +++ b/src/entities/food/sprites/rat.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/rat.png-efc63e12fde232a4cdae97b805791ec6.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://entities/food/sprites/rat.png" +dest_files=[ "res://.import/rat.png-efc63e12fde232a4cdae97b805791ec6.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 diff --git a/src/entities/food/sprites/turtle.png b/src/entities/food/sprites/turtle.png new file mode 100644 index 0000000..e901da8 Binary files /dev/null and b/src/entities/food/sprites/turtle.png differ diff --git a/src/entities/food/sprites/turtle.png.import b/src/entities/food/sprites/turtle.png.import new file mode 100644 index 0000000..cc452c7 --- /dev/null +++ b/src/entities/food/sprites/turtle.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/turtle.png-fd98564ac1dff9497a69b2772482c676.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://entities/food/sprites/turtle.png" +dest_files=[ "res://.import/turtle.png-fd98564ac1dff9497a69b2772482c676.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 -- cgit v1.2.3