diff --git a/assets/enemies/slime/slime_purple.png b/assets/enemies/slime/slime_purple.png new file mode 100644 index 0000000..76f564e Binary files /dev/null and b/assets/enemies/slime/slime_purple.png differ diff --git a/assets/enemies/slime/slime_purple.png.import b/assets/enemies/slime/slime_purple.png.import new file mode 100644 index 0000000..815a35d --- /dev/null +++ b/assets/enemies/slime/slime_purple.png.import @@ -0,0 +1,40 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://0p6a5anngp31" +path="res://.godot/imported/slime_purple.png-4674ec1226b290767ac60aef13cd0376.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://assets/enemies/slime/slime_purple.png" +dest_files=["res://.godot/imported/slime_purple.png-4674ec1226b290767ac60aef13cd0376.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/uastc_level=0 +compress/rdo_quality_loss=0.0 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/channel_remap/red=0 +process/channel_remap/green=1 +process/channel_remap/blue=2 +process/channel_remap/alpha=3 +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 diff --git a/scenes/game.tscn b/scenes/game.tscn index cb91f73..32fabc4 100644 --- a/scenes/game.tscn +++ b/scenes/game.tscn @@ -8,6 +8,7 @@ [ext_resource type="Texture2D" uid="uid://c8jhlduxe8p77" path="res://assets/graphics/terrains/veg_32x32.png" id="4_gee14"] [ext_resource type="PackedScene" uid="uid://dtegcgc8xcxfg" path="res://scenes/player.tscn" id="4_iywne"] [ext_resource type="Texture2D" uid="uid://bu8oc3yypgigw" path="res://assets/graphics/terrains/mushroom_32x64.png" id="5_0tnpc"] +[ext_resource type="PackedScene" uid="uid://6d5378om23b8" path="res://scenes/slime.tscn" id="9_0tnpc"] [sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_u5sy4"] texture = ExtResource("3_p57ef") @@ -441,4 +442,9 @@ effect = "teleport" [node name="Crate" parent="." unique_id=1367266345 instance=ExtResource("3_iywne")] position = Vector2(635, 553) +[node name="Enemies" type="Node2D" parent="." unique_id=1540811431] + +[node name="Slime" parent="Enemies" unique_id=378236608 instance=ExtResource("9_0tnpc")] +position = Vector2(693, 407) + [connection signal="areaTriggered" from="Triggers/Trigger" to="." method="_on_trigger"] diff --git a/scenes/slime.tscn b/scenes/slime.tscn new file mode 100644 index 0000000..dbfa63d --- /dev/null +++ b/scenes/slime.tscn @@ -0,0 +1,137 @@ +[gd_scene format=3 uid="uid://6d5378om23b8"] + +[ext_resource type="Texture2D" uid="uid://0p6a5anngp31" path="res://assets/enemies/slime/slime_purple.png" id="1_371uh"] +[ext_resource type="Script" uid="uid://c83er3jdb7i8d" path="res://scripts/slime.gd" id="1_p2gj0"] + +[sub_resource type="AtlasTexture" id="AtlasTexture_fd6lc"] +atlas = ExtResource("1_371uh") +region = Rect2(0, 48, 24, 24) + +[sub_resource type="AtlasTexture" id="AtlasTexture_gstla"] +atlas = ExtResource("1_371uh") +region = Rect2(24, 48, 24, 24) + +[sub_resource type="AtlasTexture" id="AtlasTexture_v5wyi"] +atlas = ExtResource("1_371uh") +region = Rect2(48, 48, 24, 24) + +[sub_resource type="AtlasTexture" id="AtlasTexture_0l8pv"] +atlas = ExtResource("1_371uh") +region = Rect2(72, 48, 24, 24) + +[sub_resource type="AtlasTexture" id="AtlasTexture_p2gj0"] +atlas = ExtResource("1_371uh") +region = Rect2(0, 24, 24, 24) + +[sub_resource type="AtlasTexture" id="AtlasTexture_n6pvg"] +atlas = ExtResource("1_371uh") +region = Rect2(24, 24, 24, 24) + +[sub_resource type="AtlasTexture" id="AtlasTexture_pjw23"] +atlas = ExtResource("1_371uh") +region = Rect2(48, 24, 24, 24) + +[sub_resource type="AtlasTexture" id="AtlasTexture_2npkn"] +atlas = ExtResource("1_371uh") +region = Rect2(72, 24, 24, 24) + +[sub_resource type="AtlasTexture" id="AtlasTexture_vnioy"] +atlas = ExtResource("1_371uh") +region = Rect2(0, 0, 24, 24) + +[sub_resource type="AtlasTexture" id="AtlasTexture_2xjuo"] +atlas = ExtResource("1_371uh") +region = Rect2(24, 0, 24, 24) + +[sub_resource type="AtlasTexture" id="AtlasTexture_a02ik"] +atlas = ExtResource("1_371uh") +region = Rect2(48, 0, 24, 24) + +[sub_resource type="AtlasTexture" id="AtlasTexture_pu8ps"] +atlas = ExtResource("1_371uh") +region = Rect2(72, 0, 24, 24) + +[sub_resource type="SpriteFrames" id="SpriteFrames_mvphy"] +animations = [{ +"frames": [{ +"duration": 1.0, +"texture": SubResource("AtlasTexture_fd6lc") +}, { +"duration": 1.0, +"texture": SubResource("AtlasTexture_gstla") +}, { +"duration": 1.0, +"texture": SubResource("AtlasTexture_v5wyi") +}, { +"duration": 1.0, +"texture": SubResource("AtlasTexture_0l8pv") +}], +"loop": 0, +"name": &"attack", +"speed": 12.0 +}, { +"frames": [{ +"duration": 1.0, +"texture": SubResource("AtlasTexture_p2gj0") +}, { +"duration": 1.0, +"texture": SubResource("AtlasTexture_n6pvg") +}, { +"duration": 1.0, +"texture": SubResource("AtlasTexture_pjw23") +}, { +"duration": 1.0, +"texture": SubResource("AtlasTexture_2npkn") +}], +"loop": 1, +"name": &"idle", +"speed": 12.0 +}, { +"frames": [{ +"duration": 1.0, +"texture": SubResource("AtlasTexture_vnioy") +}, { +"duration": 1.0, +"texture": SubResource("AtlasTexture_2xjuo") +}, { +"duration": 1.0, +"texture": SubResource("AtlasTexture_a02ik") +}, { +"duration": 1.0, +"texture": SubResource("AtlasTexture_pu8ps") +}], +"loop": 0, +"name": &"spawn", +"speed": 12.0 +}] + +[sub_resource type="CircleShape2D" id="CircleShape2D_mvphy"] + +[node name="Slime" type="Area2D" unique_id=378236608] +script = ExtResource("1_p2gj0") + +[node name="EnemyGraphic" type="AnimatedSprite2D" parent="." unique_id=20748571] +texture_filter = 1 +position = Vector2(0, -2) +sprite_frames = SubResource("SpriteFrames_mvphy") +animation = &"spawn" + +[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=1414160547] +shape = SubResource("CircleShape2D_mvphy") + +[node name="RightCast" type="RayCast2D" parent="." unique_id=2017385524] +target_position = Vector2(16, 0) + +[node name="RightDownCast" type="RayCast2D" parent="." unique_id=1117203903] +position = Vector2(8, 0) +target_position = Vector2(0, 13) + +[node name="LeftDownCast" type="RayCast2D" parent="." unique_id=1523126749] +position = Vector2(-8, 0) +target_position = Vector2(0, 13) + +[node name="LeftCast" type="RayCast2D" parent="." unique_id=224811798] +target_position = Vector2(-16, 0) + +[connection signal="body_entered" from="." to="." method="_on_body_entered"] +[connection signal="animation_finished" from="EnemyGraphic" to="." method="_on_animation_finished"] diff --git a/scripts/slime.gd b/scripts/slime.gd new file mode 100644 index 0000000..d71fbd9 --- /dev/null +++ b/scripts/slime.gd @@ -0,0 +1,82 @@ +class_name Slime extends Area2D +@onready var enemy_graphic: AnimatedSprite2D = $EnemyGraphic +@onready var right_cast: RayCast2D = $RightCast +@onready var right_down_cast: RayCast2D = $RightDownCast +@onready var left_down_cast: RayCast2D = $LeftDownCast +@onready var left_cast: RayCast2D = $LeftCast + +signal enemyAttackSignal(body, enemy) + +var speed = 100 +enum FaceDirection {RIGHT,LEFT} +var facing:FaceDirection = FaceDirection.RIGHT +var direction = 0 + +enum EnemyStates {RUN, ATTACK, SPAWN} +var currentEnemyState:EnemyStates = EnemyStates.SPAWN + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + pass # Replace with function body. + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta: float) -> void: + handleMovement(delta) + handleStates() + handleAnimation() + handleCollisions() + +func _on_body_entered(body: Node2D) -> void: + if body is Player: + print("Attack the player") + currentEnemyState = EnemyStates.ATTACK + direction = 0 + enemyAttackSignal.emit(body, self) + +func handleMovement(delta)->void: + position.x += direction*speed*delta + match facing: + FaceDirection.LEFT: + enemy_graphic.flip_h = true + FaceDirection.RIGHT: + enemy_graphic.flip_h = false + +func handleStates()->void: + match currentEnemyState: + EnemyStates.SPAWN when not direction ==0 : + print("setting state to run") + currentEnemyState = EnemyStates.RUN + + +func handleAnimation()->void: + match currentEnemyState: + EnemyStates.SPAWN: + enemy_graphic.play("spawn") + EnemyStates.RUN: + enemy_graphic.play("idle") + EnemyStates.ATTACK: + enemy_graphic.play("attack") + +func handleCollisions()->void: + if not right_down_cast.is_colliding(): + direction =-1 + facing = FaceDirection.LEFT + if not left_down_cast.is_colliding(): + direction = 1 + facing = FaceDirection.RIGHT + if right_cast.is_colliding() && not right_cast.get_collider() is Player: + direction = -1 + facing=FaceDirection.LEFT + if left_cast.is_colliding() && not left_cast.get_collider() is Player: + direction = 1 + facing=FaceDirection.RIGHT + +func _on_animation_finished() -> void: + match currentEnemyState: + EnemyStates.SPAWN: + currentEnemyState = EnemyStates.RUN + direction = 1 + EnemyStates.ATTACK: + currentEnemyState = EnemyStates.RUN + direction = (randi() % 2) * 2 - 1 diff --git a/scripts/slime.gd.uid b/scripts/slime.gd.uid new file mode 100644 index 0000000..584b910 --- /dev/null +++ b/scripts/slime.gd.uid @@ -0,0 +1 @@ +uid://c83er3jdb7i8d