From 3e5d933e219ff47b68841cc8b62c03939581965b Mon Sep 17 00:00:00 2001 From: Luna Brovchuk Date: Sun, 1 Oct 2023 17:12:45 +0200 Subject: [PATCH] Deal Damage --- res/shaders/colorize.tres | 4 +-- src/models/bullet/Bullet.gd | 15 ++++++++++-- src/models/bullet/Bullet.tscn | 8 +++++- src/models/enemies/ghost/Ghost.tscn | 3 ++- src/models/enemies/ghost/ghost.gd | 3 +++ src/models/player/player.gd | 24 ++++++++++++------ src/scripts/enemy_class.gd | 38 +++++++++++++++++++++-------- 7 files changed, 72 insertions(+), 23 deletions(-) diff --git a/res/shaders/colorize.tres b/res/shaders/colorize.tres index f7d36b5..dcdce01 100644 --- a/res/shaders/colorize.tres +++ b/res/shaders/colorize.tres @@ -15,5 +15,5 @@ void fragment(){ [resource] shader = SubResource( 3 ) -shader_param/active = false -shader_param/color = Color( 1, 1, 1, 1 ) +shader_param/active = true +shader_param/color = Color( 1, 1, 1, 0 ) diff --git a/src/models/bullet/Bullet.gd b/src/models/bullet/Bullet.gd index bc0f701..a46fa9a 100644 --- a/src/models/bullet/Bullet.gd +++ b/src/models/bullet/Bullet.gd @@ -1,12 +1,23 @@ extends KinematicBody2D -var speed = 128 var velocity = Vector2.ZERO +var speed = 128 +var poisoned = false +var damage = 5 +var frozen = false var target func _physics_process(delta): - if target != null: + if target != null and is_instance_valid(target): var target_texture = target.get_node("Sprite").frames.get_frame("idle", 0) var target_sprite_size = Vector2(target_texture.get_width(), target_texture.get_width()) var target_center = target.global_position + (target_sprite_size / 2) self.global_position = self.global_position.move_toward(target_center, delta * speed) + +func area_entered(area): + var parent = area.get_parent() + if parent.is_in_group("enemy"): + parent.deal_damage(damage) + parent.frozen = frozen + parent.poisoned = poisoned + call_deferred("free") diff --git a/src/models/bullet/Bullet.tscn b/src/models/bullet/Bullet.tscn index ea59ef4..27c55c0 100644 --- a/src/models/bullet/Bullet.tscn +++ b/src/models/bullet/Bullet.tscn @@ -11,6 +11,12 @@ script = ExtResource( 2 ) [node name="Sprite" type="Sprite" parent="."] texture = ExtResource( 1 ) +centered = false -[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +[node name="Area2D" type="Area2D" parent="."] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"] +position = Vector2( 3.5, 3.5 ) shape = SubResource( 1 ) + +[connection signal="area_entered" from="Area2D" to="." method="area_entered"] diff --git a/src/models/enemies/ghost/Ghost.tscn b/src/models/enemies/ghost/Ghost.tscn index 29d8b9f..df1c76e 100644 --- a/src/models/enemies/ghost/Ghost.tscn +++ b/src/models/enemies/ghost/Ghost.tscn @@ -6,7 +6,7 @@ [sub_resource type="RectangleShape2D" id=1] extents = Vector2( 7, 9 ) -[node name="Ghost" type="KinematicBody2D" groups=["enemy"]] +[node name="Ghost" type="KinematicBody2D" groups=["enemy", "ghost"]] z_index = 2 script = ExtResource( 2 ) @@ -14,6 +14,7 @@ script = ExtResource( 2 ) use_parent_material = true frames = ExtResource( 1 ) animation = "idle" +frame = 1 playing = true centered = false diff --git a/src/models/enemies/ghost/ghost.gd b/src/models/enemies/ghost/ghost.gd index 046b94a..c0d7a54 100644 --- a/src/models/enemies/ghost/ghost.gd +++ b/src/models/enemies/ghost/ghost.gd @@ -5,6 +5,9 @@ var dash_target = Vector2.ZERO var dash_timer = .0 var dashing = false +func _ready(): + hp = 8 + func _physics_process(delta): var dist = global_position.distance_to(player.global_position) if dist > 25 and dash_target == Vector2.ZERO: diff --git a/src/models/player/player.gd b/src/models/player/player.gd index ea828cf..dbe2c97 100644 --- a/src/models/player/player.gd +++ b/src/models/player/player.gd @@ -72,9 +72,14 @@ func _shoot(): bullet.global_position.y = self.global_position.y - 4 func _draw(): - if target != null: + if target != null and is_instance_valid(target): var dist2enemy = position.distance_to(target.global_position) - draw_dashed_line(Vector2(12, 12), target.global_position - self.global_position + Vector2(8, 7), Color("ffde00"), 1) + var mob_offset = Vector2.ZERO + if target.is_in_group("bat"): + mob_offset = Vector2(8, 7) + if target.is_in_group("ghost"): + mob_offset = Vector2(8, 12) + draw_dashed_line(Vector2(12, 12), target.global_position - self.global_position + mob_offset, Color("ffde00"), 1) func _process(_delta): $Sprite.play("idle") if velocity == Vector2.ZERO else $Sprite.play("run") @@ -87,11 +92,9 @@ func _physics_process(delta): get_input() invincibility_timer += delta - recharge_timer += delta - if recharge_timer >= get_recharge(): - _shoot() - recharge_timer = .0 + if recharge_timer <= get_recharge(): + recharge_timer += delta var areas = $EnemyCollision.get_overlapping_areas() slowed = false @@ -110,4 +113,11 @@ func _physics_process(delta): velocity = move_and_slide(velocity * speed_multiplier) choose_target() - target.material = outline_material + + if target != null and is_instance_valid(target): + target.material = outline_material + + if recharge_timer >= get_recharge(): + _shoot() + recharge_timer = .0 + diff --git a/src/scripts/enemy_class.gd b/src/scripts/enemy_class.gd index 104fa3a..a188a8c 100644 --- a/src/scripts/enemy_class.gd +++ b/src/scripts/enemy_class.gd @@ -4,17 +4,35 @@ var hp var poisoned = false var poisoned_timer = .0 var speed_multiplier = 1 +var frozen_timer = .0 +var frozen_time = 2 +var poisoned_time = 3 +var poisoned_damage = 5 +var poison_damage_timer = .0 + var frozen = false -func check_frozen(): - speed_multiplier = 1 +func deal_damage(amount): + hp -= amount + if hp <= 0: + call_deferred("free") + +func _physics_process(delta): + if poisoned: + poisoned_timer += delta + poison_damage_timer += delta + if poisoned_timer >= poisoned_time: + poisoned_timer = .0 + poison_damage_timer = .0 + poisoned = false + if poison_damage_timer >= 1: + deal_damage(poisoned_damage) + if frozen: + frozen_timer += delta speed_multiplier = 0.5 - -func check_poisoned(): - if poisoned_timer >= 1: - hp -= 5 - -func _physics_process(_delta): - check_frozen() - check_poisoned() + if frozen_timer >= frozen_time: + frozen_time = 0 + frozen = false + else: + speed_multiplier = 1