From f647f5ed083a8c5ce66829404b39ccdc74b5b50a Mon Sep 17 00:00:00 2001 From: Jojackman1 Date: Sun, 25 Jan 2026 17:37:56 -0700 Subject: [PATCH] Optimised code --- Assets/Scenes/DEVSCENES/EnemyTest.tscn | 310 +++++++++++++++++++++ Assets/Scenes/DEVSCENES/IQEnemy.cs | 34 ++- Assets/Scenes/DEVSCENES/IQEnemy.tscn | 38 +-- Assets/Scripts/CommonScripts/CommonData.cs | 22 +- 4 files changed, 366 insertions(+), 38 deletions(-) create mode 100644 Assets/Scenes/DEVSCENES/EnemyTest.tscn diff --git a/Assets/Scenes/DEVSCENES/EnemyTest.tscn b/Assets/Scenes/DEVSCENES/EnemyTest.tscn new file mode 100644 index 0000000..2974230 --- /dev/null +++ b/Assets/Scenes/DEVSCENES/EnemyTest.tscn @@ -0,0 +1,310 @@ +[gd_scene load_steps=15 format=4 uid="uid://b3g0hgfb0uw6a"] + +[ext_resource type="Texture2D" uid="uid://b86fd86n5sxtr" path="res://Assets/Sprites/world_tileset.png" id="1_q0x10"] +[ext_resource type="PackedScene" uid="uid://dr8qdk10uy0o4" path="res://Assets/Scenes/Persistant/Player.tscn" id="2_shvfg"] +[ext_resource type="Script" uid="uid://ddyi5usr6poda" path="res://Assets/Scripts/CommonScripts/UI.cs" id="5_5ftcn"] +[ext_resource type="PackedScene" uid="uid://j3d4u4gjxncu" path="res://Assets/Scenes/Persistant/LevelManager.tscn" id="6_avdo2"] +[ext_resource type="Script" uid="uid://b28bv2l2asbpy" path="res://Assets/Scripts/CommonScripts/ChangeMenuInitiator.cs" id="7_vog1t"] +[ext_resource type="PackedScene" uid="uid://eknn66u8gt26" path="res://Assets/Scenes/DEVSCENES/IQEnemy.tscn" id="8_s63xy"] + +[sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_e35rp"] +texture = ExtResource("1_q0x10") +0:0/0 = 0 +0:0/0/physics_layer_0/polygon_0/points = PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8) +1:0/0 = 0 +1:0/0/physics_layer_0/polygon_0/points = PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8) +2:0/0 = 0 +3:0/0 = 0 +4:0/0 = 0 +5:0/0 = 0 +6:0/0 = 0 +7:0/0 = 0 +8:0/0 = 0 +9:0/0 = 0 +9:0/0/physics_layer_0/polygon_0/points = PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8) +10:0/0 = 0 +10:0/0/physics_layer_0/polygon_0/points = PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8) +11:0/0 = 0 +11:0/0/physics_layer_0/polygon_0/points = PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8) +0:1/0 = 0 +0:1/0/physics_layer_0/polygon_0/points = PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8) +1:1/0 = 0 +1:1/0/physics_layer_0/polygon_0/points = PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8) +2:1/0 = 0 +3:1/0 = 0 +4:1/0 = 0 +5:1/0 = 0 +6:1/0 = 0 +7:1/0 = 0 +8:1/0 = 0 +9:1/0 = 0 +10:1/0 = 0 +11:1/0 = 0 +0:2/0 = 0 +0:2/0/physics_layer_0/polygon_0/points = PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8) +1:2/0 = 0 +1:2/0/physics_layer_0/polygon_0/points = PackedVector2Array(-8, -8, 8, -8, 8, 8, -8, 8) +2:2/0 = 0 +3:2/0 = 0 +4:2/0 = 0 +5:2/0 = 0 +6:2/0 = 0 +7:2/0 = 0 +8:2/0 = 0 +9:2/0 = 0 +10:2/0 = 0 +11:2/0 = 0 +0:3/0 = 0 +1:3/0 = 0 +2:3/0 = 0 +5:3/0 = 0 +6:3/0 = 0 +7:3/0 = 0 +8:3/0 = 0 +9:3/0 = 0 +0:4/0 = 0 +1:4/0 = 0 +2:4/0 = 0 +3:4/0 = 0 +4:4/0 = 0 +5:4/0 = 0 +6:4/0 = 0 +7:4/0 = 0 +8:4/0 = 0 +9:4/0 = 0 +0:5/0 = 0 +1:5/0 = 0 +2:5/0 = 0 +3:5/0 = 0 +4:5/0 = 0 +5:5/0 = 0 +6:5/0 = 0 +7:5/0 = 0 +8:5/0 = 0 +1:6/0 = 0 +2:6/0 = 0 +3:6/0 = 0 +4:6/0 = 0 +5:6/0 = 0 +6:6/0 = 0 +7:6/0 = 0 +8:6/0 = 0 +0:7/0 = 0 +1:7/0 = 0 +3:7/0 = 0 +5:7/0 = 0 +6:7/0 = 0 +7:7/0 = 0 +8:7/0 = 0 +0:8/0 = 0 +1:8/0 = 0 +2:8/0 = 0 +3:8/0 = 0 +4:8/0 = 0 +5:8/0 = 0 +6:8/0 = 0 +7:8/0 = 0 +8:8/0 = 0 +0:9/0 = 0 +1:9/0 = 0 +2:9/0 = 0 +3:9/0 = 0 +4:9/0 = 0 +5:9/0 = 0 +6:9/0 = 0 +7:9/0 = 0 +0:10/0 = 0 +1:10/0 = 0 +2:10/0 = 0 +3:10/0 = 0 +4:10/0 = 0 +6:10/0 = 0 +0:11/0 = 0 +1:11/0 = 0 +2:11/0 = 0 +3:11/0 = 0 +4:11/0 = 0 +5:11/0 = 0 +0:12/0 = 0 +1:12/0 = 0 +2:12/0 = 0 +3:12/0 = 0 +4:12/0 = 0 +0:13/0 = 0 +1:13/0 = 0 +2:13/0 = 0 +3:13/0 = 0 +4:13/0 = 0 +5:13/0 = 0 +0:14/0 = 0 +1:14/0 = 0 +2:14/0 = 0 +3:14/0 = 0 +4:14/0 = 0 +0:15/0 = 0 +1:15/0 = 0 +2:15/0 = 0 +3:15/0 = 0 + +[sub_resource type="TileSet" id="TileSet_q0ben"] +physics_layer_0/collision_layer = 1 +sources/0 = SubResource("TileSetAtlasSource_e35rp") + +[sub_resource type="Animation" id="Animation_owkni"] +length = 0.001 +tracks/0/type = "value" +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/path = NodePath("LevelStartAssets/RichTextLabel:position") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/keys = { +"times": PackedFloat32Array(0), +"transitions": PackedFloat32Array(1), +"update": 0, +"values": [Vector2(-160, -90)] +} + +[sub_resource type="Animation" id="Animation_b6w6i"] +resource_name = "LevelStart" +length = 4.0 +tracks/0/type = "value" +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/path = NodePath("LevelStartAssets/RichTextLabel:position") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/keys = { +"times": PackedFloat32Array(0, 2, 4), +"transitions": PackedFloat32Array(0.5, 2, 1), +"update": 0, +"values": [Vector2(-540, -90), Vector2(-160, -90), Vector2(220, -90)] +} + +[sub_resource type="AnimationLibrary" id="AnimationLibrary_vp0xq"] +_data = { +&"RESET": SubResource("Animation_owkni"), +&"default": SubResource("Animation_b6w6i") +} + +[sub_resource type="Animation" id="Animation_yx27v"] +length = 0.001 +tracks/0/type = "value" +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/path = NodePath("RichTextLabel:position") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/keys = { +"times": PackedFloat32Array(0), +"transitions": PackedFloat32Array(1), +"update": 0, +"values": [Vector2(-160, -90)] +} + +[sub_resource type="Animation" id="Animation_vp0xq"] +resource_name = "default" +length = 4.0 +tracks/0/type = "value" +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/path = NodePath("RichTextLabel:position") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/keys = { +"times": PackedFloat32Array(0, 2, 4), +"transitions": PackedFloat32Array(0.5, 2, 1), +"update": 0, +"values": [Vector2(-540, -90), Vector2(-160, -90), Vector2(220, -90)] +} + +[sub_resource type="AnimationLibrary" id="AnimationLibrary_0gmdc"] +_data = { +&"RESET": SubResource("Animation_yx27v"), +&"default": SubResource("Animation_vp0xq") +} + +[node name="Level1" type="Node2D"] + +[node name="PlayArea" type="TileMapLayer" parent="."] +tile_map_data = PackedByteArray("AAD//wAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAACAAAAAAAAAAAAAAADAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAFAAAAAAAAAAAAAAAGAAAAAAAAAAAAAAAHAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAJAAAAAAAAAAAAAAAKAAAAAAAAAAAAAAAOAAAAAAAAAAAAAAAPAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAD//wEAAAAAAAEAAAAAAAEAAAAAAAEAAAABAAEAAAAAAAEAAAACAAEAAAAAAAEAAAADAAEAAAAAAAEAAAAEAAEAAAAAAAEAAAAFAAEAAAAAAAEAAAAGAAEAAAAAAAEAAAAHAAEAAAAAAAEAAAAIAAEAAAAAAAEAAAAJAAEAAAAAAAEAAAAKAAEAAAAAAAEAAAALAAEAAAAAAAAAAAAMAAEAAAAAAAAAAAANAAEAAAAAAAAAAAAOAAEAAAAAAAEAAAAPAAEAAAAAAAEAAAAQAAEAAAAAAAEAAAARAAEAAAAAAAAAAAA=") +tile_set = SubResource("TileSet_q0ben") + +[node name="Background" type="TileMapLayer" parent="."] + +[node name="Foreground" type="TileMapLayer" parent="."] + +[node name="Player" parent="." instance=ExtResource("2_shvfg")] + +[node name="Enemies" type="Node" parent="."] + +[node name="IQEnemy" parent="Enemies" instance=ExtResource("8_s63xy")] +position = Vector2(248, -10) + +[node name="StaticThreats" type="Node" parent="."] + +[node name="TextInteractables" type="Node" parent="."] + +[node name="ScreenAnimations" type="Node" parent="."] + +[node name="LevelStartAssets" type="Node2D" parent="ScreenAnimations"] +visible = false +z_index = 10 + +[node name="LevelStart" type="AnimationPlayer" parent="ScreenAnimations/LevelStartAssets"] +root_node = NodePath("../..") +libraries = { +&"": SubResource("AnimationLibrary_vp0xq") +} + +[node name="RichTextLabel" type="RichTextLabel" parent="ScreenAnimations/LevelStartAssets"] +offset_left = -160.0 +offset_top = -90.0 +offset_right = 160.0 +offset_bottom = 90.0 +pivot_offset = Vector2(160, 90) +theme_override_font_sizes/normal_font_size = 32 +text = "START!" +scroll_active = false +autowrap_trim_flags = 0 +shortcut_keys_enabled = false +horizontal_alignment = 1 +vertical_alignment = 1 + +[node name="LevelCompleteAssets" type="Node2D" parent="ScreenAnimations"] +visible = false +z_index = 10 + +[node name="LevelComplete" type="AnimationPlayer" parent="ScreenAnimations/LevelCompleteAssets"] +libraries = { +&"": SubResource("AnimationLibrary_0gmdc") +} + +[node name="RichTextLabel" type="RichTextLabel" parent="ScreenAnimations/LevelCompleteAssets"] +offset_left = -160.0 +offset_top = -90.0 +offset_right = 159.99976 +offset_bottom = 90.0 +pivot_offset = Vector2(160, 90) +theme_override_font_sizes/normal_font_size = 32 +text = "LEVEL COMPLETE!" +scroll_active = false +autowrap_trim_flags = 0 +shortcut_keys_enabled = false +horizontal_alignment = 1 +vertical_alignment = 1 + +[node name="UI" type="Node2D" parent="."] +z_index = 10 +script = ExtResource("5_5ftcn") + +[node name="StartNode" type="Node2D" parent="."] +position = Vector2(0, -15) + +[node name="EndNode" type="Node2D" parent="."] +position = Vector2(3080, 120) + +[node name="LevelManager" parent="." instance=ExtResource("6_avdo2")] + +[node name="ChangeMenuInitiator" type="Node2D" parent="LevelManager"] +script = ExtResource("7_vog1t") diff --git a/Assets/Scenes/DEVSCENES/IQEnemy.cs b/Assets/Scenes/DEVSCENES/IQEnemy.cs index 703e8db..69da7ec 100644 --- a/Assets/Scenes/DEVSCENES/IQEnemy.cs +++ b/Assets/Scenes/DEVSCENES/IQEnemy.cs @@ -1,7 +1,7 @@ using Godot; using System; -public partial class IQEnemy : RigidBody2D +public partial class IQEnemy : CharacterBody2D { // Misc float GlobalTimer; @@ -12,11 +12,14 @@ public partial class IQEnemy : RigidBody2D public float MaxHP; float RegenerationAmmount; float RegenerationInterval; + float WalkSpeed; + float JumpHeight; // Enemy Conditions float CurrentYVelocity; float PreviousYVelocity; - public bool IsGrounded; + bool GroundDetected; + bool LedgeDetected; // Enemy Abilities bool AbilityJump = false; @@ -41,6 +44,10 @@ public partial class IQEnemy : RigidBody2D else RegenerationAmmount = 1.0f; if (HasMeta("RegenerationInterval")) RegenerationInterval = (float)GetMeta("RegenerationInterval"); else RegenerationInterval = 1.0f; + if (HasMeta("WalkSpeed")) WalkSpeed = (float)GetMeta("WalkSpeed"); + else WalkSpeed = 10.0f; + if (HasMeta("JumpHeight")) JumpHeight = (float)GetMeta("JumpHeight"); + else JumpHeight = 10.0f; // Read and set stats if (HasMeta("MaxHP")) MaxHP = (float)GetMeta("MaxHP"); @@ -49,7 +56,11 @@ public partial class IQEnemy : RigidBody2D GetNode("GroundDetector").AreaEntered += OnGroundEntered; GetNode("GroundDetector").AreaExited += OnGroundExited; - IsGrounded = true; + GetNode("LedgeDetector").AreaEntered += OnLedgeEntered; + GetNode("LedgeDetector").AreaExited += OnLedgeExited; + + GroundDetected = true; + LedgeDetected = true; // Apply IQ based permenant modifiers IQ = (int)GetMeta("IQ"); // Read IQ @@ -73,9 +84,19 @@ public partial class IQEnemy : RigidBody2D if (GlobalTimer % RegenerationInterval == 0){Regenerate(RegenerationAmmount);} } + // Movement + // Add gravity. + if (GroundDetected == false){Velocity = new Vector2(Velocity[0],-1);} + if (IQ <= 20){ // Goomba ahh diddy blud + if (GroundDetected && LedgeDetected == false) {Flip("ANY");} + if (Scale[0] > 0){Velocity = new Vector2(WalkSpeed, Velocity[1]);} // Facing right + if (Scale[0] < 0){Velocity = new Vector2(-1 * WalkSpeed, Velocity[1]);} // Facing left + } + MoveAndSlide(); } void Flip(string facing){ + if (facing == "ANY") Scale = new Vector2(Scale[0] * -1,1); if (facing == "RIGHT") Scale = new Vector2(1,1); if (facing =="LEFT") Scale = new Vector2(-1,1); } @@ -95,6 +116,9 @@ public partial class IQEnemy : RigidBody2D } // Ground Detection - public void OnGroundEntered(Area2D area){if (area.CollisionLayer == 0) IsGrounded = true;} - public void OnGroundExited(Area2D area){if (area.CollisionLayer == 0) IsGrounded = false;} + public void OnGroundEntered(Area2D area){if (area.CollisionLayer == 0) GroundDetected = true;} + public void OnGroundExited(Area2D area){if (area.CollisionLayer == 0) GroundDetected = false;} + // Ledge Detection + public void OnLedgeEntered(Area2D area){if (area.CollisionLayer == 0) LedgeDetected = true;} + public void OnLedgeExited(Area2D area){if (area.CollisionLayer == 0) LedgeDetected = false;} } diff --git a/Assets/Scenes/DEVSCENES/IQEnemy.tscn b/Assets/Scenes/DEVSCENES/IQEnemy.tscn index 8dd956a..6463988 100644 --- a/Assets/Scenes/DEVSCENES/IQEnemy.tscn +++ b/Assets/Scenes/DEVSCENES/IQEnemy.tscn @@ -5,6 +5,8 @@ [sub_resource type="RectangleShape2D" id="RectangleShape2D_bc7hl"] +[sub_resource type="RectangleShape2D" id="RectangleShape2D_41iyt"] + [sub_resource type="AtlasTexture" id="AtlasTexture_a1e3t"] atlas = ExtResource("2_o0b1e") region = Rect2(0, 64, 32, 32) @@ -125,15 +127,11 @@ animations = [{ "speed": 5.0 }] -[sub_resource type="RectangleShape2D" id="RectangleShape2D_41iyt"] - -[node name="IQEnemy" type="RigidBody2D"] +[node name="IQEnemy" type="CharacterBody2D"] collision_layer = 8 collision_mask = 3 script = ExtResource("1_p53ib") metadata/IQ = 20 -metadata/MaxHP = 100.0 -metadata/CurrentHP = 0.0 metadata/AbilityRegenerate = true [node name="GroundDetector" type="Area2D" parent="."] @@ -144,6 +142,23 @@ scale = Vector2(0.099999994, 0.099999994) scale = Vector2(0.99999994, 0.99999994) shape = SubResource("RectangleShape2D_bc7hl") +[node name="LedgeDetector" type="Area2D" parent="."] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="LedgeDetector"] +position = Vector2(12, 12) +scale = Vector2(0.1, 0.1) +shape = SubResource("RectangleShape2D_bc7hl") + +[node name="WallDetector" type="Area2D" parent="."] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="WallDetector"] +position = Vector2(12, 9.536743e-07) +scale = Vector2(0.1, 0.1) +shape = SubResource("RectangleShape2D_bc7hl") + +[node name="Hitbox" type="CollisionShape2D" parent="."] +shape = SubResource("RectangleShape2D_41iyt") + [node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."] position = Vector2(9.536743e-07, -1.9999999) scale = Vector2(0.99999994, 0.99999994) @@ -151,16 +166,3 @@ sprite_frames = SubResource("SpriteFrames_rqn21") autoplay = "default" frame = 1 frame_progress = 0.9866207 - -[node name="Hitbox" type="CollisionShape2D" parent="."] -shape = SubResource("RectangleShape2D_41iyt") - -[node name="LedgeDetector" type="CollisionShape2D" parent="."] -position = Vector2(12, 12) -scale = Vector2(0.1, 0.1) -shape = SubResource("RectangleShape2D_bc7hl") - -[node name="WallDetector" type="CollisionShape2D" parent="."] -position = Vector2(12, 9.536743e-07) -scale = Vector2(0.1, 0.1) -shape = SubResource("RectangleShape2D_bc7hl") diff --git a/Assets/Scripts/CommonScripts/CommonData.cs b/Assets/Scripts/CommonScripts/CommonData.cs index 7164694..5655a9a 100644 --- a/Assets/Scripts/CommonScripts/CommonData.cs +++ b/Assets/Scripts/CommonScripts/CommonData.cs @@ -33,7 +33,8 @@ public partial class CommonData : Node GetTree().Root.CallDeferred(Node.MethodName.AddChild, inst); // Start game by loading the main menu - scene = GD.Load("res://Assets/Scenes/MenusAndLevels/MainMenu.tscn"); + //scene = GD.Load("res://Assets/Scenes/MenusAndLevels/MainMenu.tscn"); + scene = GD.Load("res://Assets/Scenes/DEVSCENES/EnemyTest.tscn"); inst = scene.Instantiate(); GetTree().Root.CallDeferred(Node.MethodName.AddChild, inst); PlayMusic("MainMenu"); @@ -47,11 +48,9 @@ public partial class CommonData : Node NumFramesElapsed += 1; GlobalTimer = NumFramesElapsed / FPS; GlobalTimer = (float)Convert.ToDouble(GlobalTimer.ToString("0.##")); // Truncates seconds elapsed to hundreds place (0.00) + //GD.Print(GlobalTimer); + if (Input.IsActionJustPressed("quit_game")){GetTree().Quit();} - if (Input.IsActionJustPressed("quit_game")) - { - GetTree().Quit(); - } if (Input.IsActionJustPressed("mute")) { if (Muted) @@ -68,20 +67,13 @@ public partial class CommonData : Node } // BG Audio Management - public void StopMusic() - { - // Stop whatever audio is playing - GetNode("AudioStreamPlayer").Stop(); - } + public void StopMusic(){GetNode("AudioStreamPlayer").Stop();}// Stop whatever audio is playing\ + public void PlayMusic(string AudioName) { // Load target audio and play the audio GetNode("AudioStreamPlayer").Stream = (Godot.AudioStream)GD.Load($"res://Assets/Audio/Music/{AudioName}.mp3"); GetNode("AudioStreamPlayer").Play(); } - void LoopAudio() - { - // Find when the stream finishes playing to play it again - GetNode("AudioStreamPlayer").Play(); - } + void LoopAudio(){GetNode("AudioStreamPlayer").Play();}// Find when the stream finishes playing to play it again }