Fixed enemy ai bugs

This commit is contained in:
=
2026-01-29 06:20:48 -07:00
parent 4392367ef6
commit b58f4412f2
3 changed files with 17 additions and 19 deletions

File diff suppressed because one or more lines are too long

View File

@@ -113,16 +113,15 @@ public partial class IQEnemy : CharacterBody2D
#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) Jump();
if (WallDetected && GroundDetected && Velocity[1] == 0) Jump(0.0f);
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)
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) Jump();
//if (GroundDetected && LedgeDetected == false && Velocity[1] == 0) Velocity = new Vector2(Velocity[0] * 50, JumpHeight); // Jump at ledge with x motion
if (GroundDetected && LedgeDetected == false && Velocity[1] == 0) Jump(); // Jump at ledge
Velocity = (GroundDetected) ? new Vector2(WalkSpeed * VAH.Scale[0], Velocity[1]) : new Vector2 (Velocity[0] + 0.05f, Velocity[1]); // Walk on ground : air
if (WallDetected && GroundDetected) Jump(0.0f);
if (GroundDetected && LedgeDetected == false) Jump(50.0f * VAH.Scale[0]); // Jump at ledge with x motion
if (WallDetected && GroundDetected && Velocity[1] > 0) Flip(); // Wall too high
}
#endregion
@@ -133,15 +132,14 @@ public partial class IQEnemy : CharacterBody2D
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]) Jump(); // Jump when player is overhead
} else { // Patrol
if (WallDetected && GroundDetected && Velocity[1] > 0)Flip(); // Wall too high
if (DistanceFromPlayerX < (-30.0f / (IQ / 4)) && GroundDetected && GlobalPosition[1] > CurrentLevel[2].GetNode<Node2D>("Player").GlobalPosition[1]) Jump(0.0f); // Jump when player is overhead
} else{ // Patrol
if (WallDetected && GroundDetected && Velocity[1] > 0)Flip(); // Wall too high (flips when it lands on the ground in front of a wall)
}
// 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) Jump(); // Jump
//if (GroundDetected && LedgeDetected == false && Velocity[1] == 0) Velocity = new Vector2(Velocity[0] * 50, JumpHeight); // Jump at ledge with x motion
if (GroundDetected && LedgeDetected == false && Velocity[1] == 0) Jump(); // Jump at ledge
Velocity = (GroundDetected) ? new Vector2(WalkSpeed * VAH.Scale[0], Velocity[1]) : new Vector2 (Velocity[0] + 0.05f, Velocity[1]); // Walk on ground : air
if (WallDetected && GroundDetected) Jump(0.0f); // Jump when touching a wall
if (GroundDetected && LedgeDetected == false) Jump(50.0f * VAH.Scale[0]); // Jump at ledge with x motion
}
#endregion
#region 80IQ
@@ -172,8 +170,8 @@ public partial class IQEnemy : CharacterBody2D
else if (FacingDirection == "LEFT"){FacingDirection = "RIGHT";}
}
void Jump(){
if (GroundDetected && CanJump) {Velocity = new Vector2(Velocity[0], JumpHeight);
void Jump(float xMagnitude){
if (GroundDetected && CanJump) {Velocity = new Vector2(Velocity[0] + xMagnitude, JumpHeight);
CanJump = false;}
}

View File

@@ -33,8 +33,8 @@ public partial class CommonData : Node
GetTree().Root.CallDeferred(Node.MethodName.AddChild, inst);
// Start game by loading the main menu
//scene = GD.Load<PackedScene>("res://Assets/Scenes/MenusAndLevels/MainMenu.tscn");
scene = GD.Load<PackedScene>("res://Assets/Scenes/DEVSCENES/EnemyTest.tscn");
scene = GD.Load<PackedScene>("res://Assets/Scenes/MenusAndLevels/MainMenu.tscn");
//scene = GD.Load<PackedScene>("res://Assets/Scenes/DEVSCENES/EnemyTest.tscn");
inst = scene.Instantiate();
GetTree().Root.CallDeferred(Node.MethodName.AddChild, inst);
PlayMusic("MainMenu");