From 95e301e24a844dfcf54ef6bded8ccfa811b97153 Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Wed, 17 Mar 2021 00:04:27 +0000 Subject: [PATCH] Add state scene to unify the state object --- AI/HitBox.gd | 4 ++++ AI/HitBox.tscn | 5 ++++- AI/Stats.gd | 15 +++++++++++++++ AI/Stats.tscn | 6 ++++++ Effects/Effects.gd | 10 ++++++++++ Enemies/Bat.gd | 6 ++++++ Enemies/Bat.tscn | 8 +++++++- Player/player.gd | 4 ++-- Player/player.tscn | 9 +++++---- Player/sword.gd | 2 +- 10 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 AI/HitBox.gd create mode 100644 AI/Stats.gd create mode 100644 AI/Stats.tscn create mode 100644 Effects/Effects.gd diff --git a/AI/HitBox.gd b/AI/HitBox.gd new file mode 100644 index 0000000..d922021 --- /dev/null +++ b/AI/HitBox.gd @@ -0,0 +1,4 @@ +extends Area2D + + +export(int) var damage diff --git a/AI/HitBox.tscn b/AI/HitBox.tscn index 7829f53..7720611 100644 --- a/AI/HitBox.tscn +++ b/AI/HitBox.tscn @@ -1,5 +1,8 @@ -[gd_scene format=2] +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://AI/HitBox.gd" type="Script" id=1] [node name="HitBox" type="Area2D"] +script = ExtResource( 1 ) [node name="CollisionShape2D" type="CollisionShape2D" parent="."] diff --git a/AI/Stats.gd b/AI/Stats.gd new file mode 100644 index 0000000..3c70faa --- /dev/null +++ b/AI/Stats.gd @@ -0,0 +1,15 @@ +extends Node + + +export(int) var max_health = 1 +onready var health = max_health setget set_health + +func set_health(v): + if v <= 0: + emit_signal("no_health") + health = 0 + else: + health = v + + +signal no_health diff --git a/AI/Stats.tscn b/AI/Stats.tscn new file mode 100644 index 0000000..26055a5 --- /dev/null +++ b/AI/Stats.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://AI/Stats.gd" type="Script" id=1] + +[node name="Stats" type="Node"] +script = ExtResource( 1 ) diff --git a/Effects/Effects.gd b/Effects/Effects.gd new file mode 100644 index 0000000..79f2f73 --- /dev/null +++ b/Effects/Effects.gd @@ -0,0 +1,10 @@ +extends AnimationSprite + +func _ready(): + self.connect("animation_finished", self, "_on_animation_finished") + frame = 0 + play("Animate") + + +func _on_animation_finished(): + queue_free() diff --git a/Enemies/Bat.gd b/Enemies/Bat.gd index 04a66de..b8fb7d0 100644 --- a/Enemies/Bat.gd +++ b/Enemies/Bat.gd @@ -1,10 +1,16 @@ extends KinematicBody2D var knockback = Vector2.ZERO +onready var stats = $Stats func _physics_process(delta): knockback = knockback.move_toward(Vector2.ZERO, 200 * delta) knockback = move_and_slide(knockback) func _on_HurtBox_area_entered(area): + stats.health -= area.damage knockback = area.knockback_vector * 120 + + +func _on_Stats_no_health(): + queue_free() diff --git a/Enemies/Bat.tscn b/Enemies/Bat.tscn index a9459e9..4815a97 100644 --- a/Enemies/Bat.tscn +++ b/Enemies/Bat.tscn @@ -1,9 +1,10 @@ -[gd_scene load_steps=13 format=2] +[gd_scene load_steps=14 format=2] [ext_resource path="res://Shadows/SmallShadow.png" type="Texture" id=1] [ext_resource path="res://Enemies/Bat.png" type="Texture" id=2] [ext_resource path="res://AI/HurtBox.tscn" type="PackedScene" id=3] [ext_resource path="res://Enemies/Bat.gd" type="Script" id=4] +[ext_resource path="res://AI/Stats.tscn" type="PackedScene" id=5] [sub_resource type="AtlasTexture" id=1] atlas = ExtResource( 2 ) @@ -47,6 +48,7 @@ script = ExtResource( 4 ) [node name="AnimatedSprite" type="AnimatedSprite" parent="."] frames = SubResource( 6 ) animation = "Fly" +frame = 2 playing = true offset = Vector2( 0, -12 ) @@ -63,6 +65,10 @@ collision_mask = 0 [node name="CollisionShape2D" parent="HurtBox" index="0"] position = Vector2( 0, -15 ) shape = SubResource( 8 ) + +[node name="Stats" parent="." instance=ExtResource( 5 )] +max_health = 2 [connection signal="area_entered" from="HurtBox" to="." method="_on_HurtBox_area_entered"] +[connection signal="no_health" from="Stats" to="." method="_on_Stats_no_health"] [editable path="HurtBox"] diff --git a/Player/player.gd b/Player/player.gd index a64c558..67ec910 100644 --- a/Player/player.gd +++ b/Player/player.gd @@ -13,9 +13,9 @@ enum { var state = MOVE var velocity = Vector2.ZERO -# At the Player scense, player has to be faced to the left for this +# At the Player scense, player has to be faced to the right for this # vector to be correct -var roll_vector = Vector2.LEFT +var roll_vector = Vector2.RIGHT onready var animation_tree = $AnimationTree onready var animation_state = animation_tree.get("parameters/playback") diff --git a/Player/player.tscn b/Player/player.tscn index cd18e95..6327fd3 100644 --- a/Player/player.tscn +++ b/Player/player.tscn @@ -636,10 +636,10 @@ anims/RunUp = SubResource( 17 ) tree_root = SubResource( 44 ) anim_player = NodePath("../AnimationPlayer") parameters/playback = SubResource( 45 ) -parameters/Attack/blend_position = Vector2( 0, 0 ) -parameters/Idle/blend_position = Vector2( 0, 0 ) -parameters/Roll/blend_position = Vector2( 0, 0 ) -parameters/Run/blend_position = Vector2( 0, 0 ) +parameters/Attack/blend_position = Vector2( 1, 0 ) +parameters/Idle/blend_position = Vector2( 1, 0 ) +parameters/Roll/blend_position = Vector2( 1, 0 ) +parameters/Run/blend_position = Vector2( 1, 0 ) [node name="HitBoxPosition" type="Position2D" parent="."] position = Vector2( 0, -5 ) @@ -649,6 +649,7 @@ position = Vector2( 16, 0 ) collision_layer = 0 collision_mask = 8 script = ExtResource( 4 ) +damage = 2 [node name="CollisionShape2D" parent="HitBoxPosition/SwordHitBox" index="0"] shape = SubResource( 46 ) diff --git a/Player/sword.gd b/Player/sword.gd index fe06184..02eda85 100644 --- a/Player/sword.gd +++ b/Player/sword.gd @@ -1,3 +1,3 @@ -extends Area2D +extends "res://AI/HitBox.gd" var knockback_vector = Vector2.ZERO