diff options
author | David Luevano Alvarado <david@luevano.xyz> | 2022-06-04 23:00:58 -0600 |
---|---|---|
committer | David Luevano Alvarado <david@luevano.xyz> | 2022-06-04 23:00:58 -0600 |
commit | 36abc689d783774ce4f2d7b5a1bb621d8684be45 (patch) | |
tree | a3dbed6069fbd8d9a588c7510ce581af3e847af0 /src | |
parent | 9a2bcf02c2623c8f3e8f5e74e70b3c0333790484 (diff) |
added more ui for after gameplay, generalized basic food
Diffstat (limited to 'src')
38 files changed, 839 insertions, 86 deletions
diff --git a/src/Game.tscn b/src/Game.tscn index 1709a85..7971672 100644 --- a/src/Game.tscn +++ b/src/Game.tscn @@ -1,24 +1,27 @@ -[gd_scene load_steps=7 format=2] +[gd_scene load_steps=8 format=2] [ext_resource path="res://entities/actors/snake/scenes/Snake.tscn" type="PackedScene" id=1] [ext_resource path="res://ui/hud/HUD.tscn" type="PackedScene" id=2] -[ext_resource path="res://tools/score_manager/ScoreManager.tscn" type="PackedScene" id=3] +[ext_resource path="res://tools/score_manager/scenes/ScoreManager.tscn" type="PackedScene" id=3] [ext_resource path="res://entities/food/scenes/FoodManager.tscn" type="PackedScene" id=4] [ext_resource path="res://tools/world_generator/scenes/WorldGenerator.tscn" type="PackedScene" id=5] [ext_resource path="res://game.gd" type="Script" id=6] +[ext_resource path="res://ui/stats_display/scenes/StatsDisplay.tscn" type="PackedScene" id=7] [node name="Game" type="Node2D"] script = ExtResource( 6 ) [node name="WorldGenerator" parent="." instance=ExtResource( 5 )] -[node name="ScoreManager" parent="." instance=ExtResource( 3 )] - [node name="Snake" parent="." instance=ExtResource( 1 )] [node name="FoodManager" parent="." instance=ExtResource( 4 )] WORLD_GENERATOR_NP = NodePath("../WorldGenerator") -[node name="HUDScreen" type="CanvasLayer" parent="."] +[node name="ScoreManager" parent="." instance=ExtResource( 3 )] + +[node name="UIScreen" type="CanvasLayer" parent="."] + +[node name="HUD" parent="UIScreen" instance=ExtResource( 2 )] -[node name="HUD" parent="HUDScreen" instance=ExtResource( 2 )] +[node name="StatsDisplay" parent="UIScreen" instance=ExtResource( 7 )] diff --git a/src/entities/actors/snake/scripts/head.gd b/src/entities/actors/snake/scripts/head.gd index 32847f7..8747910 100644 --- a/src/entities/actors/snake/scripts/head.gd +++ b/src/entities/actors/snake/scripts/head.gd @@ -28,6 +28,19 @@ func _physics_process(delta: float) -> void: # not sure if needed, worked wonders when using a Node2D instead of KB2D velocity = move_and_slide(velocity) + + # slow down on collisions, so it isn't as unfair + if get_last_slide_collision(): + var speed: float = velocity.length() + Global.SNAKE_SPEED = speed + else: + Global.SNAKE_SPEED = Global.SNAKE_SPEED_BACKUP + + # handle slow speeds + if Global.SNAKE_SPEED <= Global.SNAKE_SPEED_BACKUP / 4.0: + Global.SNAKE_SPEED = Global.SNAKE_SPEED_BACKUP + Event.emit_signal("game_over") + _handle_time_elapsed(delta) diff --git a/src/entities/actors/snake/scripts/snake.gd b/src/entities/actors/snake/scripts/snake.gd index 91e4e39..bae6e83 100644 --- a/src/entities/actors/snake/scripts/snake.gd +++ b/src/entities/actors/snake/scripts/snake.gd @@ -6,6 +6,8 @@ export(PackedScene) var TAIL_SEGMENT_NP: PackedScene onready var path: Path2D = $Path +var stats: Stats = SaveData.get_stats() + var finished_adding_initial_segments: bool = false var current_body_segments: int = 0 var body_segment_stack: Array diff --git a/src/entities/food/scripts/food.gd b/src/entities/food/scripts/food.gd index ca27acc..aa5207f 100644 --- a/src/entities/food/scripts/food.gd +++ b/src/entities/food/scripts/food.gd @@ -47,7 +47,7 @@ func _set_properties() -> void: func randomize_stats() -> void: - points = int(rand_range(1, 30)) + points = int(rand_range(1, 10)) func _on_body_entered(body: Node) -> void: diff --git a/src/entities/food/scripts/food_basic.gd b/src/entities/food/scripts/food_basic.gd index 0a56208..032ec23 100644 --- a/src/entities/food/scripts/food_basic.gd +++ b/src/entities/food/scripts/food_basic.gd @@ -2,9 +2,11 @@ class_name FoodBasic extends Food enum Type { - APPLE + APPLE, + BANANA } func _ready(): - texture[Type.APPLE] = preload("res://entities/food/sprites/apple.png")
\ No newline at end of file + 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 diff --git a/src/entities/food/scripts/food_manager.gd b/src/entities/food/scripts/food_manager.gd index 4196806..bbe27be 100644 --- a/src/entities/food/scripts/food_manager.gd +++ b/src/entities/food/scripts/food_manager.gd @@ -6,8 +6,7 @@ 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() -var max_apples: int = 10 -var current_food: Array = [] +var current_basic_food: Array = [] func _ready(): @@ -16,13 +15,13 @@ func _ready(): func _process(delta) -> void: - if current_food.size() < max_apples: - _place_new_food() + if current_basic_food.size() < Global.MAX_BASIC_FOOD: + _place_new_basic_food() -func _place_new_food() -> void: +func _place_new_basic_food() -> void: var food: FoodBasic = FOOD_BASIC.instance() - var type: int = FoodBasic.Type.APPLE + var type: int = _get_random_food_type(FoodBasic.Type) Event.emit_signal("food_placing_new_food", type) var pos_loc: Array = _get_random_pos() var pos: Vector2 = pos_loc[0] @@ -31,13 +30,17 @@ func _place_new_food() -> void: # need to set the position first, else it will spawn on the middle and the moved food.global_position = pos add_child(food) - food.set_type(FoodBasic.Type.APPLE) + food.set_type(type) food.set_location(loc) - food.properties["points"] = 1 - current_food.append(loc) + # food.properties["points"] = 1 + current_basic_food.append(loc) Event.emit_signal("food_placed_new_food", food.properties["type"], food.properties["location"]) +func _get_random_food_type(type) -> int: + return randi() % type.size() + + func _get_random_pos() -> Array: var found_valid_loc: bool = false var index: int @@ -46,12 +49,12 @@ func _get_random_pos() -> Array: while not found_valid_loc: index = randi() % possible_food_locations.size() location = possible_food_locations[index] - if current_food.find(location) == -1: + if current_basic_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_food.find(properties["location"]) - current_food.remove(index) + var index: int = current_basic_food.find(properties["location"]) + current_basic_food.remove(index) diff --git a/src/entities/food/sprites/apple.png b/src/entities/food/sprites/apple.png Binary files differindex 7111450..2eb9080 100644 --- a/src/entities/food/sprites/apple.png +++ b/src/entities/food/sprites/apple.png diff --git a/src/entities/food/sprites/banana.png b/src/entities/food/sprites/banana.png Binary files differnew file mode 100644 index 0000000..afe7b51 --- /dev/null +++ b/src/entities/food/sprites/banana.png diff --git a/src/entities/food/sprites/banana.png.import b/src/entities/food/sprites/banana.png.import new file mode 100644 index 0000000..e4da833 --- /dev/null +++ b/src/entities/food/sprites/banana.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/banana.png-790b92b84b3b62d7a7828565ae8ed4ec.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://entities/food/sprites/banana.png" +dest_files=[ "res://.import/banana.png-790b92b84b3b62d7a7828565ae8ed4ec.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/event.gd b/src/event.gd index fb79a30..cc15684 100644 --- a/src/event.gd +++ b/src/event.gd @@ -22,3 +22,5 @@ signal world_gen_walker_started(id) signal world_gen_walker_finished(id) signal world_gen_walker_died(id) signal world_gen_spawn_walker_unit(location) + +signal display_stats(old_stats, new_stats, mut_stats)
\ No newline at end of file diff --git a/src/global.gd b/src/global.gd index 59fd4bd..01d3388 100644 --- a/src/global.gd +++ b/src/global.gd @@ -8,9 +8,12 @@ var WORLD_TILE_WALL: int = 1 var SNAKE_INITIAL_SEGMENTS: int = 1 var SNAKE_SPEED: float = 50.0 +var SNAKE_SPEED_BACKUP: float = SNAKE_SPEED var SNAKE_ROT_SPEED: float = 300.0 var SNAKE_POSITION_UPDATE_INTERVAL: float = 0.001 # this usually corresponds to the sprite size var SNAKE_SEGMENT_SIZE: float = 6.0 var POINTS_TO_GROW: int = 10 + +var MAX_BASIC_FOOD: int = 30 diff --git a/src/project.godot b/src/project.godot index 5ea911a..eb70498 100644 --- a/src/project.godot +++ b/src/project.godot @@ -44,10 +44,25 @@ _global_script_classes=[ { "language": "GDScript", "path": "res://ui/main_menu/scripts/menu_option.gd" }, { +"base": "MarginContainer", +"class": "ProgressionLabel", +"language": "GDScript", +"path": "res://ui/stats_display/scripts/progression_label.gd" +}, { +"base": "MarginContainer", +"class": "ScoreLabel", +"language": "GDScript", +"path": "res://tools/score_manager/scripts/score_label.gd" +}, { "base": "Node2D", "class": "Snake", "language": "GDScript", "path": "res://entities/actors/snake/scripts/snake.gd" +}, { +"base": "Node", +"class": "Stats", +"language": "GDScript", +"path": "res://stats.gd" } ] _global_script_class_icons={ "Food": "", @@ -57,7 +72,10 @@ _global_script_class_icons={ "GifRecorder": "res://addons/GifMaker/gif.svg", "GifRectangle": "res://addons/GifMaker/GifRectangle.svg", "MenuOption": "", -"Snake": "" +"ProgressionLabel": "", +"ScoreLabel": "", +"Snake": "", +"Stats": "" } [application] @@ -70,6 +88,7 @@ config/icon="res://icons/game.png" Event="*res://event.gd" Global="*res://global.gd" +SaveData="*res://save_data.gd" [debug] diff --git a/src/save_data.gd b/src/save_data.gd new file mode 100644 index 0000000..cbb3ed8 --- /dev/null +++ b/src/save_data.gd @@ -0,0 +1,42 @@ +extends Node + +const DATA_PATH: String = "user://data.save" + +var _stats: Stats + + +func _ready() -> void: + _load_data() + + +# called when setting "stats" and thus saving +func save_data(stats: Stats) -> void: + _stats = stats + var file: File = File.new() + file.open(DATA_PATH, File.WRITE) + file.store_line(to_json(_stats.get_stats())) + file.close() + + +func get_stats() -> Stats: + return _stats + + +func _load_data() -> void: + # create an empty file if not present to avoid error while loading settings + _handle_new_file() + + var file = File.new() + file.open(DATA_PATH, File.READ) + _stats = Stats.new() + _stats.set_stats(parse_json(file.get_line())) + file.close() + + +func _handle_new_file() -> void: + var file: File = File.new() + if not file.file_exists(DATA_PATH): + file.open(DATA_PATH, File.WRITE) + _stats = Stats.new() + file.store_line(to_json(_stats.get_stats())) + file.close() diff --git a/src/stats.gd b/src/stats.gd new file mode 100644 index 0000000..6906221 --- /dev/null +++ b/src/stats.gd @@ -0,0 +1,51 @@ +class_name Stats +extends Node + +# main +var points: int = 0 +var segments: int = 0 + +# track of trait points +var dash_points: int = 0 +var slow_points: int = 0 +var jump_points: int = 0 + +# times trait achieved +var dash_segments: int = 0 +var slow_segments: int = 0 +var jump_segments: int = 0 + +# trait properties +var dash_percentage: float = 0.0 +var slow_percentage: float = 0.0 +var jump_lenght: float = 0.0 + +# trait active +var trait_dash: bool = false +var trait_slow: bool = false +var trait_jump: bool = false + + +func get_stats() -> Dictionary: + return { + "points": points, + "segments": segments, + "dash_points": dash_points, + "slow_points": slow_points, + "jump_points": jump_points, + "dash_segments": dash_segments, + "slow_segments": slow_segments, + "jump_segments": jump_segments, + "dash_percentage": dash_percentage, + "slow_percentage": slow_percentage, + "jump_lenght": jump_lenght, + "trait_dash": trait_dash, + "trait_slow": trait_slow, + "trait_jump": trait_jump + } + + +func set_stats(stats: Dictionary) -> void: + var current_stats: Dictionary = get_stats() + for i in stats: + current_stats[i] = stats[i] diff --git a/src/themes/Theme.tres b/src/themes/Theme.tres new file mode 100644 index 0000000..50f97b6 --- /dev/null +++ b/src/themes/Theme.tres @@ -0,0 +1,31 @@ +[gd_resource type="Theme" load_steps=6 format=2] + +[ext_resource path="res://fonts/MonogramExtended.tres" type="DynamicFont" id=1] +[ext_resource path="res://themes/style_box_texture.png" type="Texture" id=2] +[ext_resource path="res://themes/style_box_texture_1x1.png" type="Texture" id=3] + +[sub_resource type="StyleBoxTexture" id=2] +texture = ExtResource( 3 ) +region_rect = Rect2( 0, 0, 1, 1 ) +margin_left = 1.0 +margin_right = 1.0 +margin_top = 1.0 +margin_bottom = 1.0 +expand_margin_left = 1.0 +expand_margin_right = 1.0 + +[sub_resource type="StyleBoxTexture" id=1] +texture = ExtResource( 2 ) +region_rect = Rect2( 0, 0, 3, 3 ) +margin_left = 1.0 +margin_right = 1.0 +margin_top = 1.0 +margin_bottom = 1.0 +expand_margin_left = 1.0 +expand_margin_right = 1.0 + +[resource] +default_font = ExtResource( 1 ) +HSeparator/constants/separation = 1 +HSeparator/styles/separator = SubResource( 2 ) +PanelContainer/styles/panel = SubResource( 1 ) diff --git a/src/themes/style_box_texture.png b/src/themes/style_box_texture.png Binary files differnew file mode 100644 index 0000000..5dee8c9 --- /dev/null +++ b/src/themes/style_box_texture.png diff --git a/src/themes/style_box_texture.png.import b/src/themes/style_box_texture.png.import new file mode 100644 index 0000000..1c8b969 --- /dev/null +++ b/src/themes/style_box_texture.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/style_box_texture.png-702252c4667eff8d422d0ab55d194d60.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://themes/style_box_texture.png" +dest_files=[ "res://.import/style_box_texture.png-702252c4667eff8d422d0ab55d194d60.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/themes/style_box_texture_1x1.png b/src/themes/style_box_texture_1x1.png Binary files differnew file mode 100644 index 0000000..e8b825e --- /dev/null +++ b/src/themes/style_box_texture_1x1.png diff --git a/src/themes/style_box_texture_1x1.png.import b/src/themes/style_box_texture_1x1.png.import new file mode 100644 index 0000000..ad601c9 --- /dev/null +++ b/src/themes/style_box_texture_1x1.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/style_box_texture_1x1.png-240c3465add053b3f26b16dcbc34223b.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://themes/style_box_texture_1x1.png" +dest_files=[ "res://.import/style_box_texture_1x1.png-240c3465add053b3f26b16dcbc34223b.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/tools/score_manager/ScoreManager.tscn b/src/tools/score_manager/ScoreManager.tscn deleted file mode 100644 index e54e089..0000000 --- a/src/tools/score_manager/ScoreManager.tscn +++ /dev/null @@ -1,8 +0,0 @@ -[gd_scene load_steps=3 format=2] - -[ext_resource path="res://tools/score_manager/score_manager.gd" type="Script" id=1] -[ext_resource path="res://fonts/MonogramExtended.tres" type="DynamicFont" id=2] - -[node name="ScoreManager" type="Node2D"] -script = ExtResource( 1 ) -TEXT_FONT = ExtResource( 2 ) diff --git a/src/tools/score_manager/scenes/ScoreLabel.tscn b/src/tools/score_manager/scenes/ScoreLabel.tscn new file mode 100644 index 0000000..365aff1 --- /dev/null +++ b/src/tools/score_manager/scenes/ScoreLabel.tscn @@ -0,0 +1,46 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://fonts/MonogramExtended.tres" type="DynamicFont" id=1] +[ext_resource path="res://tools/score_manager/scripts/score_label.gd" type="Script" id=2] +[ext_resource path="res://ui/stats_display/sprites/sep_2x2.png" type="Texture" id=3] + +[node name="ScoreLabel" type="MarginContainer"] +margin_bottom = 13.0 +script = ExtResource( 2 ) + +[node name="Hbox" type="HBoxContainer" parent="."] +margin_right = 16.0 +margin_bottom = 13.0 +custom_constants/separation = 2 + +[node name="Label" type="Label" parent="Hbox"] +margin_right = 12.0 +margin_bottom = 13.0 +custom_colors/font_color_shadow = Color( 0, 0, 0, 1 ) +custom_fonts/font = ExtResource( 1 ) +text = "x1" + +[node name="Timer" type="Timer" parent="Hbox/Label"] +one_shot = true + +[node name="Center" type="CenterContainer" parent="Hbox"] +margin_left = 14.0 +margin_right = 16.0 +margin_bottom = 13.0 + +[node name="VBox" type="VBoxContainer" parent="Hbox/Center"] +margin_top = 5.0 +margin_right = 2.0 +margin_bottom = 7.0 +custom_constants/separation = 0 + +[node name="Sep" type="TextureRect" parent="Hbox/Center/VBox"] +margin_right = 2.0 +margin_bottom = 2.0 +texture = ExtResource( 3 ) + +[node name="TextureRect" type="TextureRect" parent="Hbox/Center/VBox"] +visible = false +margin_top = 4.0 +margin_right = 4.0 +margin_bottom = 4.0 diff --git a/src/tools/score_manager/scenes/ScoreManager.tscn b/src/tools/score_manager/scenes/ScoreManager.tscn new file mode 100644 index 0000000..c80439b --- /dev/null +++ b/src/tools/score_manager/scenes/ScoreManager.tscn @@ -0,0 +1,8 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://tools/score_manager/scripts/score_manager.gd" type="Script" id=1] +[ext_resource path="res://tools/score_manager/scenes/ScoreLabel.tscn" type="PackedScene" id=4] + +[node name="ScoreManager" type="Node2D"] +script = ExtResource( 1 ) +SCORE_LABEL = ExtResource( 4 ) diff --git a/src/tools/score_manager/score_manager.gd b/src/tools/score_manager/score_manager.gd deleted file mode 100644 index 63c78a7..0000000 --- a/src/tools/score_manager/score_manager.gd +++ /dev/null @@ -1,48 +0,0 @@ -extends Node - -export(Resource) var TEXT_FONT: Resource - -var score: int = 0 -var growth: int = 0 - -var point_text_time: float = 2.0 -var _point_text_fmt: String = "+%s" - - -func _ready(): - Event.connect("food_eaten", self, "_on_food_eaten") - - -func _on_food_eaten(properties: Dictionary) -> void: - var points: int = properties["points"] - var location: Vector2 = properties["global_position"] - _process_points(points) - _spawn_added_score_text(points, location) - - -func _process_points(points: int) -> void: - var score_to_grow: int = (growth + 1) * Global.POINTS_TO_GROW - score - var amount_to_grow: int = 0 - var growth_progress: int - score += points - if points >= score_to_grow: - amount_to_grow += 1 - points -= score_to_grow - # maybe be careful with this - amount_to_grow += points / Global.POINTS_TO_GROW - growth += amount_to_grow - Event.emit_signal("snake_add_new_segment", amount_to_grow) - - growth_progress = Global.POINTS_TO_GROW - ((growth + 1) * Global.POINTS_TO_GROW - score) - Event.emit_signal("snake_growth_progress", growth_progress) - - -func _spawn_added_score_text(points: int, location: Vector2) -> void: - var label: Label = Label.new() - label.text = _point_text_fmt % points - label.add_color_override("font_color", Color.red) - label.add_font_override("font", TEXT_FONT) - label.set_global_position(location) - add_child(label) - yield(get_tree().create_timer(point_text_time), "timeout") - remove_child(label) diff --git a/src/tools/score_manager/scripts/score_label.gd b/src/tools/score_manager/scripts/score_label.gd new file mode 100644 index 0000000..f1ccb9d --- /dev/null +++ b/src/tools/score_manager/scripts/score_label.gd @@ -0,0 +1,42 @@ +class_name ScoreLabel +extends MarginContainer + +onready var label: Label = $Hbox/Label +onready var timer: Timer = $Hbox/Label/Timer +onready var texture_rect: TextureRect = $Hbox/Center/VBox/TextureRect + +enum Type { + BODY_SEGMENT +} + +var texture: Dictionary = { + Type.BODY_SEGMENT: preload("res://ui/hud/progress_bars/sprites/grow_progress_icon.png") +} + +var alive_time: float = 2.0 +var fmt: String = "x%s" +var points: int + + +func _ready(): + timer.connect("timeout", self, "_on_timer_timout") + timer.wait_time = 2.0 + timer.start() + + +func set_properties(_points: int, color: Color, location: Vector2, type: int=-1) -> void: + print("Constructing") + points = _points + label.text = fmt % points + label.add_color_override("font_color", color) + set_global_position(location) + if type != -1: + texture_rect.visible = true + match type: + Type.BODY_SEGMENT: + texture_rect.texture = texture[type] + label.update() + + +func _on_timer_timout() -> void: + queue_free() diff --git a/src/tools/score_manager/scripts/score_manager.gd b/src/tools/score_manager/scripts/score_manager.gd new file mode 100644 index 0000000..27a8dd4 --- /dev/null +++ b/src/tools/score_manager/scripts/score_manager.gd @@ -0,0 +1,67 @@ +extends Node + +export(PackedScene) var SCORE_LABEL: PackedScene + +var initial_stats: Stats = SaveData.get_stats() +var stats: Stats = Stats.new() +var mutation_stats: Array = [ + Stats.new(), + Stats.new(), + Stats.new() +] + +var last_snake_pos: Vector2 = Vector2.ZERO + + +func _ready(): + Event.connect("game_over", self, "_on_game_over") + Event.connect("food_eaten", self, "_on_food_eaten") + Event.connect("snake_path_new_point", self, "_on_snake_path_new_point") + + +func _on_food_eaten(properties: Dictionary) -> void: + var points: int = properties["points"] + var location: Vector2 = properties["global_position"] + var amount_to_grow: int = _process_points(points) + _spawn_added_score_text(points, location) + _spawn_added_segment_text(amount_to_grow) + + +func _process_points(points: int) -> int: + var score_to_grow: int = (stats.segments + 1) * Global.POINTS_TO_GROW - stats.points + var amount_to_grow: int = 0 + var growth_progress: int + stats.points += points + if points >= score_to_grow: + amount_to_grow += 1 + points -= score_to_grow + # maybe be careful with this + amount_to_grow += points / Global.POINTS_TO_GROW + stats.segments += amount_to_grow + Event.emit_signal("snake_add_new_segment", amount_to_grow) + + growth_progress = Global.POINTS_TO_GROW - ((stats.segments + 1) * Global.POINTS_TO_GROW - stats.points) + Event.emit_signal("snake_growth_progress", growth_progress) + return amount_to_grow + + +func _spawn_added_score_text(points: int, location: Vector2) -> void: + var label: ScoreLabel = SCORE_LABEL.instance() + add_child(label) + label.set_properties(points, Color.red, location) + + +func _spawn_added_segment_text(amount: int) -> void: + if amount > 0: + var label: ScoreLabel = SCORE_LABEL.instance() + add_child(label) + label.set_properties(amount, Color.green, last_snake_pos, ScoreLabel.Type.BODY_SEGMENT) + + +func _on_snake_path_new_point(coordinates: Vector2) -> void: + last_snake_pos = coordinates + + +func _on_game_over() -> void: + print("Show comparison.") + Event.emit_signal("display_stats", initial_stats, stats, mutation_stats) diff --git a/src/ui/main_menu/scenes/MainMenu.tscn b/src/ui/main_menu/scenes/MainMenu.tscn index 249885e..9bce7f0 100644 --- a/src/ui/main_menu/scenes/MainMenu.tscn +++ b/src/ui/main_menu/scenes/MainMenu.tscn @@ -21,31 +21,31 @@ margin_right = 320.0 margin_bottom = 180.0 [node name="VBox" type="VBoxContainer" parent="Center"] -margin_left = 64.0 +margin_left = 100.0 margin_top = 28.0 -margin_right = 256.0 +margin_right = 220.0 margin_bottom = 152.0 custom_constants/separation = 16 [node name="Title" type="CenterContainer" parent="Center/VBox"] -margin_right = 192.0 +margin_right = 120.0 margin_bottom = 52.0 [node name="Label" type="Label" parent="Center/VBox/Title"] -margin_right = 192.0 +margin_right = 120.0 margin_bottom = 52.0 custom_colors/font_color_shadow = Color( 0, 0, 0, 1 ) custom_fonts/font = SubResource( 1 ) -text = "El Snake" +text = "Snake" [node name="Options" type="CenterContainer" parent="Center/VBox"] margin_top = 68.0 -margin_right = 192.0 +margin_right = 120.0 margin_bottom = 124.0 [node name="VBox" type="VBoxContainer" parent="Center/VBox/Options"] -margin_left = 56.0 -margin_right = 136.0 +margin_left = 20.0 +margin_right = 100.0 margin_bottom = 56.0 [node name="Start" parent="Center/VBox/Options/VBox" instance=ExtResource( 1 )] diff --git a/src/ui/stats_display/scenes/ProgressionLabel.tscn b/src/ui/stats_display/scenes/ProgressionLabel.tscn new file mode 100644 index 0000000..bc7aef8 --- /dev/null +++ b/src/ui/stats_display/scenes/ProgressionLabel.tscn @@ -0,0 +1,50 @@ +[gd_scene load_steps=5 format=2] + +[ext_resource path="res://ui/stats_display/sprites/sep_8x8.png" type="Texture" id=1] +[ext_resource path="res://themes/Theme.tres" type="Theme" id=2] +[ext_resource path="res://ui/stats_display/scripts/progression_label.gd" type="Script" id=3] +[ext_resource path="res://ui/stats_display/sprites/sep_2x2.png" type="Texture" id=4] + +[node name="ProgressionLabel" type="MarginContainer"] +theme = ExtResource( 2 ) +script = ExtResource( 3 ) + +[node name="HBox" type="HBoxContainer" parent="."] +margin_right = 74.0 +margin_bottom = 13.0 +custom_constants/separation = 0 + +[node name="Center" type="CenterContainer" parent="HBox"] +margin_right = 8.0 +margin_bottom = 13.0 + +[node name="VBox" type="VBoxContainer" parent="HBox/Center"] +margin_top = 1.0 +margin_right = 8.0 +margin_bottom = 11.0 +custom_constants/separation = 0 + +[node name="Sep" type="TextureRect" parent="HBox/Center/VBox"] +margin_right = 8.0 +margin_bottom = 2.0 +texture = ExtResource( 4 ) + +[node name="TextureRect" type="TextureRect" parent="HBox/Center/VBox"] +margin_top = 2.0 +margin_right = 8.0 +margin_bottom = 10.0 +texture = ExtResource( 1 ) + +[node name="Prefix" type="Label" parent="HBox"] +margin_left = 8.0 +margin_right = 44.0 +margin_bottom = 13.0 +custom_colors/font_color_shadow = Color( 0, 0, 0, 1 ) +text = "Prefix" + +[node name="Stats" type="Label" parent="HBox"] +margin_left = 44.0 +margin_right = 74.0 +margin_bottom = 13.0 +custom_colors/font_color_shadow = Color( 0, 0, 0, 1 ) +text = "Stats" diff --git a/src/ui/stats_display/scenes/StatsDisplay.tscn b/src/ui/stats_display/scenes/StatsDisplay.tscn new file mode 100644 index 0000000..9b86a55 --- /dev/null +++ b/src/ui/stats_display/scenes/StatsDisplay.tscn @@ -0,0 +1,52 @@ +[gd_scene load_steps=5 format=2] + +[ext_resource path="res://fonts/MonogramExtended.tres" type="DynamicFont" id=1] +[ext_resource path="res://themes/Theme.tres" type="Theme" id=2] +[ext_resource path="res://ui/stats_display/scenes/ProgressionLabel.tscn" type="PackedScene" id=3] +[ext_resource path="res://ui/stats_display/scripts/stats_display.gd" type="Script" id=4] + +[node name="StatsDisplay" type="MarginContainer"] +anchor_right = 1.0 +anchor_bottom = 1.0 +theme = ExtResource( 2 ) +script = ExtResource( 4 ) +PROGRESSION_LABEL = ExtResource( 3 ) + +[node name="Center" type="CenterContainer" parent="."] +margin_right = 320.0 +margin_bottom = 180.0 + +[node name="HBox" type="HBoxContainer" parent="Center"] +margin_left = 126.0 +margin_top = 82.0 +margin_right = 194.0 +margin_bottom = 98.0 + +[node name="ProgressionPanel" type="PanelContainer" parent="Center/HBox"] +margin_right = 68.0 +margin_bottom = 16.0 + +[node name="VBox" type="VBoxContainer" parent="Center/HBox/ProgressionPanel"] +margin_left = 1.0 +margin_top = 1.0 +margin_right = 67.0 +margin_bottom = 15.0 +custom_constants/separation = 0 + +[node name="Label" type="Label" parent="Center/HBox/ProgressionPanel/VBox"] +margin_right = 66.0 +margin_bottom = 13.0 +custom_colors/font_color_shadow = Color( 0, 0, 0, 1 ) +custom_fonts/font = ExtResource( 1 ) +text = "Progression" + +[node name="HSeparator" type="HSeparator" parent="Center/HBox/ProgressionPanel/VBox"] +margin_top = 13.0 +margin_right = 66.0 +margin_bottom = 14.0 + +[node name="VBox" type="VBoxContainer" parent="Center/HBox/ProgressionPanel/VBox"] +margin_top = 14.0 +margin_right = 66.0 +margin_bottom = 14.0 +custom_constants/separation = 0 diff --git a/src/ui/stats_display/scripts/progression_label.gd b/src/ui/stats_display/scripts/progression_label.gd new file mode 100644 index 0000000..d4a0da0 --- /dev/null +++ b/src/ui/stats_display/scripts/progression_label.gd @@ -0,0 +1,48 @@ +class_name ProgressionLabel +extends MarginContainer + +enum Type { + BODY_SEGMENT, + EMPTY +} + +export(int) var TYPE: int = Type.EMPTY +export(String) var PREFIX: String +export(float) var BEFORE_VALUE: float +export(float) var AFTER_VALUE: float = -1.0 + +onready var label_prefix: Label = $HBox/Prefix +onready var label_stats: Label = $HBox/Stats +onready var texture_rect: TextureRect = $HBox/Center/VBox/TextureRect + + +var textures: Dictionary = { + Type.BODY_SEGMENT: preload("res://ui/hud/progress_bars/sprites/grow_progress_icon.png"), + Type.EMPTY: preload("res://ui/stats_display/sprites/sep_8x8.png") +} + +var _fmt: String = "%s->%s" +var _fmt_partial: String = "%s" +var _fmt_prefix: String = " %s " + + +func _ready() -> void: + set_properties(TYPE, PREFIX, BEFORE_VALUE, AFTER_VALUE) + + +func set_properties(icon: int, prefix: String, before: float, after: float=-1.0) -> void: + texture_rect.texture = textures[icon] + if after == -1.0: + label_stats.text =_fmt_partial % before + else: + label_stats.text =_fmt % [before, after] + if after > before: + label_stats.add_color_override("font_color", Color.green) + elif after < before: + label_stats.add_color_override("font_color", Color.red) + if not prefix.empty(): + label_prefix.text = _fmt_prefix % prefix + else: + label_prefix.text = " " + label_prefix.update() + label_stats.update() diff --git a/src/ui/stats_display/scripts/stats_display.gd b/src/ui/stats_display/scripts/stats_display.gd new file mode 100644 index 0000000..f1f77ef --- /dev/null +++ b/src/ui/stats_display/scripts/stats_display.gd @@ -0,0 +1,80 @@ +extends MarginContainer + +export(PackedScene) var PROGRESSION_LABEL: PackedScene + +onready var label_parent: VBoxContainer = $Center/HBox/ProgressionPanel/VBox/VBox + + +func _ready(): + Event.connect("display_stats", self, "_on_display_stats") + visible = false + + +func _on_display_stats(old_stats: Stats, new_stats: Stats, mut_stats: Array) -> void: + visible = true + var old_stats_dict: Dictionary = old_stats.get_stats() + var new_stats_dict: Dictionary = new_stats.get_stats() + var mut_stats_dict: Array = [] + for i in mut_stats: + mut_stats_dict.append(i) + print(i) + + var type: int + var prefix: String + var should_print: bool + + for stat_name in old_stats_dict: + should_print = _should_print(stat_name, new_stats_dict) + if should_print: + type = _get_type(stat_name) + prefix = _get_prefix(stat_name) + var _temp_label: ProgressionLabel = PROGRESSION_LABEL.instance() + label_parent.add_child(_temp_label) + _temp_label.set_properties(type, prefix, old_stats_dict[stat_name], new_stats_dict[stat_name]) + + +func _get_type(stat_name: String) -> int: + var type: int + match stat_name: + "segments": + type = ProgressionLabel.Type.BODY_SEGMENT + _: + type = ProgressionLabel.Type.EMPTY + return type + + +func _get_prefix(stat_name: String) -> String: + var prefix: String + match stat_name: + "points": + prefix = "points" + _: + prefix = "" + return prefix + + +func _should_print(stat_name: String, stats: Dictionary) -> bool: + var to_print: Array = [ + "points", + "segments", + ] + var to_print_check: Array = [ + "dash_percentage", + "slow_percentage", + "jump_lenght" + ] + var checks: Dictionary = { + to_print_check[0]: stats["trait_dash"], + to_print_check[1]: stats["trait_slow"], + to_print_check[2]: stats["trait_jump"] + } + + if to_print.has(stat_name): + return true + elif to_print_check.has(stat_name): + if checks[stat_name]: + return true + else: + return false + else: + return false diff --git a/src/ui/stats_display/sprites/sep_16x16.png b/src/ui/stats_display/sprites/sep_16x16.png Binary files differnew file mode 100644 index 0000000..5e4988e --- /dev/null +++ b/src/ui/stats_display/sprites/sep_16x16.png diff --git a/src/ui/stats_display/sprites/sep_16x16.png.import b/src/ui/stats_display/sprites/sep_16x16.png.import new file mode 100644 index 0000000..af73be3 --- /dev/null +++ b/src/ui/stats_display/sprites/sep_16x16.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/sep_16x16.png-07358072cc2f69acd36c02f177ac1acb.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://ui/stats_display/sprites/sep_16x16.png" +dest_files=[ "res://.import/sep_16x16.png-07358072cc2f69acd36c02f177ac1acb.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/ui/stats_display/sprites/sep_2x2.png b/src/ui/stats_display/sprites/sep_2x2.png Binary files differnew file mode 100644 index 0000000..e17fd41 --- /dev/null +++ b/src/ui/stats_display/sprites/sep_2x2.png diff --git a/src/ui/stats_display/sprites/sep_2x2.png.import b/src/ui/stats_display/sprites/sep_2x2.png.import new file mode 100644 index 0000000..db8cfc0 --- /dev/null +++ b/src/ui/stats_display/sprites/sep_2x2.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/sep_2x2.png-723a79374f3362fd763c297ceaabe580.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://ui/stats_display/sprites/sep_2x2.png" +dest_files=[ "res://.import/sep_2x2.png-723a79374f3362fd763c297ceaabe580.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/ui/stats_display/sprites/sep_4x4.png b/src/ui/stats_display/sprites/sep_4x4.png Binary files differnew file mode 100644 index 0000000..f5a21ee --- /dev/null +++ b/src/ui/stats_display/sprites/sep_4x4.png diff --git a/src/ui/stats_display/sprites/sep_4x4.png.import b/src/ui/stats_display/sprites/sep_4x4.png.import new file mode 100644 index 0000000..e46103f --- /dev/null +++ b/src/ui/stats_display/sprites/sep_4x4.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/sep_4x4.png-df075ccb421f59c1ef36d42098338d8f.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://ui/stats_display/sprites/sep_4x4.png" +dest_files=[ "res://.import/sep_4x4.png-df075ccb421f59c1ef36d42098338d8f.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/ui/stats_display/sprites/sep_8x8.png b/src/ui/stats_display/sprites/sep_8x8.png Binary files differnew file mode 100644 index 0000000..01220e2 --- /dev/null +++ b/src/ui/stats_display/sprites/sep_8x8.png diff --git a/src/ui/stats_display/sprites/sep_8x8.png.import b/src/ui/stats_display/sprites/sep_8x8.png.import new file mode 100644 index 0000000..6ca9f8a --- /dev/null +++ b/src/ui/stats_display/sprites/sep_8x8.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/sep_8x8.png-2d331c15ccc1e2264f4ae5995f2c0099.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://ui/stats_display/sprites/sep_8x8.png" +dest_files=[ "res://.import/sep_8x8.png-2d331c15ccc1e2264f4ae5995f2c0099.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 |