Optimised enemy code

This commit is contained in:
weedmjac000
2026-01-24 11:04:26 -07:00
parent 582d276a00
commit f1ab5a352c
2 changed files with 81 additions and 19 deletions

View File

@@ -3,10 +3,15 @@ using System;
public partial class IQBasedEnemy : RigidBody2D
{
// Misc
float GlobalTimer;
// Enemy Stats
public int IQ;
public float HP;
public float MaxHP;
float RegenerationAmmount;
float RegenerationInterval;
// Enemy Conditions
float CurrentYVelocity;
@@ -14,32 +19,79 @@ public partial class IQBasedEnemy : RigidBody2D
public bool IsGrounded;
// Enemy Abilities
bool AbilityJump = false;
bool AbilityMove = false;
bool AbilityAttack = false;
bool AbilityDash = false;
bool AbilityCloak = false;
bool AbilityTeleport = false;
bool AbilityRegenerate = false;
public override void _Ready()
{
IQ = (int)GetMeta("IQ"); // Read IQ
{
// Read & Set Abilities
if (HasMeta("AbilityJump")) AbilityJump = (bool)GetMeta("AbilityJump");
if (HasMeta("AbilityMove")) AbilityMove = (bool)GetMeta("AbilityMove");
if (HasMeta("AbilityAttack")) AbilityAttack = (bool)GetMeta("AbilityAttack");
if (HasMeta("AbilityDash")) AbilityDash = (bool)GetMeta("AbilityDash");
if (HasMeta("AbilityCloak")) AbilityCloak = (bool)GetMeta("AbilityCloak");
if (HasMeta("AbilityTeleport")) AbilityTeleport = (bool)GetMeta("AbilityTeleport");
if (HasMeta("AbilityRegenerate")) AbilityRegenerate = (bool)GetMeta("AbilityRegenerate");
if (HasMeta("RegenerationAmmount")) RegenerationAmmount = (float)GetMeta("RegenerationAmmount");
else RegenerationAmmount = 1.0f;
if (HasMeta("RegenerationInterval")) RegenerationInterval = (float)GetMeta("RegenerationInterval");
else RegenerationInterval = 1.0f;
// Read and set stats
if (HasMeta("MaxHP")) MaxHP = (float)GetMeta("MaxHP");
MaxHP = (float)GetMeta("MaxHP"); // Get Max HP
if (IQ >= 100)
MaxHP += 20;
HP = MaxHP;
CurrentYVelocity = 0f;
PreviousYVelocity = 0f;
// Set up ground detection
GetNode<Area2D>("GroundDetector").AreaEntered += OnGroundEntered;
GetNode<Area2D>("GroundDetector").AreaExited += OnGroundExited;
IsGrounded = true;
// Apply IQ based permenant modifiers
IQ = (int)GetMeta("IQ"); // Read IQ
if (IQ >= 100) MaxHP += 20;
HP = MaxHP;
}
public override void _Process(double delta)
{
// Ground Detection
CurrentYVelocity = LinearVelocity[1];
if (CurrentYVelocity == 0 && PreviousYVelocity == 0)
IsGrounded = true;
else
IsGrounded = false;
PreviousYVelocity = CurrentYVelocity;
// Incriment Global Timer
GlobalTimer += (float)delta;
// Regenerate Health
if (AbilityRegenerate){
if (GlobalTimer % RegenerationInterval == 0) Regenerate(RegenerationAmmount);
}
}
void Flip(string facing){
if (facing == "RIGHT") Scale = new Vector2(1,1);
if (facing =="LEFT") Scale = new Vector2(-1,1);
}
public void Regenerate(float ammount){
if (HP < MaxHP) HP += ammount;
if (HP > MaxHP) HP = MaxHP; // Catch errors
}
public void Damage(float ammount){
HP -= ammount;
if (HP <= 0) Kill();
}
public void Kill(){
QueueFree(); // Change to proper death w/ corpse
}
// Ground Detection
public void OnGroundEntered(Area2D area){if (area.CollisionLayer == 0) IsGrounded = true;}
public void OnGroundExited(Area2D area){if (area.CollisionLayer == 0) IsGrounded = false;}
}