Optimized enemy code

This commit is contained in:
=
2026-01-28 12:56:43 -07:00
parent 88ecd78c4e
commit 6a00e320b3
2 changed files with 176 additions and 44 deletions

View File

@@ -1,4 +1,4 @@
[gd_scene load_steps=15 format=4 uid="uid://b3g0hgfb0uw6a"]
[gd_scene load_steps=17 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"]
@@ -151,6 +151,139 @@ physics_layer_0/collision_layer = 1
physics_layer_0/collision_mask = 0
sources/0 = SubResource("TileSetAtlasSource_e35rp")
[sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_0b2hr"]
texture = ExtResource("1_q0x10")
0:0/0 = 0
1:0/0 = 0
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
10:0/0 = 0
11:0/0 = 0
0:1/0 = 0
1:1/0 = 0
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
1:2/0 = 0
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_7343r"]
sources/0 = SubResource("TileSetAtlasSource_0b2hr")
[sub_resource type="Animation" id="Animation_owkni"]
length = 0.001
tracks/0/type = "value"
@@ -228,10 +361,14 @@ _data = {
[node name="Level1" type="Node2D"]
[node name="PlayArea" type="TileMapLayer" parent="."]
tile_map_data = PackedByteArray("AAD//wAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAACAAAAAAAAAAAAAAADAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAFAAAAAAAAAAAAAAAGAAAAAAAAAAAAAAAHAAAAAAAAAAAAAAAIAAAAAAAAAAAA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AAAAD3////AAAAAAAAAAD2////AAAAAAAAAAD1////AAAAAAEAAAD1//7/AAAAAAAAAAD0//7/AAAAAAEAAAD5//z/AAAAAAAAAAD6//z/AAAAAAAAAAD8/wEAAAAAAAEAAAD7/wEAAAAAAAEAAAD6/wEAAAAAAAEAAAD5/wEAAAAAAAEAAAD4/wEAAAAAAAEAAAD3/wEAAAAAAAEAAAD2/wEAAAAAAAEAAAD1/wEAAAAAAAEAAAD0/wEAAAAAAAEAAAD0/wAAAAAAAAEAAAD0////AAAAAAEAAAD0//3/AAAAAAEAAAD0//z/AAAAAAEAAAD0//v/AAAAAAEAAAD0//r/AAAAAAEAAADz//r/AAAAAAEAAADz//v/AAAAAAEAAADz//z/AAAAAAEAAADz//3/AAAAAAEAAADz//7/AAAAAAEAAADz////AAAAAAEAAADz/wAAAAAAAAEAAAD1/wAAAAAAAAEAAAD2/wAAAAAAAAEAAADz/wEAAAAAAAEAAADz//n/AAAAAAAAAAD0//n/AAAAAAAAAAAKAAAAAAAAAAEAAAA=")
tile_set = SubResource("TileSet_q0ben")
[node name="Background" type="TileMapLayer" parent="."]
self_modulate = Color(0.50980395, 0.50980395, 0.50980395, 1)
z_index = -1
tile_map_data = PackedByteArray("AAD5//3/AAAAAAEAAAD5//7/AAAAAAEAAAD5////AAAAAAEAAAD6////AAAAAAEAAAD6//7/AAAAAAEAAAD6//3/AAAAAAEAAAD7////AAAAAAEAAAD8////AAAAAAEAAAD9////AAAAAAEAAAD9//7/AAAAAAEAAAD+//7/AAAAAAEAAAD+////AAAAAAEAAAD8//7/AAAAAAEAAAACAPz/AAAAAAEAAAACAP3/AAAAAAEAAAACAP7/AAAAAAEAAAACAP//AAAAAAEAAAADAP//AAAAAAEAAAADAP7/AAAAAAEAAAADAP3/AAAAAAEAAAADAPz/AAAAAAEAAAAGAP7/AAAAAAEAAAAGAP//AAAAAAEAAAAHAP//AAAAAAEAAAAHAP7/AAAAAAEAAAAFAP//AAAAAAEAAAD/////AAAAAAEAAAABAP7/AAAAAAEAAAABAP//AAAAAAEAAAAAAP//AAAAAAEAAAAMAP3/AAAAAAEAAAANAP3/AAAAAAEAAAAOAP3/AAAAAAEAAAAOAP7/AAAAAAEAAAAOAP//AAAAAAEAAAAOAAAAAAAAAAEAAAANAAAAAAAAAAEAAAAMAAAAAAAAAAEAAAAMAP//AAAAAAEAAAAMAP7/AAAAAAEAAAANAP7/AAAAAAEAAAANAP//AAAAAAEAAAALAAAAAAAAAAEAAAALAP//AAAAAAEAAAAKAAAAAAAAAAEAAAAPAAAAAAAAAAEAAAAQAAAAAAAAAAEAAAABAP3/AAAAAAAAAAD8//3/AAAAAAAAAAD7//7/AAAAAAAAAAAFAP7/AAAAAAAAAAALAP7/AAAAAAAAAAAPAP//AAAAAAAAAAAQAP//AAAAAAAAAAA=")
tile_set = SubResource("TileSet_7343r")
[node name="Foreground" type="TileMapLayer" parent="."]

View File

@@ -10,28 +10,14 @@ public partial class IQEnemy : CharacterBody2D
// Enemy Stats
public int IQ;
public float HP;
public float MaxHP;
float RegenerationAmmount;
float RegenerationInterval;
float WalkSpeed;
float JumpHeight;
float MaxHP, RegenerationAmmount, RegenerationInterval, WalkSpeed, JumpHeight, DetectionRange;
// Enemy Conditions
float CurrentYVelocity;
float PreviousYVelocity;
bool GroundDetected;
bool LedgeDetected;
bool WallDetected;
bool GroundDetected, LedgeDetected, WallDetected;
String FacingDirection;
// Enemy Abilities
bool AbilityJump = false;
bool AbilityMove = false;
bool AbilityAttack = false;
bool AbilityDash = false;
bool AbilityCloak = false;
bool AbilityTeleport = false;
bool AbilityRegenerate = false;
bool AbilityJump, AbilityMove, AbilityAttack, AbilityDash, AbilityCloak, AbilityTeleport, AbilityRegenerate = false;
public override void _Ready()
{
@@ -51,6 +37,8 @@ public partial class IQEnemy : CharacterBody2D
else WalkSpeed = 20.0f;
if (HasMeta("JumpHeight")) JumpHeight = (float)GetMeta("JumpHeight");
else JumpHeight = 250.0f;
if (HasMeta("DetectionRange")) DetectionRange = (float)GetMeta("DetectionRange");
else DetectionRange = 100.0f;
JumpHeight *= -1;
// Read and set stats
@@ -93,54 +81,56 @@ public partial class IQEnemy : CharacterBody2D
// Regenerate Health
if (AbilityRegenerate){
if (GlobalTimer % RegenerationInterval == 0){Regenerate(RegenerationAmmount);}
if (GlobalTimer % RegenerationInterval == 0) Regenerate(RegenerationAmmount);
}
// Movement
// Add gravity.
if (GroundDetected == false){Velocity = new Vector2(Velocity[0],Velocity[1] + 10);}
if (GroundDetected == false) Velocity = new Vector2(Velocity[0],Velocity[1] + 10);
#region 20IQ
if (IQ >= 0 && IQ <= 20){ // Goomba ahh diddy blud
if (GroundDetected && LedgeDetected == false){Flip();} // Flip when presented with a ledge
if (WallDetected){Flip();} // Flip when comeing in contact with a wall
if (GroundDetected){Velocity = new Vector2(WalkSpeed * VAH.Scale[0], Velocity[1]);} // Walk
if (GroundDetected && LedgeDetected == false) Flip(); // Flip when presented with a ledge
if (WallDetected) Flip(); // Flip when comeing in contact with a wall
if (GroundDetected) Velocity = new Vector2(WalkSpeed * VAH.Scale[0], Velocity[1]); // Walk
}
#endregion
#region 30IQ
if (IQ > 20 && IQ <= 30){ // Goomba w/ hopps (no judy)
Velocity = new Vector2(WalkSpeed * VAH.Scale[0], Velocity[1]); // Walk
if (WallDetected && GroundDetected && Velocity[1] == 0){Velocity = new Vector2(Velocity[0], JumpHeight);} // Jump
if (WallDetected && GroundDetected && Velocity[1] > 0){Flip();} // Wall too high
if (WallDetected && GroundDetected && Velocity[1] == 0) Velocity = new Vector2(Velocity[0], JumpHeight); // Jump
if (WallDetected && GroundDetected && Velocity[1] > 0) Flip(); // Wall too high
}
#endregion
#region 40IQ
if (IQ > 30 && IQ <= 40){ // Adds jumping when reaching edge as feature (also adding directional x force)
if (GroundDetected){Velocity = new Vector2(WalkSpeed * VAH.Scale[0], Velocity[1]);} // Walk on ground
else{Velocity = new Vector2(WalkSpeed * VAH.Scale[0] * 2, Velocity[1]);} // Walk on air
if (WallDetected && GroundDetected && Velocity[1] == 0){Velocity = new Vector2(Velocity[0], JumpHeight);} // Jump
//if (GroundDetected && LedgeDetected == false && Velocity[1] == 0){Velocity = new Vector2(Velocity[0] * 50, JumpHeight);} // Jump at ledge
if (GroundDetected && LedgeDetected == false && Velocity[1] == 0){Velocity = new Vector2(Velocity[0], JumpHeight);} // Jump at ledge
if (WallDetected && GroundDetected && Velocity[1] > 0){Flip();} // Wall too high
Velocity = (GroundDetected) ? new Vector2(WalkSpeed * VAH.Scale[0], Velocity[1]) : new Vector2(WalkSpeed * VAH.Scale[0] / 2, Velocity[1]); // Walk on air or ground
if (WallDetected && GroundDetected && Velocity[1] == 0) Velocity = new Vector2(Velocity[0], JumpHeight); // Jump
//if (GroundDetected && LedgeDetected == false && Velocity[1] == 0) Velocity = new Vector2(Velocity[0] * 50, JumpHeight); // Jump at ledge
if (GroundDetected && LedgeDetected == false && Velocity[1] == 0) Velocity = new Vector2(Velocity[0], JumpHeight); // Jump at ledge
if (WallDetected && GroundDetected && Velocity[1] > 0) Flip(); // Wall too high
}
#endregion
#region 60IQ
if (IQ > 40 && IQ <= 60){ // Concious of what side of the enemy the player is on
var CurrentLevel = GetTree().Root.GetChildren();
float DistanceFromPlayerX = GlobalPosition[0] - CurrentLevel[2].GetNode<Node2D>("Player").GlobalPosition[0];
if (DistanceFromPlayerX > (30.0f / (IQ / 4))){ if (VAH.Scale[0] > 0){Flip();}} // left of player
if (DistanceFromPlayerX < (-30.0f / (IQ / 4))){ if (VAH.Scale[0] < 0){Flip();}} // right of player
if (DistanceFromPlayerX < (-30.0f / (IQ / 4)) && GroundDetected && GlobalPosition[0] > CurrentLevel[2].GetNode<Node2D>("Player").GlobalPosition[1]) {Velocity = new Vector2(Velocity[0], JumpHeight);} // Jump when player is overhead
if (GroundDetected){Velocity = new Vector2(WalkSpeed * VAH.Scale[0], Velocity[1]);} // Walk on ground
else{Velocity = new Vector2(WalkSpeed * VAH.Scale[0] * 2, Velocity[1]);} // Walk on air
if (WallDetected && GroundDetected && Velocity[1] == 0){Velocity = new Vector2(Velocity[0], JumpHeight);} // Jump
//if (GroundDetected && LedgeDetected == false && Velocity[1] == 0){Velocity = new Vector2(Velocity[0] * 50, JumpHeight);} // Jump at ledge
if (GroundDetected && LedgeDetected == false && Velocity[1] == 0){Velocity = new Vector2(Velocity[0], JumpHeight);} // Jump at ledge
if (DetectsPlayer()){ // Pursuing Player
var CurrentLevel = GetTree().Root.GetChildren();
float DistanceFromPlayerX = GlobalPosition[0] - CurrentLevel[2].GetNode<Node2D>("Player").GlobalPosition[0];
if (DistanceFromPlayerX > (30.0f / (IQ / 4))) if (VAH.Scale[0] > 0)Flip(); // left of player
if (DistanceFromPlayerX < (-30.0f / (IQ / 4))) if (VAH.Scale[0] < 0)Flip(); // right of player
if (DistanceFromPlayerX < (-30.0f / (IQ / 4)) && GroundDetected && GlobalPosition[0] > CurrentLevel[2].GetNode<Node2D>("Player").GlobalPosition[1]) Velocity = new Vector2(Velocity[0], JumpHeight); // Jump when player is overhead
} else { // Patrol
if (WallDetected && GroundDetected && Velocity[1] > 0)Flip(); // Wall too high
}
// Common Behaviors
Velocity = (GroundDetected) ? new Vector2(WalkSpeed * VAH.Scale[0], Velocity[1]) : new Vector2(WalkSpeed * VAH.Scale[0] / 2, Velocity[1]); // Walk on air or ground
if (WallDetected && GroundDetected && Velocity[1] == 0) Velocity = new Vector2(Velocity[0], JumpHeight); // Jump
//if (GroundDetected && LedgeDetected == false && Velocity[1] == 0) Velocity = new Vector2(Velocity[0] * 50, JumpHeight); // Jump at ledge
if (GroundDetected && LedgeDetected == false && Velocity[1] == 0) Velocity = new Vector2(Velocity[0], JumpHeight); // Jump at ledge
}
#endregion
#region 80IQ
if (IQ > 60 && IQ <= 80){ // Maintan distance to avoid melee attacks and optimise ranged attacks
if (IQ > 60 && IQ <= 80){ // Maintain distance to avoid melee attacks and optimise ranged attacks
}
#endregion
#region 1000IQ
@@ -166,6 +156,11 @@ public partial class IQEnemy : CharacterBody2D
else if (FacingDirection == "LEFT"){FacingDirection = "RIGHT";}
}
bool DetectsPlayer(){
var CurrentLevel = GetTree().Root.GetChildren();
return (GlobalPosition.DistanceTo(CurrentLevel[2].GetNode<Node2D>("Player").GlobalPosition) <= DetectionRange) ? true : false;
}
public void Regenerate(float ammount){
if (HP < MaxHP) HP += ammount;
if (HP > MaxHP) HP = MaxHP; // Catch errors