TileMapLayer collisions with area2d sucessfull

This commit is contained in:
=
2026-01-26 13:15:45 -07:00
parent f647f5ed08
commit 318397ee29
3 changed files with 47 additions and 26 deletions

View File

@@ -20,6 +20,8 @@ public partial class IQEnemy : CharacterBody2D
float PreviousYVelocity;
bool GroundDetected;
bool LedgeDetected;
bool WallDetected;
String FacingDirection;
// Enemy Abilities
bool AbilityJump = false;
@@ -45,22 +47,26 @@ public partial class IQEnemy : CharacterBody2D
if (HasMeta("RegenerationInterval")) RegenerationInterval = (float)GetMeta("RegenerationInterval");
else RegenerationInterval = 1.0f;
if (HasMeta("WalkSpeed")) WalkSpeed = (float)GetMeta("WalkSpeed");
else WalkSpeed = 10.0f;
else WalkSpeed = 20.0f;
if (HasMeta("JumpHeight")) JumpHeight = (float)GetMeta("JumpHeight");
else JumpHeight = 10.0f;
else JumpHeight = 20.0f;
// Read and set stats
if (HasMeta("MaxHP")) MaxHP = (float)GetMeta("MaxHP");
// Set up ground detection
GetNode<Area2D>("GroundDetector").AreaEntered += OnGroundEntered;
GetNode<Area2D>("GroundDetector").AreaExited += OnGroundExited;
// Set up collision detection
GetNode<Area2D>("GroundDetector").BodyEntered += OnGroundEntered; // Ground
GetNode<Area2D>("GroundDetector").BodyExited += OnGroundExited;
GetNode<Area2D>("LedgeDetector").BodyEntered += OnLedgeEntered; // Ledge
GetNode<Area2D>("LedgeDetector").BodyExited += OnLedgeExited;
GetNode<Area2D>("WallDetector").BodyEntered += OnWallEntered; // Wall
GetNode<Area2D>("WallDetector").BodyExited += OnWallExited;
GetNode<Area2D>("LedgeDetector").AreaEntered += OnLedgeEntered;
GetNode<Area2D>("LedgeDetector").AreaExited += OnLedgeExited;
GroundDetected = false;
LedgeDetected = false;
WallDetected = false;
GroundDetected = true;
LedgeDetected = true;
FacingDirection = "RIGHT";
// Apply IQ based permenant modifiers
IQ = (int)GetMeta("IQ"); // Read IQ
@@ -86,19 +92,27 @@ public partial class IQEnemy : CharacterBody2D
// Movement
// Add gravity.
if (GroundDetected == false){Velocity = new Vector2(Velocity[0],-1);}
if (GroundDetected == false){Velocity = new Vector2(Velocity[0],100);}
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
if (GroundDetected && LedgeDetected == false){Flip();}
if (WallDetected){Flip();}
// Walk
if (GroundDetected){Velocity = new Vector2(WalkSpeed * Scale[0], Velocity[1]);}
}
MoveAndSlide();
MoveAndSlide(); // Updates physics applied this frame
}
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);
void Flip(){
if (FacingDirection == "RIGHT"){
Scale = new Vector2(-1,1);
FacingDirection = "LEFT";
}
else if (FacingDirection == "LEFT")
{
Scale = new Vector2(1,1);
FacingDirection = "RIGHT";
}
}
public void Regenerate(float ammount){
@@ -116,9 +130,12 @@ public partial class IQEnemy : CharacterBody2D
}
// Ground Detection
public void OnGroundEntered(Area2D area){if (area.CollisionLayer == 0) GroundDetected = true;}
public void OnGroundExited(Area2D area){if (area.CollisionLayer == 0) GroundDetected = false;}
public void OnGroundEntered(Node2D body){if (body is TileMapLayer tileLayer) GroundDetected = true;}
public void OnGroundExited(Node2D body){if (body is TileMapLayer tileLayer) 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;}
public void OnLedgeEntered(Node2D body){if (body is TileMapLayer tileLayer) LedgeDetected = true;}
public void OnLedgeExited(Node2D body){if (body is TileMapLayer tileLayer) LedgeDetected = false;}
// Wall Detection
public void OnWallEntered(Node2D body){if (body is TileMapLayer tileLayer) WallDetected = true;}
public void OnWallExited(Node2D body){if (body is TileMapLayer tileLayer) WallDetected = false;}
}