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

@@ -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