From 4417e481926504224860ba396fa5875a4b5aaa87 Mon Sep 17 00:00:00 2001 From: Luna Brovchuk Date: Sun, 1 Oct 2023 14:47:37 +0200 Subject: [PATCH] Fix Enemy Outline, Create Damage System --- res/shaders/colorize.tres | 19 +++++++++ res/shaders/outline_material.tres | 11 +++++ src/models/enemies/bat/Bat.tscn | 2 +- src/models/player/Player.tscn | 67 ++++++++++++++++++++++++++++++- src/models/player/player.gd | 49 ++++++++++++++-------- src/scenes/game/Game.tscn | 30 +------------- src/scenes/game/game.gd | 3 +- src/scenes/menu/menu.gd | 2 +- src/scripts/global.gd | 11 ++++- 9 files changed, 143 insertions(+), 51 deletions(-) create mode 100644 res/shaders/colorize.tres create mode 100644 res/shaders/outline_material.tres diff --git a/res/shaders/colorize.tres b/res/shaders/colorize.tres new file mode 100644 index 0000000..f7d36b5 --- /dev/null +++ b/res/shaders/colorize.tres @@ -0,0 +1,19 @@ +[gd_resource type="ShaderMaterial" load_steps=2 format=2] + +[sub_resource type="Shader" id=3] +code = "shader_type canvas_item; + +uniform bool active = false; +uniform vec4 color: hint_color = vec4(1, 1, 1, 1); + +void fragment(){ + COLOR = texture(TEXTURE, UV); + if (active) { + COLOR = vec4(COLOR.rgb + color.rgb * color.a, COLOR.a) + } +}" + +[resource] +shader = SubResource( 3 ) +shader_param/active = false +shader_param/color = Color( 1, 1, 1, 1 ) diff --git a/res/shaders/outline_material.tres b/res/shaders/outline_material.tres new file mode 100644 index 0000000..1690e17 --- /dev/null +++ b/res/shaders/outline_material.tres @@ -0,0 +1,11 @@ +[gd_resource type="ShaderMaterial" load_steps=2 format=2] + +[ext_resource path="res://res/shaders/outline.tres" type="Shader" id=1] + +[resource] +shader = ExtResource( 1 ) +shader_param/color = Color( 1, 0.870588, 0, 1 ) +shader_param/width = 1.0 +shader_param/pattern = 0 +shader_param/inside = false +shader_param/add_margins = true diff --git a/src/models/enemies/bat/Bat.tscn b/src/models/enemies/bat/Bat.tscn index 0d16b9f..e60b400 100644 --- a/src/models/enemies/bat/Bat.tscn +++ b/src/models/enemies/bat/Bat.tscn @@ -9,7 +9,7 @@ extents = Vector2( 4, 3 ) [sub_resource type="RectangleShape2D" id=2] extents = Vector2( 6, 4 ) -[node name="Bat" type="KinematicBody2D" groups=["enemy"]] +[node name="Bat" type="KinematicBody2D" groups=["bat", "enemy"]] collision_layer = 2 collision_mask = 2 script = ExtResource( 2 ) diff --git a/src/models/player/Player.tscn b/src/models/player/Player.tscn index 0318b23..7094e34 100644 --- a/src/models/player/Player.tscn +++ b/src/models/player/Player.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=5 format=2] +[gd_scene load_steps=8 format=2] [ext_resource path="res://src/models/player/sprite.png" type="Texture" id=1] [ext_resource path="res://src/models/player/player.gd" type="Script" id=2] +[ext_resource path="res://res/shaders/colorize.tres" type="Material" id=3] [sub_resource type="RectangleShape2D" id=1] extents = Vector2( 4, 5.5 ) @@ -9,10 +10,67 @@ extents = Vector2( 4, 5.5 ) [sub_resource type="RectangleShape2D" id=2] extents = Vector2( 7, 11 ) +[sub_resource type="Animation" id=4] +resource_name = "RESET" +tracks/0/type = "value" +tracks/0/path = NodePath("Sprite:material:shader_param/color") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 0, +"values": [ Color( 1, 1, 1, 1 ) ] +} +tracks/1/type = "value" +tracks/1/path = NodePath("Sprite:material:shader_param/active") +tracks/1/interp = 1 +tracks/1/loop_wrap = true +tracks/1/imported = false +tracks/1/enabled = true +tracks/1/keys = { +"times": PoolRealArray( 0 ), +"transitions": PoolRealArray( 1 ), +"update": 1, +"values": [ false ] +} + +[sub_resource type="Animation" id=3] +resource_name = "damage" +length = 0.9 +step = 0.05 +tracks/0/type = "value" +tracks/0/path = NodePath("Sprite:material:shader_param/active") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/keys = { +"times": PoolRealArray( 0, 2 ), +"transitions": PoolRealArray( 1, 1 ), +"update": 1, +"values": [ true, false ] +} +tracks/1/type = "value" +tracks/1/path = NodePath("Sprite:material:shader_param/color") +tracks/1/interp = 1 +tracks/1/loop_wrap = true +tracks/1/imported = false +tracks/1/enabled = true +tracks/1/keys = { +"times": PoolRealArray( 0, 0.15, 0.3, 0.45, 0.6, 0.75, 0.9 ), +"transitions": PoolRealArray( 8, 8, 8, 8, 8, 8, 8 ), +"update": 0, +"values": [ Color( 1, 1, 1, 0 ), Color( 1, 1, 1, 1 ), Color( 1, 1, 1, 0 ), Color( 1, 1, 1, 1 ), Color( 1, 1, 1, 0 ), Color( 1, 1, 1, 1 ), Color( 1, 1, 1, 0 ) ] +} + [node name="Player" type="KinematicBody2D" groups=["player"]] script = ExtResource( 2 ) [node name="Sprite" type="Sprite" parent="."] +material = ExtResource( 3 ) texture = ExtResource( 1 ) centered = false @@ -25,3 +83,10 @@ shape = SubResource( 1 ) [node name="CollisionShape2D" type="CollisionShape2D" parent="EnemyCollision"] position = Vector2( 12, 12 ) shape = SubResource( 2 ) + +[node name="AnimationPlayer" type="AnimationPlayer" parent="."] +autoplay = "RESET" +anims/RESET = SubResource( 4 ) +anims/damage = SubResource( 3 ) + +[connection signal="area_entered" from="EnemyCollision" to="." method="area_entered"] diff --git a/src/models/player/player.gd b/src/models/player/player.gd index ad57e33..29da4f8 100644 --- a/src/models/player/player.gd +++ b/src/models/player/player.gd @@ -1,17 +1,42 @@ extends KinematicBody2D -var velocity = Vector2.ZERO +var outline_material = load("res://res/shaders/outline_material.tres") var speed_multiplier: float = 1.0 +var invincibility_timer = .0 +var velocity = Vector2.ZERO +var invincible = false +var target: EnemyClass var slowed = false -var target var speed = 100 + func get_input(): var input_direction = Input.get_vector("left", "right", "up", "down") velocity = input_direction * speed -func _physics_process(_delta): +func choose_target(): + var least_dist = 4000 + var enemy_dist + var enemies = get_tree().get_nodes_in_group("enemy") + for enemy in enemies: + enemy.material = null + enemy_dist = global_position.distance_to(enemy.global_position) + if enemy_dist < least_dist: + least_dist = enemy_dist + target = enemy + +func area_entered(area): + var parent = area.get_parent() + if parent.is_in_group("enemy") and not parent.is_in_group("bat") and not invincible: + $AnimationPlayer.play("damage") + Global.add_health(-1) + invincible = true + invincibility_timer = .0 + +func _physics_process(delta): get_input() + invincibility_timer += delta + var areas = $EnemyCollision.get_overlapping_areas() slowed = false for area in areas: @@ -19,6 +44,9 @@ func _physics_process(_delta): slowed = true break + if invincible and invincibility_timer >= 0.9: + invincible = false + if slowed: speed_multiplier = 0.5 else: @@ -26,17 +54,4 @@ func _physics_process(_delta): velocity = move_and_slide(velocity * speed_multiplier) choose_target() - -const SHADER = preload("res://res/shaders/outline.tres") - - -func choose_target(): - var least_dist = 4000 - var enemy_dist - var enemies = get_tree().get_nodes_in_group("enemy") - for enemy in enemies: - enemy_dist = global_position.distance_to(enemy.global_position) - if enemy_dist < least_dist: - least_dist = enemy_dist - target = enemy - #enemy.set_shader_param("width", 0) + target.material = outline_material diff --git a/src/scenes/game/Game.tscn b/src/scenes/game/Game.tscn index bb04902..340653b 100644 --- a/src/scenes/game/Game.tscn +++ b/src/scenes/game/Game.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=24 format=2] +[gd_scene load_steps=20 format=2] [ext_resource path="res://src/scenes/menu/background/background.png" type="Texture" id=1] [ext_resource path="res://src/scenes/game/sprites/background_houses.png" type="Texture" id=2] @@ -15,7 +15,6 @@ [ext_resource path="res://src/models/enemies/trash_can/TrashCan.tscn" type="PackedScene" id=13] [ext_resource path="res://src/models/enemies/bat/Bat.tscn" type="PackedScene" id=14] [ext_resource path="res://src/models/enemies/ghost/Ghost.tscn" type="PackedScene" id=15] -[ext_resource path="res://res/shaders/outline.tres" type="Shader" id=16] [sub_resource type="RectangleShape2D" id=2] extents = Vector2( 10, 60 ) @@ -23,30 +22,6 @@ extents = Vector2( 10, 60 ) [sub_resource type="RectangleShape2D" id=3] extents = Vector2( 140, 10.125 ) -[sub_resource type="ShaderMaterial" id=5] -shader = ExtResource( 16 ) -shader_param/color = Color( 1, 0.870588, 0, 1 ) -shader_param/width = 1.0 -shader_param/pattern = 0 -shader_param/inside = false -shader_param/add_margins = true - -[sub_resource type="ShaderMaterial" id=6] -shader = ExtResource( 16 ) -shader_param/color = Color( 1, 0.870588, 0, 1 ) -shader_param/width = 1.0 -shader_param/pattern = 0 -shader_param/inside = false -shader_param/add_margins = true - -[sub_resource type="ShaderMaterial" id=7] -shader = ExtResource( 16 ) -shader_param/color = Color( 1, 0.870588, 0, 1 ) -shader_param/width = 1.0 -shader_param/pattern = 0 -shader_param/inside = true -shader_param/add_margins = true - [sub_resource type="DynamicFont" id=1] size = 8 font_data = ExtResource( 4 ) @@ -101,16 +76,13 @@ shape = SubResource( 3 ) [node name="TrashCan" parent="View/Enemies" instance=ExtResource( 13 )] [node name="Bat" parent="View/Enemies" instance=ExtResource( 14 )] -material = SubResource( 5 ) z_index = 1 [node name="Bat2" parent="View/Enemies" instance=ExtResource( 14 )] -material = SubResource( 6 ) position = Vector2( 142, 54 ) z_index = 1 [node name="Ghost" parent="View/Enemies" instance=ExtResource( 15 )] -material = SubResource( 7 ) position = Vector2( 509, 100 ) [node name="Player" parent="View" instance=ExtResource( 5 )] diff --git a/src/scenes/game/game.gd b/src/scenes/game/game.gd index 4f7ae8b..4e41905 100644 --- a/src/scenes/game/game.gd +++ b/src/scenes/game/game.gd @@ -1,4 +1,5 @@ extends Node func _ready(): - Global.add_health(1) + Global.health = 3 + Global.add_health(0) diff --git a/src/scenes/menu/menu.gd b/src/scenes/menu/menu.gd index 6ea9112..11ecf52 100644 --- a/src/scenes/menu/menu.gd +++ b/src/scenes/menu/menu.gd @@ -1,7 +1,7 @@ extends Node func game_start(): - get_tree().change_scene_to(Global.GAME_SCENE) + Global.game_start() func game_exit(): get_tree().quit(0) diff --git a/src/scripts/global.gd b/src/scripts/global.gd index 2e0a596..4662e83 100644 --- a/src/scripts/global.gd +++ b/src/scripts/global.gd @@ -1,12 +1,21 @@ extends Node var GAME_SCENE = preload("res://src/scenes/game/Game.tscn") +var MENU_SCENE = preload("res://src/scenes/menu/Menu.tscn") var playing = true var health = 2 +func game_start(): + playing = true + get_tree().change_scene_to(GAME_SCENE) + +func return_to_menu(): + playing = false + get_tree().change_scene_to(MENU_SCENE) + func add_health(amount): health = clamp(health + amount, 0, 3) get_tree().get_root().get_node("Game/UIWrapper/UI/Health/HealthSprite").frame = health if health == 0: - pass # TODO endgame + return_to_menu() # TODO endgame